zoukankan      html  css  js  c++  java
  • GridView.RowCommand 事件

    当单击 GridView控件中的按钮时发生。

    命名空间:System.Web.UI.WebControls
    程序集:System.Web(在 system.web.dll 中)

    在单击 GridView 控件中的按钮时,将引发 RowCommand 事件。这使您可以提供一个这样的事件处理方法,即每次发生此事件时执行一个自定义例程。

    GridView 控件中的按钮也可调用该控件的某些内置功能。若要执行这些操作之一,请将按钮的 CommandName 属性设置为下表中的某个值。

    CommandName 值

    说明

    “Cancel”

    取消编辑操作并将 GridView 控件返回为只读模式。引发 RowCancelingEdit 事件。

    “Delete”

    删除当前记录。引发 RowDeletingRowDeleted 事件。

    “Edit”

    将当前记录置于编辑模式。引发 RowEditing 事件。

    “Page”

    执行分页操作。将按钮的 CommandArgument 属性设置为“First”、“Last”、“Next”、“Prev”或页码,以指定要执行的分页操作类型。引发 PageIndexChangingPageIndexChanged 事件。

    “Select”

    选择当前记录。引发 SelectedIndexChangingSelectedIndexChanged 事件。

    “Sort”

    GridView 控件进行排序。引发 SortingSorted 事件。

    “Update”

    更新数据源中的当前记录。引发 RowUpdatingRowUpdated 事件。

    尽管单击上表中所列出的按钮时将引发 RowCommand 事件,但仍建议您使用该表中列出的事件来执行该操作。

    GridViewCommandEventArgs 对象传递到事件处理方法,以便您可以确定被单击按钮的命令名和命令参数。

    Note注意

    GridViewCommandEventArgs 类未包含一个用于指示单击按钮所在行的属性。如果需要知道哪个行引发了事件,请使用 CommandArgument 属性将行的索引传给事件处理方法。

    下面的代码示例演示如何使用 RowCommand 事件在单击某行的“添加”按钮时将客户名称从 GridView 控件添加到 ListBox 控件。

    <%@ Page language="C#" %> <script runat="server">

      void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
      {
        // If multiple buttons are used in a GridView control, use the
        // CommandName property to determine which button was clicked.
        if(e.CommandName=="Add")
        {
          // Convert the row index stored in the CommandArgument
          // property to an Integer.
          int index = Convert.ToInt32(e.CommandArgument);
               
          // Retrieve the row that contains the button clicked
          // by the user from the Rows collection.
          GridViewRow row = CustomersGridView.Rows[index];
               
          // Create a new ListItem object for the customer in the row.    
          ListItem item = new ListItem();
          item.Text = Server.HtmlDecode(row.Cells[2].Text);
               
          // If the customer is not already in the ListBox, add the ListItem
          // object to the Items collection of the ListBox control.
          if (!CustomersListBox.Items.Contains(item))
          {
            CustomersListBox.Items.Add(item);
          }          
        }
      }

      void CustomersGridView_RowCreated(Object sender, GridViewRowEventArgs e)
      {
       
        // The GridViewCommandEventArgs class does not contain a
        // property that indicates which row's command button was
        // clicked. To identify which row's button was clicked, use
        // the button's CommandArgument property by setting it to the
        // row's index.
        if(e.Row.RowType == DataControlRowType.DataRow)
        {
          // Retrieve the LinkButton control from the first column.
          LinkButton addButton = (LinkButton)e.Row.Cells[0].Controls[0];
             
          // Set the LinkButton's CommandArgument property with the
          // row's index.
          addButton.CommandArgument = e.Row.RowIndex.ToString();
        }

      }
       
    </script>

    <html>
      <body>
        <form runat="server">
           
          <h3>GridView RowCommand Example</h3>
               
          <table width="100%">        
            <tr>               
              <td width="50%">
                       
                <asp:gridview id="CustomersGridView"
                  datasourceid="CustomersSource"
                  allowpaging="true"
                  autogeneratecolumns="false"
                  onrowcommand="CustomersGridView_RowCommand"
                  onrowcreated="CustomersGridView_RowCreated" 
                  runat="server">
                   
                  <columns>
                    <asp:buttonfield buttontype="Link"
                      commandname="Add"
                      text="Add"/>
                    <asp:boundfield datafield="CustomerID"
                      headertext="Customer ID"/>
                    <asp:boundfield datafield="CompanyName"
                      headertext="Company Name"/>
                    <asp:boundfield datafield="City"
                      headertext="City"/>        
                  </columns>
                   
                </asp:gridview>
                       
              </td>
                       
              <td valign="top" width="50%">
                       
                Customers: <br/>
                <asp:listbox id="CustomersListBox"
                  runat="server"/>
                       
              </td> 
            </tr>     
          </table>
               
          <!-- 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="CustomersSource"
            selectcommand="Select [CustomerID], [CompanyName], [City] From [Customers]"
            connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
            runat="server"/>
               
        </form>
      </body>
    </html>

  • 相关阅读:
    一种开源的分布式消息系统Nats
    资产盘点:除了金钱,一个人还有哪些资产?
    博客首页规则改版公告
    <html>
    欢迎使用CSDN-markdown编辑器
    java 小程序查看器 启动:未初始化小程序 解决方法
    Hadoop2.6.0版本号MapReudce演示样例之WordCount(一)
    深入学习IOZone【转】
    i.MX6UL -- PWM用户空间使用方法【转】
    linux PWM蜂鸣器移植以及驱动程序分析【转】
  • 原文地址:https://www.cnblogs.com/12go/p/2159805.html
Copyright © 2011-2022 走看看