ASP.NET中获取DridView点击的按钮后的索引

来源:未知 责任编辑:责任编辑 发表时间:2013-08-22 04:53 点击:

 

<span style="font-family:'Microsoft YaHei';font-size:13px;"><%@ 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_RowCommand(Object sender, GridViewCommandEventArgs e) 

  { 

 

    // If multiple ButtonField column fields are used, use the 

    // CommandName property to determine which button was clicked. 

    if(e.CommandName=="Select") 

    { 

 

      // Convert the row index stored in the CommandArgument 

      // property to an Integer. 

     /*

若要确定引发命令事件的记录索引,请使用事件参数的CommandArgument 属性,该事件参数传递到数据绑定控件的命令事件。ButtonField 类自动以适当的索引值填充CommandArgument 属性。

    */   

      int index = Convert.ToInt32(e.CommandArgument);     

 

      // Get the last name of the selected author from the appropriate 

      // cell in the GridView control. 

      GridViewRow selectedRow = CustomersGridView.Rows[index]; 

      TableCell contactName = selectedRow.Cells[1]; 

      string contact = contactName.Text;   

 

      // Display the selected author. 

      Message.Text = "You selected " + contact + "."; 

 

    } 

 

  } 

 

</script> 

 

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

  <head runat="server"> 

    <title>ButtonField Example</title> 

</head> 

<body> 

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

 

      <h3>ButtonField Example</h3> 

 

      <asp:label id="Message" 

        forecolor="Red" 

        runat="server" 

        AssociatedControlID="CustomersGridView"/> 

 

      <!-- Populate the Columns collection declaratively. --> 

      <asp:gridview id="CustomersGridView"  

        datasourceid="CustomersSqlDataSource"  

        autogeneratecolumns="false" 

        onrowcommand="CustomersGridView_RowCommand" 

        runat="server"> 

 

        <columns> 

 

          <asp:buttonfield buttontype="Button"  

            commandname="Select" 

            headertext="Select Customer"  

            text="Select"/> 

          <asp:boundfield datafield="CompanyName"   

            headertext="Company Name"/> 

          <asp:boundfield datafield="ContactName"  

            headertext="Contact Name"/> 

 

        </columns> 

 

      </asp:gridview> 

 

        <!-- This example uses Microsoft SQL Server and connects --> 

        <!-- to the Northwind sample database.                   --> 

        <asp:sqldatasource id="CustomersSqlDataSource"   

          selectcommand="Select [CustomerID], [CompanyName], [ContactName], [ContactTitle] From [Customers]" 

          connectionstring="<%$ ConnectionStrings:NorthWindConnection%>" 

          runat="server"> 

        </asp:sqldatasource> 

 

    </form> 

  </body> 

</html> 

</span> 

摘自 hi_dzj的专栏

    发表评论
    请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
    用户名: 验证码:点击我更换图片
    最新评论 更多>>

    推荐热点

    • 浅析.NET下XML数据访问新机制
    • asp.net 面试+笔试题目第1/2页
    • C# 邮件地址是否合法的验证
    • asp.net 设置GridView的选中行的实现代码
    • C#高级编程:数据库连接[1]
    • 经典C++程序1
    • IIS 自动回收导致后台定时器失效的问题解决
    • ASP.NET&#160;GridView列表代码示例
    • Asp.net MVC源码分析--Action Filter的链式调用
    网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
    Copyright © 2008-2015 计算机技术学习交流网. 版权所有

    豫ICP备11007008号-1