0

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?

Rajiv Prathapan
  • 75
  • 1
  • 10

1 Answers1

0

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.

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
mtzaldo
  • 105
  • 4
  • I dont want it to map it to the datasource . I want the value of the textbox in the current page.Yes I have enabled paging – Rajiv Prathapan Jul 08 '13 at 20:28
  • what is the signature of the event? the one that uses GridViewEditEventArgs, GridViewRowEventArgs, or GridViewUpdatedEventArgs? – mtzaldo Jul 08 '13 at 20:31
  • I have mentioned in the question it is Rowupdating .. The signature has a GridViewUpdateEventArgs – Rajiv Prathapan Jul 08 '13 at 20:35
  • The only thing I think it might happening it's that somehow you're not binding during the RowEditing event, neither saving the edit index. This might help http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowupdating.aspx ... – mtzaldo Jul 08 '13 at 20:50
  • So, leave if(!IsPostBack)Bind_grid(); and add the RowEditing event. – mtzaldo Jul 08 '13 at 20:53
  • is the OnRowEditing assigned in the grid? If yes, that all I can do :( – mtzaldo Jul 08 '13 at 21:10