20

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?

4
  • do you need it in a gridview? Commented Jul 25, 2013 at 13:04
  • @kobe in this case - no. Why what would be the difference? Commented Jul 25, 2013 at 13:06
  • Refer MS Site to explore more http://msdn.microsoft.com/en-us/library/7a9d6h4f(v=vs.100).aspx Commented Jul 25, 2013 at 13:08
  • @AntonBelev because gridview can send CommandArgument with controls. Commented Jul 25, 2013 at 13:15

2 Answers 2

32

It's perfectly possible:

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

string chckValue = myChck.Attributes["Value"].ToString();
Sign up to request clarification or add additional context in comments.

1 Comment

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.
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()
    {

    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.