HMR
HMR

Reputation: 39270

.net GridView OnRowUpdating not getting the current values of the TextBox

According to this documentation I've added a gridview and implemented onupdate. http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowediting.aspx

I open the page and click edit that will set the editinex and rebind:

protected void rowEdit(object sender, GridViewEditEventArgs e)
{
    EditProducts.EditIndex = e.NewEditIndex;
    EditProducts.DataBind();
}

In the 3rd column I change the value (let's say it was "hello" and I change it to "goodbye"). Then click "Update". (first column is the button column for edit and delete or update and cancel second shows the id).

The onrowupdate method is called and I try to get the value of column 3 according the example code from msdn (the second data column):

protected void RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    GridViewRow row = EditProducts.Rows[e.RowIndex];
    String previousValue= ((TextBox)(row.Cells[2].Controls[0])).Text;
    String actualValue = Request.Params["EditProducts$ctl02$ctl03"];

The string previousValue is not "goodbye"; it is still "hello"

In Firefox I can see "goodbye" is submitted but the value of the TextBox control in rowUpdating is "hello". Must be incorrectly taken from the viewstate.

So I added the actualValue getting request.params and that is set to "goodbye".

I have the following questions:

Is the code provided on msdn wrong?

Isn't the TextBox control supposed to represent the Text property of the value that the input contains when submitted?

Upvotes: 2

Views: 1095

Answers (1)

rahularyansharma
rahularyansharma

Reputation: 10755

You should bind your gridview on page load event in

 if (!IsPostBack)
        {
        }

Binding the control overwrites the values and You did bind it in page load without checking for post back.

Upvotes: 3

Related Questions