Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
<asp:Repeater ID="Repeater0" runat="server">
          <ItemTemplate>
              <table>
                  <tr>
                      <td>
                           <div id="lblEmployeeId" ><%# Eval("QId")%>    <%# Eval("QText")%></div>
                          <asp:HiddenField ID="QID" runat="server" Value='<%# Eval("QId")%>'/>

                          <br />

                      </td>

                  </tr>
                  <tr class="table-row">
                      <td>

                          <asp:Repeater ID="Repeater1" runat="server">
                                   <ItemTemplate>
                               <div runat="server"> <input type="radio" name='<%#Eval("QID")%>'  value='<%#Eval("AOptions")%>' id="AnswerOptions"><%#Eval("AOptions")%></input></div>   <br />
                                    <%--   <asp:RadioButton ID="RadioButton1" runat="server" name='<%#Eval("QID")%>' id='<%#Eval("ID")%>' value='<%#Eval("AOptions")%>' /><%#Eval("AOptions")%><br />--%>
                                   </ItemTemplate>
                              </asp:Repeater>
                          <br />
                      </td>
                  </tr>
              </table>









public partial class Quiz : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!this.IsPostBack)
        {
            BindEmployeeData();
            Repeater();
            
        }
    }
    private void BindEmployeeData()
    {
        string connection = ConfigurationManager.AppSettings["Connection"].ToString();
        using (SqlConnection con = new SqlConnection(connection))
        {
            using (SqlCommand objCommand = new SqlCommand("SELECT * FROM t_Question", con))
            {
                using (SqlDataAdapter objSqlDataAdapter = new SqlDataAdapter(objCommand))
                {
                    DataTable dt = new DataTable();
                    dt.Columns.Add("QId");
                    dt.Columns.Add("QText");

                    objSqlDataAdapter.Fill(dt);
                    Repeater0.DataSource = dt;
                    Repeater0.DataBind();
                }
            }
        }


    }
    private void Repeater()
    {
        for (int i = 0; i <= Repeater0.Items.Count - 1; i++)
        {
            

            string connection = "server=sv01;database=testdb;uid=sa;password=mpx123";
            Repeater Repeater1 = (Repeater)Repeater0.Items[i].FindControl("Repeater1");
            HiddenField QID = (HiddenField)Repeater0.Items[i].FindControl("QID");
            SqlConnection ak = new SqlConnection(connection);
            ak.Open();
            string query = "Select * from [dbo].[t_Answers] where QId=" + QID.Value;
            SqlDataAdapter adp= new SqlDataAdapter(query, ak);
            DataTable ck = new DataTable();
            //ck.Columns.Add("AOptions");            
            adp.Fill(ck);
            Repeater1.DataSource = ck;
            Repeater1.DataBind();
            ak.Close();
        }
    }

    protected void BtnSubmit_Click(object sender, EventArgs e)
    {
        var collection = Repeater0.Items.Count;
        
        foreach (var item in collection)
        {
            
        }
    }



  
}


I have used two repeater in this, one for question,question Id and other for radio button to get data from database, but now i don't know how to save data that every radio button consist whenever user gonna check that option .

What I have tried:

I am trying for-each loop but don't know how to use it with nested repeater


protected void BtnSubmit_Click(object sender, EventArgs e)
{
var collection = Repeater0.Items.Count;

foreach (var item in collection)
{

}
}
Posted
Updated 19-Jun-17 23:47pm
v3
Comments
Kornfeld Eliyahu Peter 20-Jun-17 4:29am    
Isn't it data-bound (the repeater)?
Member 11644373 20-Jun-17 4:59am    
Yes it is
Kornfeld Eliyahu Peter 20-Jun-17 5:01am    
So? Just save your data source to the DB, changes should be delivered because of the bounded nature of the display...
Member 11644373 20-Jun-17 5:05am    
I can't understand what you trying to say..let me clear my question actually i want whenever user click on any radio button from four options i want the option clicked to be saved into database whenever user click on submit button
Kornfeld Eliyahu Peter 20-Jun-17 5:10am    
What I'm saying is this:
1. Your control is data bounded, so any changes to the control should be reflected on the data source
2. So when you are submitting the page, all you have left is to save the data source. You need not worry about updating the data source from the controls as it done because the data binding...
All that of course in case you done your binding right...
See some articles here:
https://msdn.microsoft.com/en-us/library/cc425007(v=vs.80).aspx
http://www.webcodeexpert.com/2013/04/how-to-bind-edit-update-and-delete-data.html

1 solution

Simplest way is to read the answers from the Request. The "name" of your radio buttons are the question ID so if you Request.Form[QId] you'll get the selected answer

protected void BtnSubmit_Click(object sender, EventArgs e)
{
    var collection = Repeater0.Items;

    foreach (RepeaterItem item in collection)
    {
        HiddenField QID = (HiddenField)item.FindControl("QID");

        int q = int.Parse(QID.Value);

        string answer = Request.Form[q.ToString()];

    }
}


An alternative would be to use server-side radio buttons, or a radiobuttonlist, and access those controls programmatically.
 
Share this answer
 
Comments
Member 11644373 20-Jun-17 6:26am    
Thankyou for helping me

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