19

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?

Anton Belev
  • 11,963
  • 22
  • 70
  • 111

2 Answers2

31

It's perfectly possible:

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

string chckValue = myChck.Attributes["Value"].ToString();
emerson.marini
  • 9,331
  • 2
  • 29
  • 46
  • Thank you. That is working. I've used a few times and I had some problems with persisting values of custom attributes to view state or sth like that. I don't really remember what was the problem, but I still have some concerns when using this methods. Anyways at the moment it seems to work for me. – Anton Belev Jul 25 '13 at 13:15
5

You could create a new class that inherits the CheckBox class (or any other control class for that matter) and add any further properties you need to the derived class. That way you would get an extended CheckBox more or less.

public class ExtendedCheckBox : CheckBox
{
    public string Value
    {
        get;
        set;
    }

    public ExtendedCheckBox : base()
    {

    }
}