2015-02-23 51 views
6

我在形式的ASP複選框:如何檢索數據 - HTML5屬性*使用C#

<asp:CheckBox id="option" runat="server" OnCheckedChanged="checkChange" data-attributeA="somevalue1" data-attributeB="somevalue2" AutoPostBack="true" />` 

在我OnCheckedChanged事件中,我想找回這兩個數據的屬性。

protected void checkChange(object sender, EventArgs e) {} 

我該怎麼辦呢?

+1

的可能重複[我怎樣才能訪問ASP.Net自定義文本框的屬性? ](http://stackoverflow.com/questions/12785946/how-can-i-access-custom-textbox-attributes-in-asp-net) – musefan 2015-02-23 10:45:22

回答

8

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

    } 

最後我已經設置的複選框,真正的AutoPostBack屬性,所以它的變化事件一旦被點擊就會被觸發。

我獲得了預期的結果

自定義屬性A和B =測試自定義的Attr一個測試自定義乙