C# custom control to render other controls
How can I use a custom control to render other controls? Obviously, at the 开发者_开发技巧Render stage, it is too late. test works as it does not contain <asp:sometag>, but test2 is what I want to be able to write out and render properly
protected override void Render(HtmlTextWriter writer)
{
const string test = @"<a href='http://www.something.com'></a>";
const string test2 = "<asp:HyperLink ID='HyperLink1' runat='server' NavigateUrl='www.something.com'>Some Text</asp:HyperLink>";
writer.Write(test2);
}
You can override the CreateChildControls method as shown in this example:
protected override void CreateChildControls()
{
// Add a LiteralControl to the current ControlCollection.
this.Controls.Add(new LiteralControl("<h3>Value: "));
// Create a text box control, set the default Text property,
// and add it to the ControlCollection.
TextBox box = new TextBox();
box.Text = "0";
this.Controls.Add(box);
this.Controls.Add(new LiteralControl("</h3>"));
}
Instead of the literal/textbox controls, you would instantiate and add a HyperLink control, e.g:
var link = new HyperLink();
link.NavigateUrl = "...";
link.Text = "...";
this.Controls.Add(link);
精彩评论