CheckBoxField 构造函数

定义

初始化 CheckBoxField 类的新实例。

public:
 CheckBoxField();
public CheckBoxField ();
Public Sub New ()

示例

下面的代码示例演示如何使用构造函数动态向控件添加 CheckBoxField 对象 GridView


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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  void Page_Load(Object sender, EventArgs e)
  {

    // The columns need to be created only the first time
    // the page is loaded.
    if(!IsPostBack)
    {
  
      // Dynamically create columns to display the desired
      // fields from the data source. Columns that are 
      // dynamically added to the GridView control are not persisted 
      // across posts and must be recreated each time the page is 
      // loaded.
  
      // Create a BoundField object to display an author's last name.
      BoundField lastNameBoundField = new BoundField();
      lastNameBoundField.DataField = "au_lname";
      lastNameBoundField.HeaderText = "Last Name";
    
      // Create a CheckBoxField object to indicate whether the author
      // is on contract.
      CheckBoxField contractCheckBoxField = new CheckBoxField();
      contractCheckBoxField.DataField = "contract";
      contractCheckBoxField.HeaderText = "Contract";
    
      // Add the columns to the Columns collection of the
      // GridView control.
      AuthorsGridView.Columns.Add(lastNameBoundField);
      AuthorsGridView.Columns.Add(contractCheckBoxField);

    }
  
  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>CheckBoxField Constructor Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>CheckBoxField Constructor Example</h3>

      <asp:gridview id="AuthorsGridView" 
        datasourceid="AuthorsSqlDataSource" 
        autogeneratecolumns="False"
        runat="server">                
      </asp:gridview>
            
      <!-- This example uses Microsoft SQL Server and connects -->
      <!-- to the Pubs sample database.                        -->
      <asp:sqldatasource id="AuthorsSqlDataSource"  
        selectcommand="SELECT [au_lname], [au_fname], [address], [city], [state], [zip], [contract] FROM [authors]"
        connectionstring="server=localhost;database=pubs;integrated security=SSPI"
        runat="server">
      </asp:sqldatasource>
            
    </form>
  </body>
</html>

<%@ Page language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  Sub Page_Load(sender as Object, e As EventArgs)
  
    ' The columns need to be created only the first time
    ' the page is loaded.
    If Not IsPostBack Then

      ' Dynamically create columns to display the desired
      ' fields from the data source. Columns that are 
      ' dynamically added to the GridView control are not persisted 
      ' across posts and must be recreated each time the page is 
      ' loaded.
  
      ' Create a BoundField object to display an author's last name.
      Dim lastNameBoundField As BoundField = New BoundField
      lastNameBoundField.DataField = "au_lname"
      lastNameBoundField.HeaderText = "Last Name"
    
      ' Create a CheckBoxField object to indicate whether the author
      ' is on contract.
      Dim contractCheckBoxField As CheckBoxField = New CheckBoxField
      contractCheckBoxField.DataField = "contract"
      contractCheckBoxField.HeaderText = "Contract"
    
      ' Add the columns to the Columns collection of the
      ' GridView control.
      AuthorsGridView.Columns.Add(lastNameBoundField)
      AuthorsGridView.Columns.Add(contractCheckBoxField)

    End If
  
  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>CheckBoxField Constructor Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>CheckBoxField Constructor Example</h3>

      <asp:gridview id="AuthorsGridView" 
        datasourceid="AuthorsSqlDataSource" 
        autogeneratecolumns="False"
        runat="server">                
      </asp:gridview>
            
      <!-- This example uses Microsoft SQL Server and connects -->
      <!-- to the Pubs sample database.                        -->
      <asp:sqldatasource id="AuthorsSqlDataSource"  
        selectcommand="SELECT [au_lname], [au_fname], [address], [city], [state], [zip], [contract] FROM [authors]"
        connectionstring="server=localhost;database=pubs;integrated security=SSPI"
        runat="server">
      </asp:sqldatasource>
            
    </form>
  </body>
</html>

注解

使用此构造函数初始化类的新实例 CheckBoxField 。 将字段添加到动态创建的数据绑定控件时,通常使用此构造函数。

若要将对象动态添加到 CheckBoxField 数据绑定控件,请创建新 CheckBoxField 对象,设置其属性,然后将其添加到数据绑定控件的字段集合。 例如,如果使用控件 GridView ,请将 CheckBoxField 对象添加到 Columns 集合。

备注

尽管可以动态向数据绑定控件添加字段,但强烈建议将所有字段静态声明,然后根据需要显示或隐藏。 静态声明所有字段可减小父数据绑定控件的视图状态大小。

适用于

另请参阅