CommandField 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化 CommandField 類別的新執行個體。
public:
CommandField();
public CommandField ();
Public Sub New ()
範例
下列程式碼範例示範如何使用 建構函式,將欄位資料行動態新增 CommandField 至 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)
{
// Dynamically generated field columns need to be created only
// the first time the page is loaded.
if (!IsPostBack)
{
// Create a CommandField object to display a Select button
// for each record in the GridView control.
CommandField selectCommandField = new CommandField();
selectCommandField.ShowSelectButton = true;
selectCommandField.SelectText = "Select Company";
// Create a BoundField object to display the company names.
BoundField lastNameBoundField = new BoundField();
lastNameBoundField.DataField = "au_lname";
lastNameBoundField.HeaderText = "Last Name";
// 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(selectCommandField);
CustomersGridView.Columns.Add(nameBoundField);
CustomersGridView.Columns.Add(cityBoundField);
}
}
void CustomersGridView_SelectedIndexChanged(Object sender, EventArgs e)
{
// Get the value of the company name and city from the appropriate cells.
String companyName = CustomersGridView.SelectedRow.Cells[1].Text;
String city = CustomersGridView.SelectedRow.Cells[2].Text;
Message.Text = "You selected " + companyName + " located in " + city + ".";
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>CommandField Constructor Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>CommandField Constructor Example</h3>
<asp:label id="Message"
forecolor="Red"
runat="server"/>
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSqlDataSource"
autogeneratecolumns="False"
onselectedindexchanged="CustomersGridView_SelectedIndexChanged"
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], [City] From [Customers]"
connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server">
</asp:sqldatasource>
</form>
</body>
</html>
<%@ Page language="VB" AutoEventWireup="False" %>
<!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) Handles Me.Load
' Dynamically generated field columns need to be created only
' the first time the page is loaded.
If Not IsPostBack Then
' Create a CommandField object to display a Select button
' for each record in the GridView control.
Dim selectCommandField As New CommandField()
selectCommandField.ShowSelectButton = True
selectCommandField.SelectText = "Select Company"
' Create a BoundField object to display the company names.
Dim lastNameBoundField As New BoundField()
lastNameBoundField.DataField = "au_lname"
lastNameBoundField.HeaderText = "Last Name"
' 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(selectCommandField)
CustomersGridView.Columns.Add(nameBoundField)
CustomersGridView.Columns.Add(cityBoundField)
End If
End Sub
Sub CustomersGridView_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles CustomersGridView.SelectedIndexChanged
' Get the value of the company name and city from the appropriate cells.
Dim companyName As String = CustomersGridView.SelectedRow.Cells(1).Text
Dim city As String = CustomersGridView.SelectedRow.Cells(2).Text
Message.Text = "You selected " & companyName & " located in " & city & "."
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>CommandField Constructor Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>CommandField Constructor Example</h3>
<asp:label id="Message"
forecolor="Red"
runat="server"/>
<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], [City] From [Customers]"
connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server">
</asp:sqldatasource>
</form>
</body>
</html>
備註
使用此建構函式來初始化 類別的新實例 CommandField 。 將欄位新增至動態建立的資料繫結控制項時,通常會使用此建構函式。
若要動態將欄位新增 CommandField 至資料繫結控制項,請建立新的 CommandField 物件、設定其屬性,然後將它新增至資料繫結控制項的欄位集合。 例如,如果您使用 GridView 控制項,請將 物件新增 CommandField 至 Columns 集合。
注意
雖然您可以動態地將欄位新增至資料繫結控制項,但強烈建議以靜態方式宣告欄位,然後視需要顯示或隱藏欄位。 以靜態方式宣告所有欄位會減少父資料繫結控制項的檢視狀態大小。