HighTechTalks DotNet Forums  

RadioButton and CheckBox Controls: Where is the value attribute?

ASP.net Web Controls microsoft.public.dotnet.framework.aspnet.webcontrols


Discuss RadioButton and CheckBox Controls: Where is the value attribute? in the ASP.net Web Controls forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Nathan Sokalski
 
Posts: n/a

Default RadioButton and CheckBox Controls: Where is the value attribute? - 11-10-2007 , 12:59 PM






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 (AT) hotmail (DOT) com
http://www.nathansokalski.com/



Reply With Quote
  #2  
Old   
Teemu Keiski
 
Posts: n/a

Default Re: RadioButton and CheckBox Controls: Where is the value attribute? - 11-11-2007 , 02:29 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



"Nathan Sokalski" <njsokalski (AT) hotmail (DOT) com> wrote

Quote:
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 (AT) hotmail (DOT) com
http://www.nathansokalski.com/




Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.