• gridview的几个事件


    先了解一下:LinkButton.CommandArgument 属性

    一个可选参数,与关联的 CommandName 属性一起传递到 Command 事件处理程序。默认值为 String.Empty。
    备注:使用 CommandArgument 属性指定补充 CommandName 属性的参数。

    注意 :CommandArgument 属性通常只在设置 CommandName 属性时使用。
     

    通过 CommandArgument 属性可以提供有关要执行的命令的附加信息,从而对 CommandName 属性加以补充。例如,可以将 CommandName 属性设置为 Sort 并将 CommandArgument 属性设置为 Ascending,以指定按升序排序的命令。

    代码
    <%@ Page Language="C#" AutoEventWireup="True" %>
    <html>
    <head>
       
    <script language="C#" runat="server">    
          
    void LinkButton_Command(Object sender, CommandEventArgs e) 
          {
             Label1.Text 
    = "You chose: " + e.CommandName + " Item " + e.CommandArgument;
          } 
       
    </script> 
    </head>
    <body> 
       
    <form runat=server> 
          
    <h3>LinkButton Command Event Example</h3>  
          
    <asp:LinkButton id="LinkButton1" 
               Text
    ="Order Item 10001"
               CommandName
    ="Order" 
               CommandArgument
    ="10001" 
               OnCommand
    ="LinkButton_Command" 
               runat
    ="server"/> 
          
    <br> 
          
    <asp:LinkButton id="LinkButton2" 
               Text
    ="Order Item 10002"
               CommandName
    ="Order" 
               CommandArgument
    ="10002" 
               OnCommand
    ="LinkButton_Command" 
               Runat
    ="server"/> 
          
    <br>
          
    <p> 
          
    <asp:Label id="Label1" runat="server"/> 
       
    </form>
    </body>
    </html>

     

    GridView.RowCreated 事件

    GridView 控件中创建行时发生

    呈现 GridView 控件之前,必须先为该控件中的每一行创建一个 GridViewRow 对象。在创建 GridView 控件中的每一行时,将引发 RowCreated 事件。这使您可以提供一个这样的事件处理方法,即每次发生此事件时都执行一个自定义例程(如在行中添加自定义内容)。

    GridViewRowEventArgs 对象将被传给事件处理方法,以便您可以访问正在创建的行的属性。若要访问行中的特定单元格,请使用 GridViewRowEventArgs 对象的 Cells 属性。使用 RowType 属性可确定正在创建的是哪一种行类型(标题行、数据行等等)。

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

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

    下面的代码示例演示如何使用 RowCreated 事件将正在创建的行的索引存储在该行中所包含的 LinkButton 控件的 CommandArgument 属性中。这允许您确定在用户单击 LinkButton 控件按钮时包含该控件的行的索引。

    代码
    <%@ 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>
     
  • 相关阅读:
    tinyhttp源码阅读(注释)
    BSON 1.0版本规范(翻译)
    linux下编译qt5.6.0静态库——configure配置
    springboot(二):web综合开发
    Linux 4.10中两个新特性与我的一段故事
    [Perforce]password (P4PASSWD) invalid or unset. 的错误解决
    Cross compiling coreutils and generate the manpages
    【蓝桥杯】PREV-21 回文数字
    【Qt】StackedWidget
    凡人视角C++之string(上)
  • 原文地址:https://www.cnblogs.com/Fskjb/p/1724634.html
Copyright © 2020-2023  润新知