Home » Category » DotNet

DotNet: HtmlInputRadioButton Inside a Datalist

204| Fri, 06 Jun 2008 13:44:00 GMT| souravduttagupta| Comments (3)
I am having some problem regarding radiobutton inside a datalist. I want to
have a radiobutton on each row of my datalist. I want to check a particular
button, all the other would remain unchecked. Also I want to get the checked
value from my code behind.

I have tried with the following code. Each time I click the button to find
the value of the radio button, getting an error "Object reference is not set".

HTML
====
(In the ItemTemplate of a DataList "dtlTesting")

<ItemTemplate>
<INPUT id="RBList" style="WIDTH: 16px; HEIGHT: 20px" type="radio"
name="group"><%# DataBinder.Eval(Container.DataItem, "Name") %></INPUT>
</ItemTemplate>

CodeBehind( On Button click)
============================

private void Button1_Click(object sender, System.EventArgs e)
{
foreach(DataListItem oItem in dtlTesting.Items)
{
//Find the previous selected RadioButton
bool blnRadio =
((System.Web.UI.HtmlControls.HtmlInputRadioButton) oItem.FindControl("RBList")).Checked;
string
str=((System.Web.UI.HtmlControls.HtmlInputRadioBut ton)oItem.FindControl("RBList")).Value;

if (blnRadio)
{
TextBox1.Text=str;
}
}
}

Keywords & Tags: htmlinputradiobutton, inside, datalist, dotnet, .net

URL: http://dotnet.itags.org/dotnet-tech/313844/
 
«« Prev - Next »» 3 helpful answers below.
Add runat="server" as an attribute of RBList.
Christopher A. Reed
"The oxen are slow, but the earth is patient."

"Sourav Dutta Gupta" <SouravDuttaGupta...discussions.microsoft.com> wrote in
message news:283FF4F8-4FF5-4042-B27E-A17BE03BDCB5...microsoft.com...
>I am having some problem regarding radiobutton inside a datalist. I want to
> have a radiobutton on each row of my datalist. I want to check a
> particular
> button, all the other would remain unchecked. Also I want to get the
> checked
> value from my code behind.
> I have tried with the following code. Each time I click the button to find
> the value of the radio button, getting an error "Object reference is not
> set".
> HTML
> ====
> (In the ItemTemplate of a DataList "dtlTesting")
> <ItemTemplate>
> <INPUT id="RBList" style="WIDTH: 16px; HEIGHT: 20px" type="radio"
> name="group"><%# DataBinder.Eval(Container.DataItem, "Name") %></INPUT>
> </ItemTemplate>
>
> CodeBehind( On Button click)
> ============================
> private void Button1_Click(object sender, System.EventArgs e)
> {
> foreach(DataListItem oItem in dtlTesting.Items)
> {
> //Find the previous selected RadioButton
> bool blnRadio =
> ((System.Web.UI.HtmlControls.HtmlInputRadioButton) oItem.FindControl("RBList")).Checked;
> string
> str=((System.Web.UI.HtmlControls.HtmlInputRadioBut ton)oItem.FindControl("RBList")).Value;
> if (blnRadio)
> {
> TextBox1.Text=str;
> }
> }
> }


christopherreed | Fri, 06 Jun 2008 13:45:00 GMT |

By using runat="server", all the radio buttons inside the datalist acts as
individual buttons, i.e., we can select all of them at a time which I don't
want. I want to select only one button at a time.

"Christopher Reed" wrote:

> Add runat="server" as an attribute of RBList.
> --
> Christopher A. Reed
> "The oxen are slow, but the earth is patient."
> "Sourav Dutta Gupta" <SouravDuttaGupta...discussions.microsoft.com> wrote in
> message news:283FF4F8-4FF5-4042-B27E-A17BE03BDCB5...microsoft.com...
>
>

souravduttagupta | Fri, 06 Jun 2008 13:46:00 GMT |

You cannot access your HtmlInputRadioButton from the server without it.

You can use an ItemCommand event for the your DataList and then access the
specific row radio input button using the event arguments parameter from the
corresponding method.
Christopher A. Reed
"The oxen are slow, but the earth is patient."

"Sourav Dutta Gupta" <SouravDuttaGupta...discussions.microsoft.com> wrote in
message news:F0AA826F-4083-4A52-BC04-58901FA40073...microsoft.com...[vbcol=seagreen]
> By using runat="server", all the radio buttons inside the datalist acts as
> individual buttons, i.e., we can select all of them at a time which I
> don't
> want. I want to select only one button at a time.
> "Christopher Reed" wrote:

christopherreed | Fri, 06 Jun 2008 13:47:00 GMT |

DotNet Hot Answers

DotNet New questions

DotNet Related Categories