ButtonField 类

表示一个字段,该字段显示为数据绑定控件中的按钮。

 

 

public class ButtonField : ButtonFieldBase

 

数据绑定控件(如 GridViewDetailsView)使用 ButtonField 类为每个显示的记录显示一个按钮。根据在其中使用 ButtonField 对象的数据绑定控件,该对象会以不同的方式显示。例如,GridView 控件将 ButtonField 对象显示为一列,而 DetailsView 控件则将该对象显示为一行。

击按钮字段中的按钮将引发父数据绑定控件的命令事件。可以提供命令事件的事件处理程序,以便在单击命令按钮时提供要执行的自定义例程。

Note注意

GridView 控件引发 RowCommand 事件,而 DetailsView 控件引发 ItemCommand 事件。

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

若要指定要显示的按钮类型,请使用 ButtonType 属性。在显示链接或命令按钮时,请使用 Text 属性指定要在按钮中显示的标题。

Note注意

如果设置了 Text 属性,则 ButtonField 中的所有按钮共享同一个标题。

此外,您还可以选择将 ButtonField 对象绑定到数据源中的字段。这使您可以为 ButtonField 对象中的按钮显示不同的标题。指定字段中的值用于按钮的文本标题。设置 DataTextField 属性以将 ButtonField 对象绑定到数据源中的字段。

在显示图像按钮时,请使用 ImageUrl 属性为 ButtonField 对象中的按钮指定要显示的图像。

Note注意

一个 ButtonField 对象中的所有按钮共享同一个图像。

通过将 Visible 属性设置为 false,可以隐藏数据绑定控件中的 ButtonField 对象。

ButtonField 对象允许您自定义其标头和脚注部分。若要在标头或脚注部分中显示标题,可以分别设置 HeaderTextFooterText 属性。可以通过设置 HeaderImageUrl 属性来显示图像,而不是在标头部分中显示文本。若要隐藏 ButtonField 对象中的标头部分,请将 ShowHeader 属性设置为 false

Note注意

某些数据绑定控件(如 GridView 控件)只能显示或隐藏控件的整个标头部分。这些数据绑定控件不支持单个按钮字段的 ShowHeader 属性。若要显示或隐藏数据绑定控件的整个标头部分(如果可用),请使用该控件的 ShowHeader 属性。

您还可以通过为字段的不同部件设置样式属性来自定义 ButtonField 对象的外观(字体颜色、背景颜色等)。下表列出了不同的样式属性。

样式属性

样式设置,用于

ControlStyle

ButtonField 的子 Web 服务器控件。

FooterStyle

ButtonField 的脚注部分。

HeaderStyle

ButtonField 的标头部分。

ItemStyle

ButtonField 中的数据项。

 

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

 

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

 

posted @ 2009-04-11 18:50  minmin8110  阅读(405)  评论(0)    收藏  举报