BoundField 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化 BoundField 類別的新執行個體。
public:
BoundField();
public BoundField();
Public Sub New ()
範例
以下程式碼範例示範如何利用建構子動態將物件加入BoundFieldGridView控制項。
<%@ 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)
{
// Dynamically generated field columns need to be created only
// the first time the page is loaded.
if(!IsPostBack)
{
// Dynamically create field columns to display the desired
// fields from the data source.
// Create a BoundField object to display a customer's company name.
BoundField nameBoundField = new BoundField();
nameBoundField.DataField = "CompanyName";
nameBoundField.HeaderText = "Company Name";
// Create a BoundField object to display a customer's city.
BoundField cityBoundField = new BoundField();
cityBoundField.DataField = "City";
cityBoundField.HeaderText = "City";
// Add the field columns to the ColumnFields collection of the
// GridView control.
CustomersGridView.Columns.Add(nameBoundField);
CustomersGridView.Columns.Add(cityBoundField);
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>BoundField Constructor Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>BoundField Constructor Example</h3>
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSqlDataSource"
autogeneratecolumns="false"
runat="server">
</asp:gridview>
<!-- 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="CustomersSqlDataSource"
selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
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(ByVal sender As Object, ByVal e As EventArgs)
' Dynamically generated field columns need to be created only
' the first time the page is loaded.
If Not IsPostBack Then
' Dynamically create field columns to display the desired
' fields from the data source.
' Create a BoundField object to display a customer's company name.
Dim nameBoundField As New BoundField()
nameBoundField.DataField = "CompanyName"
nameBoundField.HeaderText = "Company Name"
' Create a BoundField object to display a customer's city.
Dim cityBoundField As New BoundField()
cityBoundField.DataField = "City"
cityBoundField.HeaderText = "City"
' Add the field columns to the ColumnFields collection of the
' GridView control.
CustomersGridView.Columns.Add(nameBoundField)
CustomersGridView.Columns.Add(cityBoundField)
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>BoundField Constructor Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>BoundField Constructor Example</h3>
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSqlDataSource"
autogeneratecolumns="false"
runat="server">
</asp:gridview>
<!-- 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="CustomersSqlDataSource"
selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server">
</asp:sqldatasource>
</form>
</body>
</html>
備註
使用此建構子初始化該類別的新實例 BoundField 。 此建構子常用於在動態建立的資料綁定控制項中新增欄位。
若要動態將物件加入 BoundField 資料綁定控制項,請建立新 BoundField 物件,設定其屬性,然後將其加入資料綁定控制項的欄位集合。 例如,如果你使用控制項,GridView請將物件加入BoundFieldColumns集合。
Note
雖然你可以動態地為資料綁定控制項新增欄位,但強烈建議欄位先靜態宣告,然後依情況顯示或隱藏。 靜態宣告所有欄位會減少父資料綁定控制的視圖狀態大小。