ButtonField 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化 ButtonField 類別的新執行個體。
public:
ButtonField();
public ButtonField();
Public Sub New ()
範例
以下程式碼範例示範如何使用 M:System.Web.UI.WebControls.ButtonField.#ctor 建構函式動態地將物件加入ButtonField控制項。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 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 Customer from the appropriate
// cell in the GridView control.
GridViewRow selectedRow = CustomersGridView.Rows[index];
TableCell contactCell = selectedRow.Cells[1];
string contact = contactCell.Text;
// Display the selected Customer.
Message.Text = "You selected " + contact + ".";
}
}
void Page_Load(Object sender, EventArgs e)
{
// The 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 ButtonField object to allow the user to
// select an Customer.
ButtonField selectButtonField = new ButtonField ();
selectButtonField.ButtonType = ButtonType.Button;
selectButtonField.CommandName = "Select";
selectButtonField.HeaderText = "Select Customer";
selectButtonField.Text = "Select";
// Create a BoundField object to display an Customer's last name.
BoundField contactNameBoundField = new BoundField();
contactNameBoundField.DataField = "ContactName";
contactNameBoundField.HeaderText = "Contact Name";
// Create a BoundField object to display an Customer's first name.
BoundField contactTitleBoundField = new BoundField();
contactTitleBoundField.DataField = "ContactTitle";
contactTitleBoundField.HeaderText = "Contact Title";
// Add the field columns to the Columns collection of the
// GridView control.
CustomersGridView.Columns.Add (selectButtonField);
CustomersGridView.Columns.Add(contactNameBoundField);
CustomersGridView.Columns.Add(contactTitleBoundField);
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ButtonField Constructor Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>ButtonField Constructor Example</h3>
<asp:label id="Message"
forecolor="Red"
runat="server"
AssociatedControlID="CustomersGridView"/>
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSqlDataSource"
autogeneratecolumns="False"
onrowcommand="CustomersGridView_RowCommand"
runat="server">
</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>
<%@ 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 CustomersGridView_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
' If multiple ButtonField column fields are used, use the
' CommandName property to determine which button was clicked.
If e.CommandName = "Select" Then
' Convert the row index stored in the CommandArgument
' property to an Integer.
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
' Get the last name of the selected Customer from the appropriate
' cell in the GridView control.
Dim selectedRow As GridViewRow = CustomersGridView.Rows(index)
Dim contactNameCell As TableCell = selectedRow.Cells(1)
Dim contactName As String = contactNameCell.Text
' Display the selected Customer.
Message.Text = "You selected " & contactName & "."
End If
End Sub
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
' The 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 ButtonField object to allow the user to
' select an Customer.
Dim selectButtonField As New ButtonField
selectButtonField.ButtonType = ButtonType.Button
selectButtonField.CommandName = "Select"
selectButtonField.HeaderText = "Select Customer"
selectButtonField.Text = "Select"
' Create a BoundField object to display an Customer's last name.
Dim contactNameBoundField As New BoundField
contactNameBoundField.DataField = "ContactName"
contactNameBoundField.HeaderText = "Contact Name"
' Create a BoundField object to display an Customer's first name.
Dim contactTitleBoundField As New BoundField
contactTitleBoundField.DataField = "ContactTitle"
contactTitleBoundField.HeaderText = "Contact Title"
' Add the field columns to the Columns collection of the
' GridView control.
CustomersGridView.Columns.Add(selectButtonField)
CustomersGridView.Columns.Add(contactNameBoundField)
CustomersGridView.Columns.Add(contactTitleBoundField)
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ButtonField Constructor Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>ButtonField Constructor Example</h3>
<asp:label id="Message"
forecolor="Red"
runat="server"
AssociatedControlID="CustomersGridView"/>
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSqlDataSource"
autogeneratecolumns="False"
onrowcommand="CustomersGridView_RowCommand"
runat="server">
</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>
備註
使用 ButtonField 建構子初始化該類別的新實例 ButtonField 。 此建構子常用於在動態建立的資料綁定控制項中新增欄位。
若要動態將物件加入 ButtonField 資料綁定控制項,請建立新 ButtonField 物件,設定其屬性,然後將其加入資料綁定控制項的欄位集合。 例如,如果你使用控制項,GridView請將物件加入ButtonFieldColumns集合。
Note
雖然你可以動態地將欄位加入資料綁定控制項,但欄位應靜態宣告,然後依情況顯示或隱藏。 靜態宣告所有欄位會減少父資料綁定控制的視圖狀態大小。