Rajiv Prathapan
Rajiv Prathapan

Reputation: 75

Edit Item Template TextBox value in a Gridview Confusion

I have a gridview with Edit commandfield. In one of the templatefield of the gridview,I have a textbox inside edit item template. In the RowUpdating event, I try to get the value of the textbox.

TextBox text = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtfname");

GridViewRow row =GridView1.Rows[e.RowIndex] as  GridViewRow;

TextBox tFirstName = row.FindControl("txtfname") as TextBox;

TextBox textFName = (TextBox)row.Cells[0].Controls[3];

I tried all three methods.When I put

if(!IsPostBack)Bind_grid();

I get the textbox.text value as empty.If I remove that condition I get the value which was bound on the control using Bind function in the markup page.I never got the value I am editing in the control.

Where am I going wrong? Solution?

Upvotes: 0

Views: 2296

Answers (1)

mtzaldo
mtzaldo

Reputation: 105

I'm guessing that your grid has the paging enabled. So your are getting the index of the row that is displaying in the grid (not the data source).

For example: You're in the 2 page of your grid, and you have 10 items per page. You click on edit on the 3rd row. The e.RowIndex will be 2.

If you want to map it to you datasource, this should be... ((page - 1) * items) + index;

((2-1) * 10) + 2 = 12

So in your datasource you should get the item that is on the position 12.

Upvotes: 0

Related Questions