CheckBoxField 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化 CheckBoxField 類別的新執行個體。
public:
CheckBoxField();
public CheckBoxField ();
Public Sub New ()
範例
下列程式碼範例示範如何使用 建構函式,以動態方式將 物件 GridView 加入 CheckBoxField 控制項。
<%@ 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 集合。
注意
雖然您可以動態將欄位新增至資料繫結控制項,但強烈建議您將所有欄位以靜態方式宣告,然後視需要顯示或隱藏。 以靜態方式宣告所有欄位會減少父資料繫結控制項的檢視狀態大小。