Homepage image
Sample of the Day RSS Feed

Sample Downloads: http://code.msdn.microsoft.com/GridViewRowEventSupport-48597096

Today’s sample demonstrates how to add GridView Row Event Support on Server side or Client side. In this sample, the server side registers script then triggers the RowEditing event of the GridView and the Client side alerts the InnerHtml text of the selected “tr”.

The sample was written by our star sample writer: Seiya Su.

imageYou can find more code samples that demonstrate the most typical programming scenarios by using Microsoft All-In-One Code Framework Sample Browser or Sample Browser Visual Studio extension. They give you the flexibility to search samples, download samples on demand, manage the downloaded samples in a centralized place, and automatically be notified about sample updates. If it is the first time that you hear about Microsoft All-In-One Code Framework, please watch the introduction video on Microsoft Showcase, or read the introduction on our homepage http://1code.codeplex.com/.

 

Introduction

The sample demonstrates how to add GridView Row Event Support on Server side or Client side. In this sample, the server side registers script then triggers the RowEditing event of the GridView and the Client side alerts the InnerHtml text of the selected “tr”

 

Running the Sample

Please follow these demonstration steps below.

Step 1: Open the CSASPNETGridViewRowEventSupport.sln.

Step 2: Right-click the ClientSideSupport.aspx page then select "View in Browser". Click the Data row you can see an alert to show the InnerHtml of the selected “tr” tag.

Step 3: Right-click the ServerSideSupport.aspx page then select "View in Browser". Click the Data row you can see enter EditTemplate of the GridView.

Using the Code

Step1. Create a C# "ASP.NET Web Application" in Visual Studio 2010/Visual Web Developer 2010. Name it as "CSASPNETGridViewRowEventSupport".

Step2. Add two pages then rename to ClientSideSupport.aspx and ServerSideSupport.aspx. Add a GridView to the both pages. Then rename them “gvCustomer”.

Step3. Under the App_Data folder add a database name it “gvData.mdf” then create a table name it “CustomerInfo”.

Step4. Add a SqlDataSource for each page, it uses the database to obtain the load data, and then bound to the GridView of the current page.

Step5. In the ClientSideSupport page, add OnRowEditing and OnRowCreated event for the GridView.

<asp:GridView ID="gvCustomer" runat="server" OnRowCreated="gvCustomer_RowCreated" 
         AutoGenerateColumns="False" OnRowEditing="gvCustomer_RowEditing" DataSourceID="SqlDataSource1"> 

 </asp:GridView> 

The code-behind is as follows:

protected void gvCustomer_RowCreated(object sender, GridViewRowEventArgs e) 
       { 
           if (e.Row.RowType == DataControlRowType.DataRow) 
           { 
               //Returns a string that can be used in a client event to cause postback to the server 
               e.Row.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference((Control)sender, "Edit$" + e.Row.RowIndex.ToString())); 
           } 
       } 
 
 
       protected void gvCustomer_RowEditing(object sender, GridViewEditEventArgs e) 
       { 
           //Set the edit index. 
           gvCustomer.EditIndex = e.NewEditIndex; 
       } 

[Note] In order to call GetPostBackEventReference method we must add the following code:

/// <summary> 
       /// Register the postback or callback data for validation. 
       /// </summary> 
       /// <param name="writer"></param> 
       protected override void Render(HtmlTextWriter writer) 
       { 
           for (int i = 0; i < gvCustomer.Rows.Count; i++) 
               ClientScript.RegisterForEventValidation(gvCustomer.UniqueID, "Edit$" + i); 
           base.Render(writer); 
       } 

Step6. Build the application and you can debug it.

 

More Information

ClientScriptManager.RegisterForEventValidation Method (String, String)

ClientScriptManager.GetPostBackEventReference Method