2013-12-12 30 views
-1

是否可以構建一個從System.Web.UI.WebControls.WebControl派生的允許「空」屬性的控件?渲染具有空屬性的WebControl

I.e.我需要輸出<div helloworld></div>

我試過壓倒RenderEndTag()this.Attributes.Add("helloworld",null)並且都不能正常工作。

回答

0

看着深一點,下面的代碼工作;

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沒有(我相信一個過濾器運行,以排除任何空屬性值)。

0

否。用於ASP.NET Web窗體中控件分析的語法要求屬性必須具有值。

你能做的最好是使用內部內容:

<asp:Label runat="server" id="fooLabel" Text="Hello, world!"/> 

相同

<asp:Label runat="server" id="fooLabel">Hello, world!</asp:Label>