C# 使用空属性渲染WebControl
是否可以构建从允许“空”属性的C# 使用空属性渲染WebControl,c#,asp.net,C#,Asp.net,是否可以构建从允许“空”属性的System.Web.UI.WebControls.WebControl派生的控件 即,我需要输出 我尝试过重写renderndtag()和this.Attributes.Add(“helloworld”,null),但两者都不能正常工作。否。ASP.NET web表单中用于控件解析的语法要求属性必须具有值 您最好使用内部内容: <asp:Label runat="server" id="fooLabel" Text="Hello, world!"/>
System.Web.UI.WebControls.WebControl
派生的控件
即,我需要输出
我尝试过重写
renderndtag()
和this.Attributes.Add(“helloworld”,null)
,但两者都不能正常工作。否。ASP.NET web表单中用于控件解析的语法要求属性必须具有值
您最好使用内部内容:
<asp:Label runat="server" id="fooLabel" Text="Hello, world!"/>
与
<asp:Label runat="server" id="fooLabel">Hello, world!</asp:Label>
你好,世界!
看得更深一点,下面的代码可以工作
protected override void AddAttributesToRender(HtmlTextWriter writer)
{
writer.AddAttribute("helloworld", null);
}
将System.Web.UI.HtmlTextWriter.RenderBegin()反射到写入属性的中间位置,显示以下行:
...
this.writer.Write(' ');
this.writer.Write(renderAttribute.name);
if (renderAttribute.value != null)
{
this.writer.Write("=\"");
...
这说明了为什么上面的方法有效,而this.Attributes.Add
则无效(我相信运行过滤器是为了排除任何空属性值)