ASP.NET - radiobutton select at a time

Asked By reena mehta on 16-Feb-11 12:49 AM

Hi,

I am trying to implement multiple choice questions within my web application. I have datalist within a datalist. First datalist contains questions, second datalist contains choice, only one choice should be selectable at a time. I am using radio button. Even though I have set RadioButton.GroupName property, I can't make them selectable one at a time.

<asp:ListView ID="lvTemplate" runat="server" ItemPlaceholderID="divTemplateContent"
               DataKeyNames="TemplateId"  onitemcommand="lvTemplate_ItemCommand" onselectedindexchanged="lvTemplate_SelectedIndexChanged"
                >
                <LayoutTemplate>
                  <div id="divTemplate">
                    <asp:PlaceHolder ID="divTemplateContent" runat="server" />
                  </div>
                </LayoutTemplate>
                <ItemTemplate>
                  <div id="divTemplateContent">
                    <img src='TemplateImage/<%#Eval("TemplateImage") %>' alt="Image" style="height:100px; width:80%;"/><br />
                   
                    <asp:RadioButton ID="rbTemplate" runat="server"  GroupName="g1" Text=' <%#Eval("TemplateName")%>'  AutoPostBack="False" />
                    
                   
                    <br /> 
                    <asp:HiddenField ID="hdnId" runat="server" Value='<%#Eval("TemplateId")%>' />
                    <asp:TextBox ID="txttemplateid" runat="server" Text='<%#Eval("TemplateId")%>' Visible="false" ></asp:TextBox>
                  </div>
                </ItemTemplate>
                <EmptyDataTemplate>
                  <asp:Label ID="lblMsg" runat="server" Text="Template Not Available" /><br /> 
                </EmptyDataTemplate>
              </asp:ListView>

foreach (ListViewDataItem row in lvTemplate.Items)
      {

        foreach (Control rb in row.Controls)
        {

          RadioButton rbb = (RadioButton)rb.FindControl("rbTemplate") as RadioButton;
         // RadioButtonList rbl = (RadioButtonList)rb.FindControl("renltemp") as RadioButtonList;


          if (rbb != null && rbb.Checked)

my prob is  radiobutton   does nt select at i time

Jatin Prajapati replied to reena mehta on 28-Feb-11 10:42 AM
Hi,
This is happening, due to the name attribute of the rendered radio button. If you see the rendered page source,
you can find that each radio button has different name property value. So, set that property value to it's containing table id. And it will work fine.
Jatin Prajapati replied to reena mehta on 28-Feb-11 10:46 AM
Hi,
Try the below code.

01.<%@ Page Language="C#" %>
02. 
03.<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
04. 
05.<script runat="server">
06. 
07.  protected void lv_ItemCreated(object sender, ListViewItemEventArgs e)
08.  {
09.    //RadioButton rb = e.Item.FindControl("rb") as RadioButton;
10.    ////if (rb != null)
11.    //{
12.    //    rb.Attributes["name"] = "productlist";
13.    //}
14.  }
15. 
16.  protected void lv_ItemDataBound(object sender, ListViewItemEventArgs e)
17.  {
18.    //RadioButton rb = e.Item.FindControl("rb") as RadioButton;
19.    //if (rb != null)
20.    //{
21.    //    rb.InputAttributes["name"] = lv.ID + "_table1";
22.    //}
23.  }
24. 
25.  protected void lv_PreRender(object sender, EventArgs e)
26.  {
27.    foreach (ListViewItem item in lv.Items)
28.    {
29.      RadioButton rb = item.FindControl("rb") as RadioButton;
30.      if (rb != null)
31.      {
32.        rb.InputAttributes.Clear();
33.        rb.InputAttributes.Add("name", "lv_table1");
34.      }
35.    }
36.  }
37.</script>
38. 
39.<html xmlns="http://www.w3.org/1999/xhtml">
40.<head runat="server">
41.  <title></title>
42.</head>
43.<body>
44.  <form id="form1" runat="server">
45.  <div>
46.    <asp:ListView ID="lv" runat="server" DataSourceID="SqlDataSource1"
47.      Visible="true" onprerender="lv_PreRender">
48.      <LayoutTemplate>
49.        <table runat="server" id="table1">
50.          <tr runat="server" id="itemPlaceHolder"></tr>
51.        </table>
52.      </LayoutTemplate>
53.      <ItemTemplate>
54.        <tr runat="server">
55.          <td runat="server">
56.            <input type="radio" name="lv_table1" title='<%#Eval("ProductName") %>' />
57.            <%#Eval("ProductName") %>
58.          </td>
59.        </tr>
60.      </ItemTemplate>
61.    </asp:ListView>
62.    <br />
63.    ---------------------------------------
64.    <asp:RadioButtonList ID="RadioButtonList1" runat="server"
65.      DataSourceID="SqlDataSource1" DataTextField="ProductName"
66.      DataValueField="ProductName" Visible="true">
67.    </asp:RadioButtonList>
68.    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
69.      ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
70.      SelectCommand="SELECT Top 5 [ProductName] FROM [Alphabetical list of products]">
71.    </asp:SqlDataSource>
72.  </div>
73.  </form>
74.</body>
75.</html>