ASP.Net GridView Update Command

by top54u.com 14 Jul, 2008

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.

 

e.g.:

 

(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.

 

C# Code for ASP.Net GridView Update Command Event

 

// 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)

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

10/11/2008 3:34:32 AM




related videos.....
recent posts.....
top54u ezines.....