<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridViewExample12b.aspx.cs" Inherits="GridViews.GridViewExample12b" %>

 

<!DOCTYPE html>

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

    <title>GridView Example 12b - Version 2</title>

</head>

<body bgcolor="#ffff99">

 

    <h3>This GridView example demonstrates handling button click events for a row in a

        GridView. </h3>

 

    <ul>

        <li>Hidden Column: BoundField column that is bound to the database field <b>ProductNumber</b>.

            The value is stored in the DataKeys collection since hidden columns aren't rendered into HTML.

        </li>

        <li>Column 1: BoundField column that is bound to the database field <b>Description</b>.</li>

        <li>Column 2: BoundField column that is bound to the database field <b>Price</b>.</li>

        <li>Column 3: ButtonField column with Add to Card button. CommandName Property is

            set to &quot;AddToCart&quot;, which is used to determine the button that was clicked and

            execute the appropriate code in the Codebehind.</li>

        <li>Column 4: ButtonField column Product Reviews button. CommandName Property is set to "ProductReviews"

            which is used to determine the button that was clicked and execute the

            appropriate code in the Codebehind.</li>

    </ul>

 

    <h3>This example also demonstrates the technique of using DataKeys to store and retrieve hidden column values.</h3>

 

 

The CommandName Property is used to determine which button was clicked on the server-side CodeBehind.

Determining the button that was clicked for a GridView Row is important because each button has code specific to that button’s functionality.

 
    <form id="form1" runat="server">

 

        <asp:Label ID="lblDisplay" runat="server" Font-Bold="True"

            ForeColor="#CC0000" />

        <br /><br />

 

        <asp:GridView ID="gvProducts" runat="server" AutoGenerateColumns="False" OnRowCommand="gvProduct_RowCommand">

            <Columns>

                <asp:BoundField DataField="ProductNumber" HeaderText="ProductID" Visible="False" />

                <asp:BoundField DataField="Description" HeaderText="Description" />

                <asp:BoundField DataField="Price" DataFormatString="{0:c}" HeaderText="Price" />

                <asp:ButtonField ButtonType="Button" HeaderText="" Text="Add to Cart" CommandName="AddToCart"  />

                <asp:ButtonField ButtonType="Button" HeaderText="" Text="Product Reviews" CommandName="ProductReviews" />

            </Columns>

ButtonFields in a GridView contain the ability to add a button for a column. However, a button in a ButtonField doesn’t fire server-side events like a normal Web Form button does.

When a button is clicked in a ButtonField of a GridView Row, the GridView control fires the RowCommand event that you can used to handle ButtonFields click events.

 
        </asp:GridView>     

 

    </form>

</body>

</html>