Hi,

I have the following code:

<asp:GridView ID="MyGridView" runat="server" AllowPaging="true" PageSize="30" AutoGenerateColumns="False"
        AllowSorting="true">
        <Columns>
            <asp:TemplateField HeaderText="Industry ID" ItemStyle-Width="150px" ItemStyle-HorizontalAlign="Center"
                SortExpression="ID">
                <ItemTemplate>
                    <asp:Label ID="IDLabel" runat="server" Text='<%#Eval("ID") %>' />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:Label ID=" IDLabel " runat="server" Text='<%#Eval("ID") %>' />
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:Label ID="EnterLabel" runat="server" Text="Please, enter the new name: " />
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Name" ItemStyle-HorizontalAlign="Center" SortExpression="Name">
                <ItemTemplate>
                    <asp:Label ID="NameLabel" runat="server" Text='<%#Eval("Name") %>' />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="EditNameTextBox" runat="server" Text='<%#Bind("Name") %>' />
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="NewNameTextBox" runat="server" />
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Action" HeaderStyle-HorizontalAlign="Center">
                <ItemTemplate>
                    <asp:LinkButton ID="EditLinkButton" runat="server" Text="Edit" CommandName="Edit" />
                    &nbsp;
                    <asp:LinkButton ID="DeleteLinkButton" runat="server" Text="Delete" OnClientClick="javascript:if(!confirm('Are you sure you want to delete the selected use?'))return false;"
                        OnClick="DeleteLinkButton_Click" />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:LinkButton ID="UpdateLinkButton" runat="server" Text="Update" CommandName="Update" />
                    &nbsp;
                    <asp:LinkButton ID="CancelLinkButton" runat="server" Text="Cancel" CommandName="Cancel" />
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:LinkButton ID="AddLinkButton" Text="Add" OnClick="AddLinkButton_Click" runat="server" />
                </FooterTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

In the rowUpsating event, in code behind, I have the following:

Protected Sub myGridView_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles myGridView.RowUpdating
        Dim row As GridViewRow = myGridView.Rows(e.RowIndex)
        Dim EditNameTextBox As TextBox = row.FindControl("EditNameTextBox")
        Dim name As String = EditNameTextBox.Text
...
End Sub

Suppose Name is initially ASP.NET and I need to change to HTML. The problem I'm having is: when I do: EditNameTextBox.Text instead of getting the new name I just type (in this case, HTML), it's getting the old one (ASP.NET). I cannot see what is wrong. Does anyone have suggestions?

Thanks,

Ana

Dani AI

Generated

Good catch, The behavior you saw (the edited TextBox still returning the original bound value) is exactly what happens when the GridView is rebound on every postback: DataBind rebuilds the rows and restores bound values before your update code runs, so the posted user input is lost.

A simple, reliable pattern is to only bind on first load and then rebind after a successful update. Example for Page_Load:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not IsPostBack Then
        BindGrid()
    End If
End Sub

In the RowUpdating handler prefer reading the new values supplied by the GridView (when using two‑way Bind) or locate the TextBox before you rebind. Also use DataKeys for IDs. Example pattern:

Protected Sub MyGridView_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
    Dim newName As String = e.NewValues("Name").ToString()
    Dim id As Integer = CInt(MyGridView.DataKeys(e.RowIndex).Value)
    ' perform update to DB...
    MyGridView.EditIndex = -1
    BindGrid()
End Sub

Quick checklist/troubleshooting:

  • Confirm EditItemTemplate uses Bind (not Eval) if you rely on e.NewValues.
  • Make sure you are not calling BindGrid() earlier in the lifecycle on postback.
  • Use DataKeyNames to retrieve IDs instead of hidden labels.
  • If using AJAX/UpdatePanel, the same rebinding rules apply.
  • Put breakpoints to see the order: bind -> load posted values -> RowUpdating.

Following that pattern avoids losing the user's input and keeps update logic simple and reliable.

Just found the answer. I was Binding my GridView in the Page Load and this was causing the problem. Whenever the page was reloaded, the value for the text box was reset to its original value.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.