Out*_*per 2 c# asp.net rendering custom-controls
如何使用自定义控件呈现其他控件?显然,在渲染阶段,为时已晚.测试工作,因为它不包含<asp:sometag>,但test2是我想要写出来并正确呈现
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);
}
Run Code Online (Sandbox Code Playgroud)
您可以覆盖CreateChildControls方法,如下例所示:
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>"));
}
Run Code Online (Sandbox Code Playgroud)
您将实例化并添加HyperLink控件,而不是文字/文本框控件,例如:
var link = new HyperLink();
link.NavigateUrl = "...";
link.Text = "...";
this.Controls.Add(link);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1864 次 |
最近记录: |