【问题标题】:How to retrieve HTML5 data-* Attributes using C#如何使用 C# 检索 HTML5 数据-* 属性
【发布时间】:2015-04-24 15:53:42
【问题描述】:
我的表单中有一个 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) {}
我该怎么做?
【问题讨论】:
标签:
c#
asp.net
html
code-behind
custom-data-attribute
【解决方案1】:
@musefan 分享的链接中的相同方法对您有用。
我创建了一个复选框:
<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,所以只要点击它就会触发它的 change 事件。
我已经得到了预期的结果
自定义属性 A 和 B = 测试自定义属性 A 测试自定义 B