按一下以給予評分及指教
MSDN
MSDN Library
.NET 開發
先前版本
類別庫參考
GridView 類別
GridView 事件
 RowCommand 事件

  開啟低頻寬檢視
本頁僅適用於
Microsoft Visual Studio 2005/.NET Framework 2.0

其他版本也適用於下列軟體:
.NET Framework 類別庫
GridView.RowCommand 事件

注意:這個事件是 .NET Framework 2.0 版的新功能。

按一下 GridView 控制項中的按鈕時發生。

命名空間: System.Web.UI.WebControls
組件: System.Web (在 system.web.dll 中)

Visual Basic (宣告)
Public Event RowCommand As GridViewCommandEventHandler
Visual Basic (使用方式)
Dim instance As GridView
Dim handler As GridViewCommandEventHandler

AddHandler instance.RowCommand, handler
C#
public event GridViewCommandEventHandler RowCommand
C++
public:
event GridViewCommandEventHandler^ RowCommand {
    void add (GridViewCommandEventHandler^ value);
    void remove (GridViewCommandEventHandler^ value);
}
J#
/** @event */
public void add_RowCommand (GridViewCommandEventHandler value)

/** @event */
public void remove_RowCommand (GridViewCommandEventHandler value)
JScript
JScript 可以支援使用事件,但不允許宣告新的事件。

按一下 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 控制項。

Visual Basic
<%@ Page language="VB" %>

<script runat="server">

  Sub CustomersGridView_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)

    ' If multiple buttons are used in a GridView control, use the
    ' CommandName property to determine which button was clicked.
    If e.CommandName = "Add" Then
    
      ' Convert the row index stored in the CommandArgument
      ' property to an Integer.
      Dim index As Integer = Convert.ToInt32(e.CommandArgument)
            
      ' Retrieve the row that contains the button clicked 
      ' by the user from the Rows collection.
      Dim row As GridViewRow = CustomersGridView.Rows(index)
            
      ' Create a new ListItem object for the customer in the row.     
      Dim item As 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 Not CustomersListBox.Items.Contains(item) Then
      
        CustomersListBox.Items.Add(item)
        
      End If
      
    End If
    
  End Sub

  Sub CustomersGridView_RowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    
    ' 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 Then
    
      ' Retrieve the LinkButton control from the first column.
      Dim addButton As LinkButton = CType(e.Row.Cells(0).Controls(0), LinkButton)
          
      ' Set the LinkButton's CommandArgument property with the
      ' row's index.
      addButton.CommandArgument = e.Row.RowIndex.ToString()
      
    End If

  End Sub
    
</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>

C#
<%@ 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>

Windows 98、 Windows 2000 SP4、 Windows Server 2003、 Windows XP Media Center Edition、 Windows XP Professional x64 Edition、 Windows XP SP2、 Windows XP Starter Edition

.NET Framework 並不支援各種平台的所有版本。如需支援平台版本的相關資訊,請參閱系統需求一節的內容。

.NET Framework

支援版本:2.0
社群內容   什麼是社群內容?
新增內容 RSS  註解
Processing
© 2009 Microsoft Corporation. 著作權所有,並保留一切權利。 使用規定  |  商標  |  隱私權聲明
Page view tracker