Export (0) Print
Expand All
Expand Minimize

GridViewUpdateEventHandler Delegate

Represents the method that handles the RowUpdating event of a GridView control.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)

public delegate void GridViewUpdateEventHandler(
	Object sender,
	GridViewUpdateEventArgs e
)

Parameters

sender
Type: System.Object

The source of the event.

e
Type: System.Web.UI.WebControls.GridViewUpdateEventArgs

A GridViewUpdateEventArgs object that contains the event data.

The RowUpdating event is raised when a row's Update button is clicked, but before the GridView control updates the row. This allows you to provide an event-handling method that performs a custom routine, such as canceling the update operation, whenever this event occurs.

When you create a GridViewUpdateEventHandler delegate, you identify the method that will handle the event. To associate the event with your event handler, add an instance of the delegate to the event. The event handler is called whenever the event occurs, unless you remove the delegate. For more information about event-handler delegates, see Events and Delegates.

The following example demonstrates how to programmatically add a GridViewUpdateEventHandler delegate to the RowUpdating event of a GridView control.


<%@ Page language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

    void Page_Load(Object sender, EventArgs e)
    {

        // Create a new GridView object.
        GridView authorGridView = new GridView();

        // Set the GridView object's properties.
        authorGridView.ID = "AuthorGridView";
        authorGridView.DataSourceID = "AuthorsSqlDataSource"; 
        authorGridView.AutoGenerateColumns = true;
        authorGridView.AutoGenerateEditButton = true;
        authorGridView.DataKeyNames = new String[] {"au_id"};

        // Programmatically register the event-handling method.
        authorGridView.RowUpdating += new GridViewUpdateEventHandler(this.AuthorsGridView_RowUpdating);

        // Add the GridView object to the Controls collection
        // of the PlaceHolder control.
        GridViewPlaceHolder.Controls.Add(authorGridView);

    }

    void AuthorsGridView_RowUpdating(Object sender, GridViewUpdateEventArgs e)
    {

        // HTML-encode all user-provided values before updating
        // the data source.
        foreach (DictionaryEntry entry in e.NewValues)
        {

            e.NewValues[entry.Key] = Server.HtmlEncode(entry.Value.ToString());

        }

    }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
    <title>GridViewUpdateEventHandler Example</title>
</head>
<body>
        <form id="form1" runat="server">

            <h3>GridViewUpdateEventHandler Example</h3>

            <asp:label id="Message"
                forecolor="Red"
                runat="server"/>

            <br/>

            <asp:placeholder id="GridViewPlaceHolder"
                runat="Server"/>

            <!-- This example uses Microsoft SQL Server and connects -->
            <!-- to the Pubs sample database.                        -->
            <asp:sqldatasource id="AuthorsSqlDataSource"  
                selectcommand="SELECT [au_id], [au_lname], [au_fname], [address], [city], [state], [zip] FROM [authors]"
                updatecommand="UPDATE authors SET au_lname=@au_lname, au_fname=@au_fname, address=@address, city=@city, state=@state, zip=@zip WHERE (authors.au_id = @au_id)"
                connectionstring="server=localhost;database=pubs;integrated security=SSPI"
                runat="server">
            </asp:sqldatasource>

        </form>
    </body>
</html>

The following example demonstrates how to declaratively add a GridViewUpdateEventHandler delegate to the RowUpdating event of a GridView control.


<%@ Page language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  void CustomersGridView_RowUpdating(Object sender, GridViewUpdateEventArgs e)
  {

    // Iterate through the NewValues collection and HTML encode all 
    // user-provided values before updating the data source.
    foreach (DictionaryEntry entry in e.NewValues)
    {

      e.NewValues[entry.Key] = Server.HtmlEncode(entry.Value.ToString());

    }

  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>GridView RowUpdating Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <h3>GridView RowUpdating Example</h3>

      <!-- The GridView control automatically sets the columns     -->
      <!-- specified in the datakeynames property as read-only.    -->
      <!-- No input controls are rendered for these columns in     -->
      <!-- edit mode.                                              -->
      <asp:gridview id="CustomersGridView" 
        datasourceid="CustomersSqlDataSource" 
        autogeneratecolumns="true"
        autogenerateeditbutton="true"
        allowpaging="true" 
        datakeynames="CustomerID"
        onrowupdating="CustomersGridView_RowUpdating"  
        runat="server">
      </asp:gridview>

      <!-- This example uses Microsoft SQL Server and connects  -->
      <!-- to the Northwind sample database. Use an ASP.NET     -->
      <!-- expression to retrieve the connection string value   -->
      <!-- from the Web.config file.                            -->
      <asp:sqldatasource id="CustomersSqlDataSource"  
        selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
        updatecommand="Update Customers SET CompanyName=@CompanyName, Address=@Address, City=@City, PostalCode=@PostalCode, Country=@Country WHERE (CustomerID = @CustomerID)"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
        runat="server">
      </asp:sqldatasource>

    </form>
  </body>
</html>

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft