• GridView中控件列使用方法小结


    方法一.使用GridView自带ButtonField控件。典型代码如下:

    <%@ Page language="C#" %>

    <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.
          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>
      
    <body>
        
    <form runat="server">
            
          
    <h3>ButtonField Example</h3>
          
          
    <asp:label id="Message"
            forecolor
    ="Red"
            runat
    ="server"/>
                        
          
    <!-- 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>

    以上代码来源于Msdnhttp://msdn.microsoft.com/zh-cn/library/system.web.ui.webcontrols.buttonfield(VS.80).aspx,原来自己以为ButtonField没有办法取到行号,可是在以上这个示例中显示,ButtonField已经自带了这个属性,只要在RowCommand事件中,转换一下e.CommandArgument即可。

    方法二.运用模版列。该方法比较灵活。可以自指定CommandArgument绑定数据源上某一字段。典型代码如下:

    CommandArgument='<%# Eval("Id") %>'

    另:无论是方法一还是方法二,只要有办法取出GridView中事件源所在行号,那么也可以结合设置GridViewDataKeyNames属性,来获取数据源相关关键字,进行事件操作。

    文章出处:www.cnblogs.com/jizhong

    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。否则保留追究法律责任的权利。

  • 相关阅读:
    【BZOJ 2440】[中山市选2011]完全平方数
    【BZOJ 1066】[SCOI2007]蜥蜴
    luogu P1317 低洼地
    luogu P1379 八数码难题
    luogu P1886 滑动窗口
    luogu P1032 字串变换
    题解 P1876 【开灯】
    题解 P1720 【月落乌啼算钱】
    题解 P2863 【[USACO06JAN]牛的舞会The Cow Prom】
    关于线性回归
  • 原文地址:https://www.cnblogs.com/jizhong/p/1523786.html
Copyright © 2020-2023  润新知