Click here to Skip to main content
15,907,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have four radio butons,Group name of all is same.
i want to get the text of selected one in codebehind.

this is how i have added the controls in design page...
<tr>
<td colspan="4">
    <asp:RadioButton ID="opt1" runat="server" GroupName="a" />
   
    <asp:RadioButton ID="opt2" runat="server" GroupName="a" />
   
    <asp:RadioButton ID="opt3" runat="server" GroupName="a" />
   
    <asp:RadioButton ID="opt4" runat="server" GroupName="a" />
    </td>
</tr>

code to populate the radiobuttons text...
try
       {
           dsques = dbcon.ExecuteDataSet("select * from tblExamineeExam where examineeid='" + userid + "'");
           if (dsques.Tables.Count > 0 && dsques.Tables[0].Rows.Count > 0)
           {
               int i = Convert.ToInt32(lblqno.Text) - 1;
               //for (int i = no; i < dsques.Tables[0].Rows.Count; i++)
               //{
               lblqno.Text = i.ToString();
               lblquestion.Text = dsques.Tables[0].Rows[i][1].ToString();
               opt1.Text = dsques.Tables[0].Rows[i][2].ToString();
               opt2.Text = dsques.Tables[0].Rows[i][3].ToString();
               opt3.Text = dsques.Tables[0].Rows[i][4].ToString();
               opt4.Text = dsques.Tables[0].Rows[i][5].ToString();
               //}
           }
       }
       catch (Exception ex)
       {

       }


now i want text of slected radiobutton..
sttring strAnswr = ;//need code here


Thanks!
Posted
Comments
JoCodes 29-Nov-13 1:17am    
You can use asp:radiobuttonlist control for the same and by using rblistId.SelectedValue you can fetch the selected value
[no name] 29-Nov-13 1:24am    
nice suggestion. Thanks.

One way you can do it following way

in aspx page i used yours markup with additional text property

XML
<asp:RadioButton ID="opt1" runat="server" Text="opt1 radioButton selected" GroupName="a"/>
     <asp:RadioButton ID="opt2" runat="server" Text="opt2 radioButton selected" GroupName="a" />
     <asp:RadioButton ID="opt3" runat="server" Text="opt3 radioButton selected" GroupName="a" />
     <asp:RadioButton ID="opt4" runat="server" Text="opt4 radioButton selected" GroupName="a" />
       <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />


in code being we can make out our selected text like below
C#
protected void Button1_Click(object sender, EventArgs e)
         {
             string checkedval="";
             if (opt1.Checked)
             { checkedval = opt1.Text; }
             else if (opt2.Checked)
             { checkedval = opt2.Text; }
             else if (opt3.Checked)
             { checkedval = opt3.Text; }
             else if (opt4.Checked)
             { checkedval = opt4.Text; }
             Response.Write(checkedval);

         }




Other way is to use radiobutton list
<asp:radiobuttonlist id="RadioButtonList1" runat="server" xmlns:asp="#unknown">
       <asp:listitem value="1">opt1</asp:listitem>
       <asp:listitem value="1">opt2</asp:listitem>
       <asp:listitem value="1">opt3</asp:listitem>
       <asp:listitem value="1">opt4</asp:listitem>
      </asp:radiobuttonlist>


C#
protected void Button1_Click(object sender, EventArgs e)
{
    string checkedval="";
    checkedval= RadioButtonList1.SelectedItem.Text;
    Response.Write(checkedval);
}
 
Share this answer
 
Hi,
In your case follow this.
C#
string strAnswr= string.Empty;
if(opt1.Checked)
{
    strAnswr = opt1.Text;
}
else if(opt2.Checked)
{
    strAnswr = opt2.Text;
}
else if(opt3.Checked)
{
    strAnswr = opt3.Text;
}
else
{
    strAnswr = opt4.Text;
}

It can be more simple if you bind it with RadioButtonList.
ASP
//suppose you have
<asp:radiobuttonlist id="RadioButtonList1" runat="server" xmlns:asp="#unknown">
</asp:radiobuttonlist>

C#
string strAnswr= RadioButtonList1.SelectedItem.Text; // simple

Hope it helps you.
Thanks.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900