Radiobtn selection I make on question 1 is being duplicated

Mar 1 2018 10:08 AM
I am creating a quiz application in ASP.net (c#) and I currently have one question at a time displaying with the use of a datapager. However, If I chose radiobutton1 and then move on to question 2, It has radiobutton1 already selected, even though I am yet to answer that question. So for some reason the selection I make on question 1 is being duplicated to the other questions - I think it's obviously something to do with the setting and retreiving of the viewstates. Any help would be greatly appreciated!!
 
  1. <body>  
  2.         <form id="form1" runat="server">  
  3.             <div>  
  4.                 <asp:ListView ID="lvCustomers" runat="server" GroupPlaceholderID="groupPlaceHolder1"  
  5.                     ItemPlaceholderID="itemPlaceHolder1" OnPagePropertiesChanging="OnPagePropertiesChanging" OnPreRender="ListPager_PreRender">  
  6.       
  7.                     <LayoutTemplate>  
  8.                         <div id="itemPlaceHolder1" runat="server">  
  9.                         </div>  
  10.                         <asp:DataPager ID="DataPager1" runat="server" PagedControlID="lvCustomers" PageSize="1">  
  11.                             <Fields>  
  12.                                 <asp:NextPreviousPagerField runat="server" ButtonType="Link"  
  13.                                     ShowFirstPageButton="false"  
  14.                                     ShowPreviousPageButton="true"  
  15.                                     ShowNextPageButton="false" />  
  16.                                 <asp:NumericPagerField ButtonType="Link" />  
  17.                                 <asp:NextPreviousPagerField ButtonType="Link"  
  18.                                     ShowNextPageButton="true"  
  19.                                     ShowLastPageButton="false"  
  20.                                     ShowPreviousPageButton="false" />  
  21.                             </Fields>  
  22.                         </asp:DataPager>  
  23.                     </LayoutTemplate>  
  24.                     <ItemTemplate>  
  25.                         <asp:Label ID="Label2" runat="server" Text='<%#Eval("QuestionID")%>'></asp:Label><br />  
  26.                         <asp:Label ID="Label1" runat="server" Text='<%#Eval("QuestionText")%>'></asp:Label><br />  
  27.                         <li>  
  28.                             <asp:RadioButton ID="Radio1" Text='<%#Eval("Answer1") %>' GroupName="radiobtns" EnableViewState="true" runat="server" />  
  29.                         </li>  
  30.                         <li>  
  31.                             <asp:RadioButton ID="Radio2" runat="server" GroupName="radiobtns" EnableViewState="true" Text='<%#Eval("Answer2") %>' />  
  32.                         </li>  
  33.                         <li>  
  34.                             <asp:RadioButton ID="Radio3" runat="server" GroupName="radiobtns" EnableViewState="true" Text='<%#Eval("Answer3") %>'  />  
  35.                         </li>  
  36.                         <li>  
  37.                             <asp:RadioButton ID="Radio4" runat="server" GroupName="radiobtns" EnableViewState="true" Text='<%#Eval("Answer4") %>'   />  
  38.                         </li>  
  39.                         <br />  
  40.                     </ItemTemplate>  
  41.                 </asp:ListView>  
  42.             </div>  
  43.             <p>  
  44.                 <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />  
  45.             </p>  
  46.         </form>  
  47.     </body>  
  48.     </html> 
  1. namespace WebApplication2.WebForms  
  2.     {  
  3.         public partial class _1QuestionQuiz : System.Web.UI.Page  
  4.         {  
  5.             string userAns;  
  6.             int QuestionID;  
  7.   
  8.             protected void Page_Load(object sender, EventArgs e)  
  9.             {  
  10.                 if (!Page.IsPostBack)  
  11.                 {  
  12.                     BindListView();  
  13.   
  14.                     if (Session["QuizID"] != null)  
  15.                     {  
  16.                         int QuizID = Convert.ToInt32(Session["QuizID"]);  
  17.                     }  
  18.                 }  
  19.             }  
  20.   
  21.   
  22.   
  23.             protected void ListPager_PreRender(object sender, EventArgs e)  
  24.             {  
  25.                 if (Session["DTSource"] != null)  
  26.                 {  
  27.                     lvCustomers.DataSource = Session["DTSource"];  
  28.                     lvCustomers.DataBind();  
  29.                 }  
  30.   
  31.                 GetSelections();  
  32.             }  
  33.   
  34.   
  35.             private void BindListView()  
  36.             {  
  37.                 SqlConnection conn = new SqlConnection();  
  38.                 string connString = ConfigurationManager.ConnectionStrings["test1ConnectionString"].ConnectionString;  
  39.                 SqlCommand Cmd = new SqlCommand();  
  40.                 conn.ConnectionString = connString;  
  41.   
  42.                 conn.Open();  
  43.   
  44.                 if (lvCustomers != null)  
  45.                 {  
  46.                     foreach (ListViewItem item in lvCustomers.Items)  
  47.                     {  
  48.                         Label lbl = (Label)item.FindControl("Label2");  
  49.   
  50.                         if (lbl != null)  
  51.                         {  
  52.                             QuestionID = Convert.ToInt32(lbl.Text);  
  53.                             ViewState["qstion"] = QuestionID;  
  54.                         }  
  55.   
  56.                         RadioButton rd1 = (RadioButton)item.FindControl("Radio1");  
  57.                         RadioButton rd2 = (RadioButton)item.FindControl("Radio2");  
  58.                         RadioButton rd3 = (RadioButton)item.FindControl("Radio3");  
  59.                         RadioButton rd4 = (RadioButton)item.FindControl("Radio4");  
  60.   
  61.                         if (rd1.Checked)  
  62.                         {  
  63.                             userAns = rd1.Text;  
  64.                             ViewState["Radio1"] = rd1.Text;  
  65.                         }  
  66.   
  67.                         else if (rd2.Checked)  
  68.                         {  
  69.                             userAns = rd2.Text;  
  70.                             ViewState["Radio2"] = rd2.Text;  
  71.                         }  
  72.   
  73.                         else if (rd3.Checked)  
  74.                         {  
  75.                             userAns = rd3.Text;  
  76.                             ViewState["Radio3"] = rd3.Text;  
  77.                         }  
  78.   
  79.                         else if (rd4.Checked)  
  80.                         {  
  81.                             userAns = rd4.Text;  
  82.                             ViewState["Radio4"] = rd4.Text;  
  83.   
  84.                         }  
  85.   
  86.                         SqlCommand comm = new SqlCommand("InsertSelections", conn);  
  87.                         comm.CommandType = CommandType.StoredProcedure;  
  88.                         SqlDataAdapter adapter = new SqlDataAdapter(comm);  
  89.                         SqlParameter p1 = new SqlParameter("Answer", userAns);  
  90.                         SqlParameter p2 = new SqlParameter("QuestionID", QuestionID);  
  91.                         SqlParameter p3 = new SqlParameter("QuizID", (Session["QuizID"]));  
  92.                         comm.Parameters.Add(p1);  
  93.                         comm.Parameters.Add(p2);  
  94.                         comm.Parameters.Add(p3);  
  95.   
  96.                         comm.ExecuteNonQuery();  
  97.   
  98.   
  99.                     }  
  100.                 }  
  101.             }  
  102.   
  103.   
  104.   
  105.      private void GetSelections()  
  106.         {  
  107.             foreach (ListViewItem item in lvCustomers.Items)  
  108.             {  
  109.                 Label lbl = (Label)item.FindControl("Label2");  
  110.   
  111.                 if (lbl != null)  
  112.                 {  
  113.                     QuestionID = Convert.ToInt32(lbl.Text);  
  114.                 }  
  115.   
  116.                 RadioButton rd1 = (RadioButton)item.FindControl("Radio1");  
  117.                 RadioButton rd2 = (RadioButton)item.FindControl("Radio2");  
  118.                 RadioButton rd3 = (RadioButton)item.FindControl("Radio3");  
  119.                 RadioButton rd4 = (RadioButton)item.FindControl("Radio4");  
  120.   
  121.                         //Try radiobutton 1 as a tester  
  122.             if (rd1 != null)  
  123.             {  
  124.                 if (lbl != null && ViewState["Radio1"] != null)  
  125.                 {  
  126.                     rd1.Checked = true;  
  127.                 }  
  128.             }  
  129.              if (rd2 != null)  
  130.             {  
  131.                 if (lbl != null && ViewState["Radio2"] != null)  
  132.                 {  
  133.                     rd2.Checked = true;  
  134.   
  135.                 }  
  136.   
  137.             }  
  138.              if (rd3 != null)  
  139.             {  
  140.                 if (lbl != null && ViewState["Radio3"] != null)  
  141.                 {  
  142.                     rd3.Checked = true;  
  143.   
  144.                 }  
  145.   
  146.             }  
  147.              if (rd4 != null)  
  148.             {  
  149.                 if (lbl != null && ViewState["Radio4"] != null)  
  150.                 {  
  151.                     rd4.Checked = true;  
  152.                     break;  
  153.                 }  
  154.   
  155.                }       
  156.             }  
  157.         }  
  158.   
  159.             protected void Button1_Click(object sender, EventArgs e)  
  160.             {  
  161.                 Response.Redirect("Score.aspx"false);  
  162.             }  
  163.   
  164.   
  165.             protected void OnPagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)  
  166.             {  
  167.                 (lvCustomers.FindControl("DataPager1"as DataPager).SetPageProperties(e.StartRowIndex, e.MaximumRows, false);  
  168.                 BindListView();  
  169.   
  170.             }  
  171.     }  
  172.     } 

Answers (3)