ASP.NET RadioButton Derived Control with Value

Sometimes you put up with things unconsciously until one day you think, hang on, why don't I just to "x." So what I am referring to is simply that the ASP.NET RadioButton control does not have a property for Value.  I have no doubt that this is purposeful, but either way, I required a RadioButtonControl with a value Property. 

Hide Code [-]
using System.Web.UI.WebControls;

/// <summary>
/// Summary description for ValueRadioButton
/// </summary>
public class ValueRadioButton : RadioButton
{
    private string m_value;

    public string Value
    {
        get { return m_value; }
        set { m_value = value; }
    }

    public ValueRadioButton()
    {
        //
        // TODO: Add constructor logic here
        //
    }
}
{..} Click Show Code

 

Cheers,

 

Andrew :-)

Published Monday, May 05, 2008 12:07 AM by REA_ANDREW

Comments

# re: ASP.NET RadioButton Derived Control with Value

Sunday, May 04, 2008 9:57 PM by pbz

If you need that value after postback then you need to store the "value" in the ViewState...

# re: ASP.NET RadioButton Derived Control with Value

Sunday, May 04, 2008 10:22 PM by REA_ANDREW

I create a variable, private with public accessor, as type ValueRadioButton.  Then in the RadioButton CheckChanged event, I assigned the sender object as the value of this variable.  I can then get the selected object and value.

Andrew :-)

# re: ASP.NET RadioButton Derived Control with Value

Thursday, July 24, 2008 3:39 PM by Jonathan Fleury

Exactly what I needed  !!! Thanks a lot

# re: ASP.NET RadioButton Derived Control with Value

Monday, November 23, 2009 7:05 AM by Patrick

You could use default RadioButton class to hold value - it's not mapped/exposed as property thou.

<asp:RadioButton ID="radio1" runat="server" GroupName="Priority" Text="Urgent" value="1" />

To get the value in the code:

string value = radio1.Attributes["value"];

That also applies to all controls implementing IAttributeAccessor (ie. all WebControls) - if the attribute provided in html representation of the element IS NOT object's property it will always be accessible through Attribute Collection.

So <asp:SomeControl ID="sc1" runat="server" SomeAttribute="some value" /> will be accessible via sc1.Attributes["SomeAttribute"];

# Twitter Trackbacks for ASP.NET RadioButton Derived Control with Value - Andrew Rea [asp.net] on Topsy.com

Pingback from  Twitter Trackbacks for                 ASP.NET RadioButton Derived Control with Value - Andrew Rea         [asp.net]        on Topsy.com

Leave a Comment

(required) 
(required) 
(optional)
(required)