all groups > asp.net webcontrols > november 2007 >
You're in the

asp.net webcontrols

group:

RadioButton and CheckBox Controls: Where is the value attribute?


RadioButton and CheckBox Controls: Where is the value attribute? Nathan Sokalski
11/10/2007 1:59:28 PM
asp.net webcontrols:
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
njsokalski@hotmail.com
http://www.nathansokalski.com/

Re: RadioButton and CheckBox Controls: Where is the value attribute? Teemu Keiski
11/11/2007 10:29:55 AM
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



[quoted text, click to view]

AddThis Social Bookmark Button