RadioButton and CheckBox Controls: Where is the value attribute?

Discussion in 'ASP .Net Web Controls' started by Nathan Sokalski, Nov 10, 2007.

  1. In the RadioButton and CheckBox controls, there is no Value attribute. The
    html <input> tag has a value attribute, which is used by both type="radio"
    and type="checkbox". The RadioButtonList and CheckBoxList controls have
    SelectedValue properties (I realize that the RadioButtonList uses the
    <select> and <option> tags instead of the <input> tag, but needing a value
    server-side exists either way). I am attempting to write a control with
    functionality similar to that of the RadioButtonList but that has a textbox
    next to some of the RadioButtons for extra specs when filling out a form.
    However, because I cannot use the <select> tag due to the limitations on
    what tags are allowed between the opening & closing tags, I am forced to use
    the RadioButton control. But I am trying to figure out how to specify and
    retrieve the value for the selected RadioButton. What can I do? Thanks.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/
    Nathan Sokalski, Nov 10, 2007
    #1
    1. Advertising

  2. Nathan Sokalski

    Teemu Keiski Guest

    If you need value attribute in client-rendered markup you could just write

    RadioButton1.Attributes["value"] = "myValue";

    RadioButtonList does not use SELECT anywhere. If you have RadioButtonList as
    follows on the Page

    <asp:RadioButtonList ID="RadioButtonList1" runat="server">
    <asp:ListItem Text="Text 1" Value="Value 1" />
    <asp:ListItem Text="Text 2" Value="Value 2" />
    </asp:RadioButtonList>

    and then you view HTML source of the Page,. you'll find

    <table id="RadioButtonList1" border="0">
    <tr>
    <td><input id="RadioButtonList1_0" type="radio" name="RadioButtonList1"
    value="Value 1" /><label for="RadioButtonList1_0">Text 1</label></td>
    </tr><tr>
    <td><input id="RadioButtonList1_1" type="radio" name="RadioButtonList1"
    value="Value 2" /><label for="RadioButtonList1_1">Text 2</label></td>
    </tr>
    </table>

    Simple way to develop that type of control is to derive from RadioButtonList
    and override RenderItem method. Following demonstrates a very simple way to
    add TextBox next to the RadioButton and have it contain the text of the
    corresponding list item.

    namespace Samples
    {
    public class MyRadioButtonList : RadioButtonList
    {

    protected override void RenderItem(ListItemType itemType, int
    repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
    {
    writer.AddAttribute(HtmlTextWriterAttribute.Type, "text");
    writer.AddAttribute(HtmlTextWriterAttribute.Value,
    this.Items[repeatIndex].Text);
    writer.RenderBeginTag(HtmlTextWriterTag.Input);
    writer.RenderEndTag();
    base.RenderItem(itemType, repeatIndex, repeatInfo, writer);
    }

    }
    }

    Of course there probably is rendering TB with suitable ID, grabbing that
    posted data on postback and such but it is quite easy and manual task once
    you get used to server control basics. In this case implementing
    IPostBackDataHandler interface

    http://msdn2.microsoft.com/en-us/library/system.web.ui.ipostbackdatahandler.aspx


    --
    Teemu Keiski
    AspInsider, ASP.NET MVP
    http://blogs.aspadvice.com/joteke
    http://teemukeiski.net



    "Nathan Sokalski" <> wrote in message
    news:...
    > In the RadioButton and CheckBox controls, there is no Value attribute. The
    > html <input> tag has a value attribute, which is used by both type="radio"
    > and type="checkbox". The RadioButtonList and CheckBoxList controls have
    > SelectedValue properties (I realize that the RadioButtonList uses the
    > <select> and <option> tags instead of the <input> tag, but needing a value
    > server-side exists either way). I am attempting to write a control with
    > functionality similar to that of the RadioButtonList but that has a
    > textbox next to some of the RadioButtons for extra specs when filling out
    > a form. However, because I cannot use the <select> tag due to the
    > limitations on what tags are allowed between the opening & closing tags, I
    > am forced to use the RadioButton control. But I am trying to figure out
    > how to specify and retrieve the value for the selected RadioButton. What
    > can I do? Thanks.
    > --
    > Nathan Sokalski
    >
    > http://www.nathansokalski.com/
    >
    Teemu Keiski, Nov 11, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Leo J. Hart IV
    Replies:
    5
    Views:
    4,104
    Leo J. Hart IV
    Oct 10, 2004
  2. Replies:
    1
    Views:
    4,264
    Joe Kesselman
    Aug 2, 2006
  3. Replies:
    8
    Views:
    521
  4. Nathan Sokalski
    Replies:
    1
    Views:
    428
    Teemu Keiski
    Nov 11, 2007
  5. Imran Aziz

    checkbox value attribute in checkbox control ?

    Imran Aziz, Aug 6, 2005, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    227
    Imran Aziz
    Aug 8, 2005
Loading...

Share This Page