如何使用C#检索HTML5数据 - *属性

Art*_*rti 10 c# asp.net html5 code-behind custom-data-attribute

我的表单中有一个asp复选框:

<asp:CheckBox id="option" runat="server" OnCheckedChanged="checkChange" data-attributeA="somevalue1" data-attributeB="somevalue2" AutoPostBack="true" />`
Run Code Online (Sandbox Code Playgroud)

在我的OnCheckedChanged事件中,我想要检索这两个数据属性.

protected void checkChange(object sender, EventArgs e) {}
Run Code Online (Sandbox Code Playgroud)

我怎么做?

ala*_*mpo 11

@musefan共享链接中的相同方法对您有用.

我创建了一个CheckBox:

<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" />
Run Code Online (Sandbox Code Playgroud)

然后是一个处理已更改事件的方法:

 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);

    }
Run Code Online (Sandbox Code Playgroud)

最后我将CheckBox的AutoPostBack属性设置为true,因此只要单击它就会触发它的更改事件.

我已经获得了预期的结果

自定义属性A和B =测试自定义属性A测试自定义B.