RadioButton and CheckBox Controls: Where is the value attribute?

N

Nathan Sokalski

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.
 
T

Teemu Keiski

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top