我在向自定义 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标签上?
最佳答案
这里有两种向复选框添加样式的方法,您使用的方法将尝试为浏览器呈现 UserAgent detected从而将复选框包装在具有样式属性的范围内。
另一种方式是将样式属性直接添加到复选框。
MyCheckbox.InputAttributes.Add("some", "style");
关于asp.net 3.5 设置自定义 web 控件中控件的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9224891/