In the previous article we discussed about ASP.Net GridView events for Edit, update and cancel commands. RowEditing event set the GridView mode to edit view. RowCancelingEdit event sets the GridView mode back read only view. bindGridView() function has been created in the sample C# code to bind the data with GridView Control. This data binding function is used in both RowEditing and RowCancelingEdit events after assigning the value EditIndex property of GridView control. Now is this tutorial we will learn how to use the FindControl function of GridView Control to search the TextBox server control placed inside the TemplateField. FindControl function accepts the string value as ID of the server control that you want to find inside the GridView TemplateField.
e.g.:
GridView1.Rows[e.RowIndex].Cells[0].FindControl("txtCategoryName")
Further to access the properties associated with control accessed by using FindControl you have to type cast the control to its WebControls providing class.
(TextBox)GridView1.Rows[e.RowIndex].Cells[0].FindControl("txtCategoryName")
Above C# code will convert associate the control with Textbox WebControls class. Control Type casting will allow you to access the Text property of the textbox control searched using FindControl function that you can pass to the SQL Update query as a new value edited by the user. RowUpdating Update command event of ASP.Net GridView Data Control can be used to perform data update action.
// SQL Update Command SqlCommand mySqlUpdate = new SqlCommand("update categories set categoryName = @categoryName, description = @description where categoryId = @categoryId", mySQLconnection);
mySqlUpdate.CommandType = CommandType.Text;
// Textbox objects TextBox txtCategoryName = new TextBox();
TextBox txtDescription = new TextBox();
// FindControl function used to get the reference to textbox controls // placed inside the ItemTemplate of GridView control txtCategoryName = (TextBox)GridView1.Rows[ e.RowIndex ].Cells[0].FindControl( "txtCategoryName" );
txtDescription = (TextBox)GridView1.Rows[ e.RowIndex ].Cells[0].FindControl( "txtDescription" );
// parameters passed to the SQL Update Command mySqlUpdate.Parameters.Add("@categoryName", SqlDbType.VarChar).Value = txtCategoryName.Text; mySqlUpdate.Parameters.Add("@description", SqlDbType.VarChar).Value = txtDescription.Text; mySqlUpdate.Parameters.Add("@categoryId", SqlDbType.Int).Value = Convert.ToInt32( GridView1.DataKeys[ e.RowIndex ].Values[0].ToString() );
mySqlUpdate.ExecuteNonQuery();
Download the Compete C# Source Code here:
gridview-edit-update.zip (3.53 kb)
Be the first to rate this post
Tags: asp.net 2.0, c#, asp.net gridview, asp.net gridview edit, asp.net gridview command, asp.net gridview update, asp.net gridview cancel, asp.net gridview commandfield, asp.net gridview rowediting, asp.net gridview rowcancelingedit, asp.net gridview rowupdating, asp.net gridview edit mode, asp.net gridview itemtemplate, asp.net gridview edititemtemplate, c# gridview, c# code
10/11/2008 3:34:32 AM