How to: Respond to button events in the gridview Control

Source: Internet
Author: User

Conversion from (msdn): http://msdn.microsoft.com/zh-cn/library/bb907626.aspx

When you click the button in the gridview control, the rowcommand event is triggered. The gridview control has built-in functions for editing, deleting, and paging operations. You can also add buttons and use the rowcommand event to add custom functions to the control.

You can add the custom function to the gridview control in the following way:

  • Add a buttonfield control to the gridview control.

  • Add a button, linkbutton, or imagebutton control to the template in the gridview control.

You can use the commandname attribute of the event parameter to identify the button function. If you use the buttonfield or templatefield control, you can also use the commandargument attribute to identify the current row. If the buttonfield control is used, the commandargument attribute is automatically set as a row index. If you use the templatefield control, the control does not automatically set the commandargument attribute. In this case, if you must determine the row index in the event handler, you can use the data binding expression to set the commandargument attribute of the button to the row index.

Responds to button events in the gridview control.
  1. Set the commandname attribute of the button to the string that identifies its function, such as print or copy ".

  2. If you use the templatefield control and you must access the row index in the event handler method, set the commandargument attribute of the button to the expression that identifies the current row.

    The following example shows how to set the commandargument attribute of a button in the templatefield column to the current row index. In this example, This column contains a button control that displays the shopping cart.

    Visual Basic
    <asp:TemplateField>  <ItemTemplate>    <asp:Button ID="AddButton" runat="server"       CommandName="AddToCart"       CommandArgument="<%# CType(Container,GridViewRow).RowIndex %>"      Text="Add to Cart" />  </ItemTemplate> </asp:TemplateField>

     

    C #
    <asp:TemplateField>  <ItemTemplate>    <asp:Button ID="AddButton" runat="server"       CommandName="AddToCart"       CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"      Text="Add to Cart" />  </ItemTemplate> </asp:TemplateField>
  3. Create a method for the rowcommand event of the gridview control. In this method, perform the following operations:

    1. Check the commandname attribute of the event parameter object to view the input string.

    2. If necessary, use the commandargument attribute to retrieve the index of the row containing the button.

    3. Execute the corresponding logic for the button clicked by the user.

    The following example shows how to respond to the button clicking in the gridview control. In this example, the button in the templatefield control sends the command "addtocart ". Rowcommand event handler determines the button to be clicked. If the shopping cart button is clicked, the Code executes the corresponding logic.

    Visual Basic
    Protected Sub GridView1_RowCommand(ByVal sender As Object, _  ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs)  If (e.CommandName = "AddToCart") Then    ' Retrieve the row index stored in the CommandArgument property.    Dim index As Integer = Convert.ToInt32(e.CommandArgument)    ' Retrieve the row that contains the button     ' from the Rows collection.    Dim row As GridViewRow = GridView1.Rows(index)    ' Add code here to add the item to the shopping cart.  End IfEnd Sub

     

    C #
    protected void GridView1_RowCommand(object sender,   GridViewCommandEventArgs e){  if (e.CommandName == "AddToCart")  {    // Retrieve the row index stored in the     // CommandArgument property.    int index = Convert.ToInt32(e.CommandArgument);    // Retrieve the row that contains the button     // from the Rows collection.    GridViewRow row = GridView1.Rows[index];    // Add code here to add the item to the shopping cart.  }  }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.