In this article I will provide solution to the problem where value of the TextBox placed in GridView is lost after PostBack in ASP.Net with C# and VB.Net.
When PostBack happens i.e. when Button is clicked or any other control that does PostBack inside GridView, the GridView is again populated with data and hence the modified values in TextBox are lost.
 
 
Problem
When PostBack happens i.e. when Button is clicked or any other control that does PostBack inside GridView, modified values in TextBox are lost.
[SOLVED] GridView TextBox value lost on PostBack in ASP.Net
 
 
Cause
When PostBack happens i.e. when Button is clicked or any other control that does PostBack inside GridView, the GridView is again populated with data and hence the modified values in TextBox are lost.
 
 
Solution
Solution to this problem is to wrap the GridView binding code inside the Not IsPostBack condition as shown in the example below.
 
HTML Markup
The following HTML Markup consists of an ASP.Net GridView with one BoundField column and two TemplateField columns consisting of a TextBox and Button.
<asp:GridView ID="GridView1" CssClass="Grid" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="Item" HeaderText="Item" />
        <asp:TemplateField HeaderText="Quantity">
            <ItemTemplate>
                <asp:TextBox ID="txtQuantity" runat="server" Text='<%# Eval("Quantity") %>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Button Text="Update" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
 
 
Namespaces
You will need to import the following namespace.
C#
using System.Data;
 
VB.Net
Imports System.Data
 
 
Binding the ASP.Net GridView control
Inside the Page Load event, the GridView is populated using a DataTable with some dummy data.
Note: For more details about using dynamic DataTable, please refer my article Create DataTable dynamically and bind to GridView in ASP.Net.
 
You will notice that the GridView population code has been placed inside the Not IsPostBack condition which will populate the GridView only when the Page is loaded first time and not when PostBack happens.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[2] { new DataColumn("Item"), new DataColumn("Quantity") });
        dt.Rows.Add("Shirt", 145);
        dt.Rows.Add("Jeans", 0);
        dt.Rows.Add("Trousers", 190);
        dt.Rows.Add("Tie", 30);
        dt.Rows.Add("Cap", 0);
        dt.Rows.Add("Hat", 90);
        dt.Rows.Add("Scarf", 290);
        dt.Rows.Add("Belt", 150);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
}
 
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim dt As New DataTable()
        dt.Columns.AddRange(New DataColumn(1) {New DataColumn("Item"), New DataColumn("Quantity")})
        dt.Rows.Add("Shirt", 145)
        dt.Rows.Add("Jeans", 0)
        dt.Rows.Add("Trousers", 190)
        dt.Rows.Add("Tie", 30)
        dt.Rows.Add("Cap", 0)
        dt.Rows.Add("Hat", 90)
        dt.Rows.Add("Scarf", 290)
        dt.Rows.Add("Belt", 150)
        GridView1.DataSource = dt
        GridView1.DataBind()
    End If
End Sub
 
 
Screenshot
[SOLVED] GridView TextBox value lost on PostBack in ASP.Net
 
 
Demo
 
 
Downloads