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 集合中。
备注
虽然可以动态向数据绑定控件添加字段,但强烈建议根据需要静态声明字段,然后显示或隐藏字段。 静态声明所有字段可减少父数据绑定控件的视图状态的大小。