Surender Singh
Surender Singh

Reputation: 21

Issue GridView in ASP.NET

I just want to know the difference between last two lines

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
 if (GridView1.Rows[e.RowIndex].RowType == DataControlRowType.DataRow)
  {
   GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];

   string lstnme = ((TextBox)row.Cells[2].FindControl("txtLstNme")).Text;

and

   string lstnme=((TextBox)row.Cells[2].Controls[0]).Text;
} 

Upvotes: 2

Views: 82

Answers (2)

DeshDeep Singh
DeshDeep Singh

Reputation: 1843

  1. string lstnme = ((TextBox)row.Cells[2].FindControl("txtLstNme")).Text; basically means it is finding a control with Name as txtLstNme in your cells and then returning the text inside that textbox control.

  2. string lstnme=((TextBox)row.Cells[2].Controls[0]).Text; it means lstnme will hold the text of control at position 0 in your cells.

the main diffenrece is 1st one is looking for a [articular text box in list collection of control, but the second one gets the text of the control at location 0.

Upvotes: 1

Master
Master

Reputation: 11

There is no difference, but I would suggest to check if the control is null or not before you assign the Text value

string lstnme = string.Empty;
var control = ((TextBox)row.Cells[2].FindControl("txtLstNme"));
if ( control != null )
{
     lstnme = control.Text
}

Upvotes: 1

Related Questions