title: PostBack in ASP.NET causes all TextBox and DropList in GridView to be cleared
body: <p>I have a grid view in asp.net as given below.</p>
  
  <pre><code>&lt;asp:GridView ID="grid_flats_allflatslist" OnRowDeleting="grid_flats_allflatslist_RowDeleting" runat="server" Width="95%" CssClass="gridview" ShowFooter="true" AllowPaging="true" PageSize="8" BackColor="Black" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="1" CellSpacing="1" frame="below" DataKeyNames="BlockID" AutoGenerateColumns="false"&gt;
      &lt;Columns&gt;
          &lt;asp:BoundField DataField="BlockID" HeaderText="Block ID" Visible="false" /&gt;
          &lt;asp:BoundField DataField="BlockName" HeaderText="Block Name" /&gt;
          &lt;asp:TemplateField HeaderText="Flat Number"&gt;
              &lt;ItemTemplate&gt;
                  &lt;asp:TextBox ID="text_flats_listflatnumber" CssClass="textbox" onkeypress="return numeric(this);" MaxLength="5" runat="server"&gt;&lt;/asp:TextBox&gt;
              &lt;/ItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
          &lt;asp:TemplateField HeaderText="Flat Number"&gt;
              &lt;ItemTemplate&gt;
                  &lt;asp:DropDownList ID="list_flats_listflattype" CssClass="droplist" runat="server"&gt;
                      &lt;asp:ListItem Text="Bachelor" Value="Bachelor"&gt;&lt;/asp:ListItem&gt;
                      &lt;asp:ListItem Text="Family" Value="Family"&gt;&lt;/asp:ListItem&gt;
                  &lt;/asp:DropDownList&gt;
              &lt;/ItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
          &lt;asp:TemplateField HeaderText="Max Vacancy"&gt;
              &lt;ItemTemplate&gt;
                  &lt;asp:TextBox ID="text_flats_listmaxvacancy" onkeypress="return numeric(this);" MaxLength="2" CssClass="textbox" runat="server"&gt;&lt;/asp:TextBox&gt;
              &lt;/ItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
          &lt;asp:CommandField ShowDeleteButton="true" DeleteText="Delete" /&gt;
      &lt;/Columns&gt;
      &lt;FooterStyle BackColor="#9e4900" ForeColor="#fff1e5" Font-Bold="false" Font-Underline="false" HorizontalAlign="Center" /&gt;
      &lt;HeaderStyle BackColor="#9e4900" ForeColor="#fff1e5" HorizontalAlign="Center" /&gt;
      &lt;PagerStyle BackColor="#ff8080" ForeColor="#660000" HorizontalAlign="Right" /&gt;
      &lt;RowStyle BackColor="#ffe7d6" ForeColor="#660000" HorizontalAlign="Center" /&gt;
      &lt;AlternatingRowStyle BackColor="#ffd6ba" ForeColor="#660000" HorizontalAlign="Center" /&gt;
      &lt;SelectedRowStyle BackColor="#fff9f4" HorizontalAlign="Center" ForeColor="Black" /&gt;
  &lt;/asp:GridView&gt;
  
  &lt;asp:Button ID="button_flats_flatplus" CssClass="button" Height="30px" Width="25%" runat="server" Text="Add Flat" OnClick="button_flats_flatplus_Click" /&gt;
  </code></pre>
  
  <p>Initially, this is empty and when I click on the <code>button_flats_flatsplus</code> button then a row will be added to the grid view with the field as above. It will also have 2 text boxes and a drop-down list. The button click event is as below.</p>
  
  <pre><code>protected void button_flats_flatplus_Click(object sender, EventArgs e)
  {
      DataTable dt;
      DataRow dr = null;
      if (ViewState["CurrentTable"] == null)
      {
          dt = new DataTable();
          dt.Columns.Add(new DataColumn("BlockID", typeof(string)));
          dt.Columns.Add(new DataColumn("BlockName", typeof(string)));
          dt.Columns.Add(new DataColumn("Column1", typeof(string)));
          dt.Columns.Add(new DataColumn("Column2", typeof(string)));
          dt.Columns.Add(new DataColumn("Column3", typeof(string)));
          dr = dt.NewRow();
          dr["BlockID"] = list_flats_blocklist.SelectedValue; // I get this value from a different list
          dr["BlockName"] = list_flats_blocklist.SelectedItem.Text; // I get this value from a different list
          dr["Column1"] = string.Empty;
          dr["Column2"] = string.Empty;
          dr["Column3"] = string.Empty;
          dt.Rows.Add(dr);
          ViewState["CurrentTable"] = dt;
          grid_flats_allflatslist.DataSource = dt;
          grid_flats_allflatslist.DataBind();
      }
      else
      {
          dt = (DataTable) ViewState["CurrentTable"];
          dr = dt.NewRow();
          dr["BlockID"] = list_flats_blocklist.SelectedValue; // I get this value from a different list
          dr["BlockName"] = list_flats_blocklist.SelectedItem.Text; // I get this value from a different list
          dr["Column1"] = string.Empty;
          dr["Column2"] = string.Empty;
          dr["Column3"] = string.Empty;
          dt.Rows.Add(dr);
          ViewState["CurrentTable"] = dt;
          grid_flats_allflatslist.DataSource = dt;
          grid_flats_allflatslist.DataBind();
      }
      SetPreviousData();
  }
  </code></pre>
  
  <p>After I add many rows the grid view will look as below,</p>
  
  <p><a href="https://i.stack.imgur.com/LjZSn.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LjZSn.png" alt="My GridView"></a></p>
  
  <p>If I press delete then that particular row it is getting deleted successfully but the values in all the other rows are cleared. My row deleting code is as below with the <code>SetPreviousData</code> function but it won't work.</p>
  
  <pre><code>protected void grid_flats_allflatslist_RowDeleting(object sender, GridViewDeleteEventArgs e)
  {
      int index = Convert.ToInt32(e.RowIndex);
      DataTable dt = ViewState["CurrentTable"] as DataTable;
      dt.Rows[index].Delete();
      ViewState["CurrentTable"] = dt;
      grid_flats_allflatslist.DataSource = dt;
      grid_flats_allflatslist.DataBind();
      SetPreviousData();
  }
  </code></pre>
  
  <p>Everytime I add a new row to the grid view or delete a row from the grid view, post back happens and whatever values I have entered into the text boxes and drop-down list of other rows gets cleared. I tried to handle this with <code>!Page.IsPostBack</code> but still not working.</p>
  
  <p>Then I tried the following code and added it to all the places that had a postback or data bind to the grid view.</p>
  
  <pre><code>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)grid_flats_allflatslist.Rows[rowIndex].Cells[3].FindControl("text_flats_listflatnumber");
                  DropDownList box2 = (DropDownList)grid_flats_allflatslist.Rows[rowIndex].Cells[4].FindControl("list_flats_listflattype");
                  TextBox box3 = (TextBox)grid_flats_allflatslist.Rows[rowIndex].Cells[5].FindControl("text_flats_listmaxvacancy");
                  box1.Text = dt.Rows[i]["Column1"].ToString();
                  box2.SelectedValue = dt.Rows[i]["Column2"].ToString();
                  box3.Text = dt.Rows[i]["Column3"].ToString();
                  rowIndex++;
              }
          }
      }
  }
  </code></pre>
  
  <p>If I try to add <code>!Page.IsPostBack</code> in the grid vide bind then whatever changes I make to grid view, add or delete rows is not getting reflected. How can I avoid clearing of the text boxes in grid view whenever there is a post back or data bind on the page? I still want to delete and add rows to grid view by still keeping the already entered values in the fields.</p>