title: c# how do Ikeep my radio button selection after postback
body: <p>This is how my gridview looks like : </p>
  
  <p>Basically , I select "you" as the correct answer .</p>
  
  <p><img src="https://i.stack.imgur.com/wuIXO.jpg" alt="enter image description here"></p>
  
  <p>But after I click the "+" button , the selection of radio button got removed .
  <img src="https://i.stack.imgur.com/YOYQQ.jpg" alt="enter image description here"></p>
  
  <p>How do I keep my selection of radio button for every row of gridview?</p>
  
  <p>This is my code: </p>
  
  <pre><code> protected void Page_Load(object sender, EventArgs e)
   {
  
       if (!Page.IsPostBack)
       {
           SetInitialRow();
           dropActivity(); // ignore this , this is for drop down list
           dropTask();  // ignore this , this is for drop down list
  
       }
   }
  
  private void SetInitialRow()
  {
      DataTable dt = new DataTable();
      DataRow dr = null;
      dt.Columns.Add(new DataColumn("Question", typeof(string)));
      dt.Columns.Add(new DataColumn("Hints No.1", typeof(string)));
      dt.Columns.Add(new DataColumn("Hints No.2", typeof(string)));
  
      dr = dt.NewRow();
      dr["Question"] = string.Empty;
      dr["Hints No.1"] = string.Empty;
      dr["Hints No.2"] = string.Empty;
  
      dt.Rows.Add(dr);
  
      //Store the DataTable in ViewState
      ViewState["CurrentTable"] = dt;
  
      GridView1.DataSource = dt;
      GridView1.DataBind();
  }
  
  private void AddNewRowToGrid()
  {
      int rowIndex = 0;
  
      if (ViewState["CurrentTable"] != null)
      {
          DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
          DataRow drCurrentRow = null;
          if (dtCurrentTable.Rows.Count &gt; 0)
          {
              for (int i = 1; i &lt;= dtCurrentTable.Rows.Count; i++)
              {
                  //extract the TextBox values
                  TextBox box1 = (TextBox)GridView1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
                  TextBox box2 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
                  TextBox box3 = (TextBox)GridView1.Rows[rowIndex].Cells[3].FindControl("TextBox3");
  
                  drCurrentRow = dtCurrentTable.NewRow();
  
                  dtCurrentTable.Rows[i - 1]["Question"] = box1.Text;
                  dtCurrentTable.Rows[i - 1]["Hints No.1"] = box2.Text;
                  dtCurrentTable.Rows[i - 1]["Hints No.2"] = box3.Text;
  
                  rowIndex++;
              }
              dtCurrentTable.Rows.Add(drCurrentRow);
              ViewState["CurrentTable"] = dtCurrentTable;
  
              GridView1.DataSource = dtCurrentTable;
              GridView1.DataBind();
          }
      }
      else
      {
          Response.Write("ViewState is null");
      }
  
      //  Set Previous Data on Postbacks
      SetPreviousData();
  }
  
  private void SetPreviousData()
  {
      int rowIndex = 0;
      if (ViewState["CurrentTable"] != null)
      {
          DataTable dt = (DataTable)ViewState["CurrentTable"];
          if (dt.Rows.Count &gt; 0)
          {
              for (int i = 0; i &lt; dt.Rows.Count; i++)
              {
                  TextBox box1 = (TextBox)GridView1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
                  TextBox box2 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
                  TextBox box3 = (TextBox)GridView1.Rows[rowIndex].Cells[3].FindControl("TextBox3");
  
                  RadioButton radiobtn1 = (RadioButton)GridView1.Rows[i].Cells[2].FindControl("RadioButton1");
                  RadioButton radiobtn2 = (RadioButton)GridView1.Rows[i].Cells[3].FindControl("RadioButton2");
  
                  //Setting previous text to the respective textboxes based on columns.
                  box1.Text = dt.Rows[i]["Question"].ToString();
                  box2.Text = dt.Rows[i]["Hints No.1"].ToString();
                  box3.Text = dt.Rows[i]["Hints No.2"].ToString();
  
                  if (radiobtn1.Checked == true)
                  {
                      ViewState["RadioButtonStatus"] = radiobtn1.Checked;
                  }
  
                  if (radiobtn2.Checked == true)
                  {
                      ViewState["RadioButtonStatus"] = radiobtn2.Checked;
                  }
  
                  Session["Question1"] = box1.Text;
                  rowIndex++;
              }
          }
      }
  }
  </code></pre>
  
  <p>Basically I need to keep the selection of radio button for every row I have created.</p>