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

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

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

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

CommandName 值

说明

“Cancel”

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

“Delete”

删除当前记录。引发 RowDeleting 和 RowDeleted 事件。

“Edit”

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

“Page”

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

“Select”

选择当前记录。引发 SelectedIndexChanging 和 SelectedIndexChanged 事件。

“Sort”

GridView 控件进行排序。引发 Sorting 和 Sorted 事件。

“Update”

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

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

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

注意

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>

转载于:https://www.cnblogs.com/12go/archive/2011/08/30/2159805.html

GridView.RowCommand 事件相关推荐

  1. GridView RowCommand事件中取得當前行

    Dim row As GridViewRow = CType(CType(e.CommandSource, Button).NamingContainer, GridViewRow) '取得當前行 D ...

  2. [GridView]在 RowCommand事件中,自订的Button 如何取出某一列的索引值(RowIndex)

    这是我的文章备份,有空请到我的网站走走, http://www.dotblogs.com.tw/mis2000lab/ 才能掌握我提供的第一手信息,谢谢您. http://www.dotblogs.c ...

  3. Android 开发之 GridView及其事件监听

    2019独角兽企业重金招聘Python工程师标准>>> 步骤: //1.准备数据源 //2.新建适配器 //3.GridView加载适配器 //4.GridView配置事件监听器 ` ...

  4. GridView的RowCommand事件中取得行索引 技巧

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == ...

  5. 【ASP.NET】演绎GridView基本操作事件

    对于gridview学NET的同学再熟悉不过,但是其中功能事件是否能编码熟练实现?前不久看点博文,以及资料,综合自己的一些想法,汇总如下: 数据库设计如下,以便更好理解: 设计: 实现: GridVi ...

  6. GridView RowCommand (handle with e.CommandArgument)

    转自:http://www.pluralsight.com/blogs/fritz/archive/2005/06/24/11975.aspx I've been working on a proje ...

  7. gridview SelectedIndexChanged事件

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False&q ...

  8. GridView RowCommand 获取列值

        //根据按钮属性,进行操作     protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) ...

  9. ASP.NET DEMO 14: 如何在 GridView/DataGrid 模板列中使用自动回发的 CheckBox/DropDownList

    有时候希望在 GridView 模板中使用自动回发的 CheckBox (autopostback=true) ,但是 CheckBox 没有 CommandName 属性,因此也就无法在 GridV ...

  10. GridView自定义分页样式(上一页,下一页,到第几页)(新手教程)

    今天要为网站做一个文章列表,发现GridView的分页样式很难看,于是结合网上的例子,自己做了一个.不是很美观,不过还是很实用的,先看下效果吧,如图(1).演示地址http://www.veryam. ...

最新文章

  1. linux代码中能出现中文吗_Linux命令很熟悉,你知道它们的英文全称和中文解释吗?...
  2. Fatal error: Can't open and lock privilege tables: Table 'mysql.host' doesn't exist的
  3. 常用的SSH注解标签
  4. SpringMVC基础配置与简单的SpringMVC的程序
  5. 协程asyncio_Python 异步模块 asyncio 中的协程与任务
  6. java学习(7):巩固练习
  7. bec初级第一课_在您的第一个初级开发人员工作中如何生存和发展
  8. (53)Xilinx时钟原语-BUG与IBUFG(第11天)
  9. 嘘!你与谷歌语音助手的对话,可能已经泄露……
  10. 库克遭一名自称其妻子的陌生女子威胁 苹果紧急申请限制令
  11. Androidstudio查不出具体哪行报错解决办法
  12. 华为防火墙管理员角色和级别详解
  13. 数据库课程设计大作业大盘点【建议在校生收藏】
  14. Linux 基本命令入门
  15. 如何使用计算机word,电脑系统教程:电脑Word分栏怎么用
  16. python量化选股策略 源码_【一点资讯】Python实现行业轮动量化选股【附完整源码】...
  17. 工作室多wifi软路由工作室Ros软路由使用教程
  18. 惠普打印机墨盒更换教程_惠普打印机加墨教程:老司机教你
  19. Excel的VLOOKUP函数及其用法
  20. Keil uVision5 MDK(ARM)软件的介绍、下载、安装与注册

热门文章

  1. zookeeper + kafka 集群安装部署教程(linux环境下)
  2. CCF - 201604-2 - 俄罗斯方块
  3. JavaScript必须了解的知识点总结【转】
  4. C# 获取所有网卡信息
  5. 《无码的青春》第七章 御姐
  6. 我是如何战胜懒惰的?
  7. Oracle创建上下文 SYS_CONTEXT
  8. Java中线程出现Exception in thread Thread-0 java.lang.IllegalMonitorStateException异常 解决方法...
  9. javascript 笔记--变量
  10. Jmeter插件监控服务器性能