我在自定义 Web 控件中向控件添加样式属性时遇到问题。下面是一个非常简单的自定义 Web 控件,只是为了说明问题:
[ParseChildren(true)]
[ToolboxData("<{0}:SomeControl runat=\"server\"></{0}:SomeControl>")]
public class SomeControl : WebControl
{
public CheckBox MyCheckbox { get; set; }
protected override void CreateChildControls()
{
MyCheckbox = new CheckBox { Text = "Here is some text" };
MyCheckbox.Style.Add("some", "style");
Controls.Add(MyCheckbox);
base.CreateChildControls();
}
}
在页面上使用时,我得到以下输出:
<span><span style="some:style;"><input id="ctl03" type="checkbox" name="ctl03" /><label for="ctl03">Here is some text</label></span></span>
为什么 style 属性在 span 标签上而不是在 input 标签上?