How to add custom attributes to ASP.NET controls

Anton Belev picture Anton Belev · Jul 25, 2013 · Viewed 36.2k times · Source

I've got an ASP.NET control say checkbox:

<asp:CheckBox ID="myChck" runat="server" Value="myCustomValue" />

Is it possible to add this custom Value attribute from code-behind and respectively get the value from Value

Something like (psuedocode):

myCkck.Value = "blq blq";
string chckValue = myChck.Value;

How can I do this?

Answer

melancia picture melancia · Jul 25, 2013

It's perfectly possible:

myCkck.Attributes.Add("Value", "blq blq");

string chckValue = myChck.Attributes["Value"].ToString();