如何使用 C# 检索 HTML5 data-* 属性
How to retrieve HTML5 data-* Attributes using C#
我的表单中有一个 asp 复选框:
<asp:CheckBox id="option" runat="server" OnCheckedChanged="checkChange" data-attributeA="somevalue1" data-attributeB="somevalue2" AutoPostBack="true" />`
在我的 OnCheckedChanged
事件中,我想检索这两个数据属性。
protected void checkChange(object sender, EventArgs e) {}
我该怎么做?
@musefan 分享的 link 中的相同方法对您有用。
我创建了一个复选框:
<asp:CheckBox ID="CheckBox1" runat="server" OnCheckedChanged="CheckBox1_CheckedChanged" dataAttributeA="Test Custom Attr A" dataAttributeB="Test Custom B" Text="Check it or dont" AutoPostBack="True" />
然后是处理变化事件的方法:
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
String customAttr1 = CheckBox1.Attributes["dataAttributeA"].ToString();
String customAttr2 = CheckBox1.Attributes["dataAttributeB"].ToString();
Response.Write("<h1> Custom Attributes A and B = " + customAttr1 + " " + customAttr2);
}
最后我将 CheckBox 的 AutoPostBack 属性 设置为 true,所以它的更改事件将在单击时立即触发。
我得到了预期的结果
Custom Attributes A and B = Test Custom Attr A Test Custom B
我的表单中有一个 asp 复选框:
<asp:CheckBox id="option" runat="server" OnCheckedChanged="checkChange" data-attributeA="somevalue1" data-attributeB="somevalue2" AutoPostBack="true" />`
在我的 OnCheckedChanged
事件中,我想检索这两个数据属性。
protected void checkChange(object sender, EventArgs e) {}
我该怎么做?
@musefan 分享的 link 中的相同方法对您有用。
我创建了一个复选框:
<asp:CheckBox ID="CheckBox1" runat="server" OnCheckedChanged="CheckBox1_CheckedChanged" dataAttributeA="Test Custom Attr A" dataAttributeB="Test Custom B" Text="Check it or dont" AutoPostBack="True" />
然后是处理变化事件的方法:
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
String customAttr1 = CheckBox1.Attributes["dataAttributeA"].ToString();
String customAttr2 = CheckBox1.Attributes["dataAttributeB"].ToString();
Response.Write("<h1> Custom Attributes A and B = " + customAttr1 + " " + customAttr2);
}
最后我将 CheckBox 的 AutoPostBack 属性 设置为 true,所以它的更改事件将在单击时立即触发。
我得到了预期的结果
Custom Attributes A and B = Test Custom Attr A Test Custom B