为什么回发后不呈现 CheckBox 的 InputAttributes 和 LabelAttributes?
Why are InputAttributes and LabelAttributes of a CheckBox not rendered after a postback?
我在标准 ASP.NET CheckBox 控件中看到一些奇怪的行为。这是一个重现:
ASPX 标记
<form runat="server">
<asp:CheckBox runat="server" ID="cb" Text="Foo" />
<asp:Button runat="server" Text="Submit" />
</form>
C# 代码隐藏
protected override void OnLoad(EventArgs e)
{
if (!IsPostBack)
{
cb.Attributes.Add("data-a", "1");
cb.InputAttributes.Add("data-b", "2");
cb.LabelAttributes.Add("data-c", "3");
}
}
在初始页面请求中,CheckBox 控件呈现所有三个 data
属性:
<span data-a="1"> <!-- RIGHT -->
<input id="cb" type="checkbox" name="cb" data-b="2" /> <!-- RIGHT -->
<label for="cb" data-c="3">Foo</label> <!-- RIGHT -->
</span>
但是在我单击“提交”按钮后,CheckBox 控件只呈现 data-a
属性:
<span data-a="1"> <!-- RIGHT -->
<input id="cb" type="checkbox" name="cb" /> <!-- WRONG -->
<label for="cb">Foo</label> <!-- WRONG -->
</span>
为什么回发后缺少 InputAttributes 和 LabelAttributes?
Heisenbug Alert:如果我在OnLoad中设置断点,点击按钮,然后检查cb.Attributes["data-a"]
、cb.InputAttributes["data-b"]
和cb.LabelAttributes["data-c"]
,则值分别为 1
、2
和 null
(而不是 3
)。此外,检查值会影响输出!
<span data-a="1"> <!-- RIGHT -->
<input id="cb" type="checkbox" name="cb" data-b="2" /> <!-- RIGHT -->
<label for="cb" data-b="2">Foo</label> <!-- WTF?! -->
</span>
如果您仍然卡在这个问题上,这是一个错误。
升级到 .net framework 4.8(及更高版本)以进行修复。
我看到您还向 Microsoft 提交了错误报告:https://developercommunity.visualstudio.com/content/problem/287564/buggy-handling-of-inputattributes-and-labelattribu.html
我在标准 ASP.NET CheckBox 控件中看到一些奇怪的行为。这是一个重现:
ASPX 标记
<form runat="server">
<asp:CheckBox runat="server" ID="cb" Text="Foo" />
<asp:Button runat="server" Text="Submit" />
</form>
C# 代码隐藏
protected override void OnLoad(EventArgs e)
{
if (!IsPostBack)
{
cb.Attributes.Add("data-a", "1");
cb.InputAttributes.Add("data-b", "2");
cb.LabelAttributes.Add("data-c", "3");
}
}
在初始页面请求中,CheckBox 控件呈现所有三个 data
属性:
<span data-a="1"> <!-- RIGHT -->
<input id="cb" type="checkbox" name="cb" data-b="2" /> <!-- RIGHT -->
<label for="cb" data-c="3">Foo</label> <!-- RIGHT -->
</span>
但是在我单击“提交”按钮后,CheckBox 控件只呈现 data-a
属性:
<span data-a="1"> <!-- RIGHT -->
<input id="cb" type="checkbox" name="cb" /> <!-- WRONG -->
<label for="cb">Foo</label> <!-- WRONG -->
</span>
为什么回发后缺少 InputAttributes 和 LabelAttributes?
Heisenbug Alert:如果我在OnLoad中设置断点,点击按钮,然后检查cb.Attributes["data-a"]
、cb.InputAttributes["data-b"]
和cb.LabelAttributes["data-c"]
,则值分别为 1
、2
和 null
(而不是 3
)。此外,检查值会影响输出!
<span data-a="1"> <!-- RIGHT -->
<input id="cb" type="checkbox" name="cb" data-b="2" /> <!-- RIGHT -->
<label for="cb" data-b="2">Foo</label> <!-- WTF?! -->
</span>
如果您仍然卡在这个问题上,这是一个错误。
升级到 .net framework 4.8(及更高版本)以进行修复。
我看到您还向 Microsoft 提交了错误报告:https://developercommunity.visualstudio.com/content/problem/287564/buggy-handling-of-inputattributes-and-labelattribu.html