当编写定制WebControl类,你仍然可以使用标准的ASP.NET控件,避免需要使用标签渲染方法:
[ToolboxData("{0}:MyCustomControl>")]
public class MyCustomControl : CompositeControl
{
public MyCustomControl()
{
}
public string Text
{
get
{
object o = ViewState["Text"];
return ((o == null) ? "Set my text!" : (string)o);
}
set
{
ViewState["Text"] = value;
}
}
protected override void CreateChildControls()
{
// Create controls
var label = new Label();
label.ID = "innerLabel";
label.Text = this.Text;
// Add controls
this.Controls.Add(label);
// Call base method
base.CreateChildControls();
}
}
或者你可以使用像这样的渲染方法标签:
[ToolboxData("{0}:MyCustomControl>")]
public class MyCustomControl : CompositeControl
{
public MyCustomControl()
{
}
public override void RenderControl(HtmlTextWriter writer)
{
base.RenderEndTag(writer);
if (!this.DesignMode)
{
var label = new Label();
label.Text = "Hello!";
label.RenderControl(writer);
}
}
}
希望这回答你的问题:■