ButtonField 对象在 GridView 控件中如合使用

下面的代码示例演示如何使用 ButtonField 对象显示 GridView 控件中的一列命令按钮。

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

< script runat = " server " >

    
Sub CustomersGridView_RowCommand(ByVal sender As ObjectByVal e As GridViewCommandEventArgs)
  
        
' 如果多個使用多個buttonfield,利用CommandName來確定那個buttonfield被點擊.
        If e.CommandName = "Select" Then
    
            
'轉換列的索引放進CommandArgument
            
            
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
    
            
' 從appropriate獲得所選作者的姓
            ' cell in the GridView control.
            Dim selectedRow As GridViewRow = CustomersGridView.Rows(index)
            
Dim contactCell As TableCell = selectedRow.Cells(1)
            
Dim contact As String = contactCell.Text
    
            
' 顯示所選作者.
            Message.Text = "You selected " & contact & "."
      
        
End If
    
    
End Sub

    
</ script >

< html >
  
< body >
    
< form id = " Form1 "  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
= " <%___FCKpd___0nbsp;ConnectionStrings:NorthWindConnection%> "
          runat
= " server " >
        
</ asp:sqldatasource >
            
    
</ form >
  
</ body >
</ html >

C#
<% @ 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 >
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值