Click here to Skip to main content
15,899,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
in front end page i'm using repeater control for printing many number of radios my front end is
<asp:Repeater ID="QuestionRepeater" runat="server" >
               <HeaderTemplate>
                   <table>
               </HeaderTemplate>
               <SeparatorTemplate>
                   <tr>
                       <td>
                           <br />
                       </td>
                   </tr>
               </SeparatorTemplate>
               <ItemTemplate>
                   <tr>
                       <td>
                          <%#Eval("Question1") %>
                       </td>
                   </tr>
                   <tr>
                       <td>
                           <asp:RadioButton runat="server" ID="rb1" GroupName="Rb_Choice" Text='<%#Eval("Choice_1") %>' Checked="false" OnCheckedChanged="CheckChanged"></asp:RadioButton>
                       </td>
                   </tr>
                   <tr>
                       <td>
                           <asp:RadioButton runat="server" ID="rb2" GroupName="Rb_Choice" Text='<%#Eval("Choice_2") %>' OnCheckedChanged="CheckChanged"></asp:RadioButton>
                       </td>
                   </tr>
                   <tr>
                       <td>
                           <asp:RadioButton runat="server" ID="rb3" GroupName="Rb_Choice" Text='<%#Eval("Choice_3") %>' OnCheckedChanged="CheckChanged"></asp:RadioButton>
                       </td>
                   </tr>
                   <tr>
                       <td>
                           <asp:RadioButton runat="server" ID="rb4" GroupName="Rb_Choice" Text='<%#Eval("Choice_4") %>' OnCheckedChanged="CheckChanged"></asp:RadioButton>
                       </td>
                   </tr>
               </ItemTemplate>
               <FooterTemplate>
                   </table>
               </FooterTemplate>
           </asp:Repeater>

and i want to find out in back end page radio button checked value

What I have tried:

my back end i'm just display msg box for the radio is checked or not like
protected void Get_Answers(object sender, EventArgs e)
       {
           //var text = "Your Answers will be submitted successfully";
           //Response.Write(text);
           foreach (RepeaterItem item in QuestionRepeater.Items)
           {
               RadioButton rb1 = (RadioButton)item.FindControl("rb1");
               //Response.Write(rb1.GroupName+"<br>");
               RadioButton rb2 = (RadioButton)item.FindControl("rb2");
               RadioButton rb3 = (RadioButton)item.FindControl("rb3");
               RadioButton rb4 = (RadioButton)item.FindControl("rb4");
               ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('"+ rb1.Text + ":" + rb1.Checked + rb2.Text + ":" + rb2.Checked + rb3.Text + ":" + rb3.Checked + rb4.Text + ":" + rb4.Checked + "')", true);

i'm always get false even though the radio button is checked
please help me how to get checked value
Posted
Updated 5-Feb-18 0:44am

1 solution

First of all make sure you are not binding the repeater on postback otherwise you're just resetting all the values to their initial state

if (!Page.IsPostBack)
{
    // bind your repeater here
}


Next you're only doing a js alert for the last question in the repeater. The "msg" param in RegisterClientScriptBlock is the key for that script block, and keys are unique, so if you have 5 questions you are trying to add 5 script blocks called "msg" but as msg is the key .net will only use the last one you configure as each block of that key replaces the previous ones. If you want an alert per question you need a unique key.

int i = 0;
foreach (RepeaterItem item in QuestionRepeater.Items)
{
    RadioButton rb1 = (RadioButton)item.FindControl("rb1");
    //Response.Write(rb1.GroupName+"<br>");
    RadioButton rb2 = (RadioButton)item.FindControl("rb2");
    RadioButton rb3 = (RadioButton)item.FindControl("rb3");
    RadioButton rb4 = (RadioButton)item.FindControl("rb4");
    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg" + (i++).ToString(), "alert('" + rb1.Text + ":" + rb1.Checked + rb2.Text + ":" + rb2.Checked + rb3.Text + ":" + rb3.Checked + rb4.Text + ":" + rb4.Checked + "');", true);
}


Note that as well as the unique key I also added a ";" after the alert.
 
Share this answer
 
Comments
Member 13658833 5-Feb-18 6:59am    
Thank you very much sir for your replay.
sir,cloud please explain how to store radio button checked values in a list after submit button click
i'm very much new to this so,please explain clearly like above
what i'm tried in back end is
protected void CheckChanged(object sender, EventArgs e)
{
string value=null;
//on each item checked, remove any other items checked
foreach (RepeaterItem item in QuestionRepeater.Items)
{
RadioButton rb1 = ((RadioButton)item.FindControl("rb1"));
RadioButton rb2 = ((RadioButton)item.FindControl("rb2"));
RadioButton rb3 = ((RadioButton)item.FindControl("rb3"));
RadioButton rb4 = ((RadioButton)item.FindControl("rb4"));
if (rb1.Checked)
{
value = rb1.Text;
}
else if (rb2.Checked)
{
value = rb2.Text;
}
else if (rb3.Checked)
{
value = rb3.Text;
}
else if(rb4.Checked)
{
value = rb4.Text;
}
}
F-ES Sitecore 5-Feb-18 7:30am    
The checked event isn't going to help you much. Define a class that can hold your answers, this will normally have an ID that shows which question it is the answer for, and a property to hold the chosen answer

public class Answer
{
public int QuestionID { get; set; }
public string Value { get; set; }
}

then in your submit event loop through the buttons and build up a list of Answer objects

List<Answer> answers = new List<Answer>();

foreach (RepeaterItem item in QuestionRepeater.Items)
{
RadioButton rb1 = (RadioButton)item.FindControl("rb1");
RadioButton rb2 = (RadioButton)item.FindControl("rb2");
RadioButton rb3 = (RadioButton)item.FindControl("rb3");
RadioButton rb4 = (RadioButton)item.FindControl("rb4");

Answer a = new Answer();
a.QuestionID = 123; // make this the ID of the question, not sure how you define that
if (rb1.Checked)
{
a.Value = rb1.Text;
}
else if (rb2.Checked)
{
a.Value = rb2.Text;
}
else if (rb3.Checked)
{
a.Value = rb3.Text;
}
else if (rb4.Checked)
{
a.Value = rb4.Text;
}

answers.Add(a);
}
Member 13658833 6-Feb-18 5:50am    
Thank you very much

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