在asp.net中,如果使用自定義屬性,通常它會按原樣呈現。Asp.net複選框和html數據屬性
考慮到這個標記(注:屬性,如id
,name
和for
在所有的例子作爲自己生成的ID被拆除/名稱詳細):
<asp:TextBox runat="server" data-foo="bar" />
呈現在asp.net爲:
<input type="text" data-foo="bar" />
也就是說,asp.net保持data-foo
不變。
複選框通常呈現:
<asp:CheckBox runat="server" Text="Normal" />
呈現爲:
<asp:CheckBox runat="server" Text="Custom attribute" data-foo="bar" />
它呈現爲:
<input type="checkbox" />
<label>Normal</label>
但是,如果你在一個複選框添加自定義屬性:
<span data-foo="bar">
<input type="checkbox" />
<label>Custom attribute</label>
</span>
正如您所看到的,呈現的跨度用於保存屬性。如果您在後面的代碼中添加屬性,也會發生這種情況。這與任何其他HtmlControl,AFAIK都不會發生。
有誰知道爲什麼這個範圍被渲染來保存屬性?
無論如何要呈現輸入標籤中的屬性?
有趣..只是好奇,你有什麼用額外的屬性爲\ –