CommandField 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示用于显示命令按钮的特殊字段,这些按钮可在数据绑定控件中执行选择、编辑、插入或删除操作。
public ref class CommandField : System::Web::UI::WebControls::ButtonFieldBase
public class CommandField : System.Web.UI.WebControls.ButtonFieldBase
type CommandField = class
inherit ButtonFieldBase
Public Class CommandField
Inherits ButtonFieldBase
- 继承
示例
下面的代码示例演示如何使用 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">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>CommandField Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>CommandField Example</h3>
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSqlDataSource"
autogeneratecolumns="false"
datakeynames="CustomerID"
runat="server">
<columns>
<asp:commandfield showeditbutton="true"
headertext="Edit Controls"/>
<asp:boundfield datafield="CustomerID"
headertext="Customer ID" />
<asp:boundfield datafield="CompanyName"
headertext="Company Name"/>
<asp:boundfield datafield="Address"
headertext="Address"/>
<asp:boundfield datafield="City"
headertext="City"/>
<asp:boundfield datafield="PostalCode"
headertext="ZIP Code"/>
<asp:boundfield datafield="Country"
headertext="Country"/>
</columns>
</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]"
updatecommand="Update Customers Set CompanyName=@CompanyName, Address=@Address, City=@City, PostalCode=@PostalCode, Country=@Country Where (CustomerID = @CustomerID)"
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">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>CommandField Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>CommandField Example</h3>
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSqlDataSource"
autogeneratecolumns="false"
datakeynames="CustomerID"
runat="server">
<columns>
<asp:commandfield showeditbutton="true"
headertext="Edit Controls"/>
<asp:boundfield datafield="CustomerID"
headertext="Customer ID" />
<asp:boundfield datafield="CompanyName"
headertext="Company Name"/>
<asp:boundfield datafield="Address"
headertext="Address"/>
<asp:boundfield datafield="City"
headertext="City"/>
<asp:boundfield datafield="PostalCode"
headertext="ZIP Code"/>
<asp:boundfield datafield="Country"
headertext="Country"/>
</columns>
</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]"
updatecommand="Update Customers Set CompanyName=@CompanyName, Address=@Address, City=@City, PostalCode=@PostalCode, Country=@Country Where (CustomerID = @CustomerID)"
connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server">
</asp:sqldatasource>
</form>
</body>
</html>
下面的代码示例演示如何使用 CancelText、 EditText和 UpdateText 属性自定义编辑控件的文本。
<%@ Page language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>CommandField Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>CommandField Example</h3>
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSqlDataSource"
autogeneratecolumns="false"
datakeynames="CustomerID"
runat="server">
<columns>
<asp:commandfield showeditbutton="true"
edittext="Edit Customer"
canceltext="Discard"
updatetext="Revise"
headertext="Edit Controls"/>
<asp:boundfield datafield="CustomerID"
headertext="Customer ID" />
<asp:boundfield datafield="CompanyName"
headertext="Company Name"/>
<asp:boundfield datafield="Address"
headertext="Address"/>
<asp:boundfield datafield="City"
headertext="City"/>
<asp:boundfield datafield="PostalCode"
headertext="ZIP Code"/>
<asp:boundfield datafield="Country"
headertext="Country"/>
</columns>
</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]"
updatecommand="Update Customers Set CompanyName=@CompanyName, Address=@Address, City=@City, PostalCode=@PostalCode, Country=@Country Where (CustomerID = @CustomerID)"
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">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>CommandField Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>CommandField Example</h3>
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSqlDataSource"
autogeneratecolumns="false"
datakeynames="CustomerID"
runat="server">
<columns>
<asp:commandfield showeditbutton="true"
edittext="Edit Customer"
canceltext="Discard"
updatetext="Revise"
headertext="Edit Controls"/>
<asp:boundfield datafield="CustomerID"
headertext="Customer ID" />
<asp:boundfield datafield="CompanyName"
headertext="Company Name"/>
<asp:boundfield datafield="Address"
headertext="Address"/>
<asp:boundfield datafield="City"
headertext="City"/>
<asp:boundfield datafield="PostalCode"
headertext="ZIP Code"/>
<asp:boundfield datafield="Country"
headertext="Country"/>
</columns>
</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]"
updatecommand="Update Customers Set CompanyName=@CompanyName, Address=@Address, City=@City, PostalCode=@PostalCode, Country=@Country Where (CustomerID = @CustomerID)"
connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server">
</asp:sqldatasource>
</form>
</body>
</html>
注解
类 CommandField 是数据绑定控件 ((如 GridView 和 DetailsView) )使用的特殊字段,用于显示执行删除、编辑、插入或选择操作的命令按钮。 可以使用下表中显示的属性显示或隐藏用于执行这些操作的命令按钮。
| 属性 | 说明 |
|---|---|
| ShowDeleteButton | 在字段中显示或隐藏数据绑定控件中每条 CommandField 记录的“删除”按钮。 “删除”按钮允许用户从数据源中删除记录。 |
| ShowEditButton | 在字段中显示或隐藏数据绑定控件中每条 CommandField 记录的“编辑”按钮。 “编辑”按钮允许用户编辑数据源中的记录。 当用户单击特定记录的“编辑”按钮时,该“编辑”按钮将替换为“更新”按钮和“取消”按钮。 所有其他命令按钮也处于隐藏状态。 |
| ShowInsertButton | 显示或隐藏字段中的“新建”按钮 CommandField 。 “新建”按钮允许用户在数据源中插入新记录。 当用户单击“新建”按钮时,它将替换为“插入”按钮和“取消”按钮。 所有其他命令按钮也处于隐藏状态。 注意: 此属性仅适用于支持插入操作的数据绑定控件,例如 控件 DetailsView 。 |
| ShowSelectButton | 在字段中显示或隐藏数据绑定控件中每条 CommandField 记录的“选择”按钮。 “选择”按钮允许用户选择数据绑定控件中的记录。 |
此外,可以通过设置 ShowCancelButton 属性来显示或隐藏记录处于编辑或插入模式时显示的“取消”按钮。
注意
当数据绑定控件与数据源控件 ((例如 SqlDataSource 控件) )结合使用时,数据绑定控件可以利用数据源控件的功能并提供自动删除、更新和插入功能。 对于其他数据源,需要提供例程,以便在数据绑定控件的相应事件期间执行这些操作。
字段 CommandField 的显示方式有所不同,具体取决于使用该字段的数据绑定控件。 例如,控件将 GridView 字段显示为 CommandField 列,而 DetailsView 控件将字段显示为行。
若要指定要显示的按钮的类型,请使用 ButtonType 属性。 当 属性 ButtonType 设置为 ButtonType.Button 或 ButtonType.Link时,可以通过设置下表中显示的属性来指定要为按钮显示的文本。
| 属性 | 说明 |
|---|---|
| CancelText | “取消”按钮的标题。 |
| DeleteText | “删除”按钮的标题。 |
| EditText | “编辑”按钮的标题。 |
| InsertText | “插入”按钮的标题。 注意: 此属性仅适用于支持插入操作的数据绑定控件,例如 DetailsView 控件。 |
| NewText | “新建”按钮的标题。 注意: 此属性仅适用于支持插入操作的数据绑定控件,例如 DetailsView 控件。 |
| SelectText | “选择”按钮的标题。 |
| UpdateText | “更新”按钮的标题。 |
可以通过将 属性设置为 ButtonTypeButtonType.Image ,然后设置下表中显示的属性来显示图像按钮,而不是显示命令按钮或链接按钮。
| 属性 | 说明 |
|---|---|
| CancelImageUrl | 要为“取消”按钮显示的图像 |
| DeleteImageUrl | 要为“删除”按钮显示的图像。 |
| EditImageUrl | 要为“编辑”按钮显示的图像。 |
| InsertText | 要为“插入”按钮显示的图像。 注意: 此属性仅适用于支持插入操作的数据绑定控件,例如 DetailsView 控件。 |
| NewImageUrl | 要为“新建”按钮显示的图像。 注意: 此属性仅适用于支持插入操作的数据绑定控件,例如 DetailsView 控件。 |
| SelectImageUrl | 要为“选择”按钮显示的图像。 |
| UpdateImageUrl | 要为“更新”按钮显示的图像。 |
默认情况下,当用户单击字段中的按钮 CommandField 时,对页面上的所有验证控件执行验证。 若要防止在单击按钮时进行验证,请将 CausesValidation 属性设置为 false。
可以通过将 属性设置为 Visiblefalse来隐藏CommandField数据绑定控件中的字段。
字段 CommandField 允许你自定义其页眉和页脚部分。 若要在页眉或页脚部分显示标题,请分别设置 HeaderText 或 FooterText 属性。 可以通过设置 HeaderImageUrl 属性来显示图像,而不是在页眉部分显示文本。 若要隐藏 对象中的 CommandField 标头部分,请将 ShowHeader 属性设置为 false。
注意
某些数据绑定控件 ((如 GridView 控件) )只能显示或隐藏控件的整个标头部分。 这些数据绑定控件不支持 ShowHeader 单个按钮字段的 属性。 若要显示或隐藏数据绑定控件 (的整个标头部分(如果可用) ),请使用 控件的 ShowHeader 属性。
还可以通过设置字段的不同部分的 CommandField 样式属性,自定义对象的外观 (字体颜色、背景色等) 。 下表列出了不同的样式属性。
| Style 属性 | 说明 |
|---|---|
| ControlStyle | 对象的子 Web 服务器控件的 CommandField 样式设置。 |
| FooterStyle | 对象的页脚部分的 CommandField 样式设置。 |
| HeaderStyle | 对象的标头部分的 CommandField 样式设置。 |
| ItemStyle | 对象中数据项的 CommandField 样式设置。 |
构造函数
| CommandField() |
初始化 CommandField 类的新实例。 |
属性
| AccessibleHeaderText |
获取或设置某些控件中呈现为 |
| ButtonType |
获取或设置要在按钮字段中显示的按钮类型。 (继承自 ButtonFieldBase) |
| CancelImageUrl |
获取或设置为 CommandField 字段中的“取消”按钮显示的图像的 URL。 |
| CancelText |
获取或设置 CommandField 字段中显示的“取消”按钮的标题。 |
| CausesValidation |
获取或设置一个值,该值指示当用户单击 CommandField 字段中的按钮时是否执行验证。 |
| Control |
获取对数据控件的引用,该控件与 DataControlField 对象关联。 (继承自 DataControlField) |
| ControlStyle |
获取 DataControlField 对象所包含的任何 Web 服务器控件的样式。 (继承自 DataControlField) |
| DeleteImageUrl |
获取或设置为 CommandField 字段中的“删除”按钮显示的图像的 URL。 |
| DeleteText |
获取或设置 CommandField 字段中的“删除”按钮的标题。 |
| DesignMode |
获取一个值,该值指示数据控件字段当前是否在设计时环境中进行查看。 (继承自 DataControlField) |
| EditImageUrl |
获取或设置为 CommandField 字段中的“编辑”按钮显示的图像的 URL。 |
| EditText |
获取或设置 CommandField 字段中的“编辑”按钮的标题。 |
| FooterStyle |
获取或设置数据控件字段脚注的样式。 (继承自 DataControlField) |
| FooterText |
获取或设置数据控件字段的脚注项中显示的文本。 (继承自 DataControlField) |
| HeaderImageUrl |
获取或设置数据控件字段的标题项中显示的图像的 URL。 (继承自 DataControlField) |
| HeaderStyle |
获取或设置数据控件字段标头的样式。 (继承自 DataControlField) |
| HeaderText |
获取或设置数据控件字段的标题项中显示的文本。 (继承自 DataControlField) |
| InsertImageUrl |
获取或设置为 CommandField 字段中的“插入”按钮显示的图像的 URL。 |
| InsertText |
获取或设置 CommandField 字段中的“插入”按钮的标题。 |
| InsertVisible |
获取一个值,该值指示 DataControlField 对象在其父级数据绑定控件处于插入模式时是否可见。 (继承自 DataControlField) |
| IsTrackingViewState |
获取一个值,该值指示 DataControlField 对象是否保存对其视图状态的更改。 (继承自 DataControlField) |
| ItemStyle |
获取由数据控件字段显示的任何基于文本的内容的样式。 (继承自 DataControlField) |
| NewImageUrl |
获取或设置为 CommandField 字段中的“新建”按钮显示的图像的 URL。 |
| NewText |
获取或设置 CommandField 字段中的“新建”按钮的标题。 |
| SelectImageUrl |
获取或设置为 CommandField 字段中的“选择”按钮显示的图像的 URL。 |
| SelectText |
获取或设置 CommandField 字段中的“选择”按钮的标题。 |
| ShowCancelButton |
获取或设置一个值,该值指示是否在 CommandField 字段中显示“取消”按钮。 |
| ShowDeleteButton |
获取或设置一个值,该值指示是否在 CommandField 字段中显示“删除”按钮。 |
| ShowEditButton |
获取或设置一个值,该值指示是否在 CommandField 字段中显示“编辑”按钮。 |
| ShowHeader |
获取或设置一个值,该值指示是否显示 ButtonFieldBase 对象的标头部分。 (继承自 ButtonFieldBase) |
| ShowInsertButton |
获取或设置一个值,该值指示是否在 CommandField 字段中显示“新建”按钮。 |
| ShowSelectButton |
获取或设置一个值,该值指示是否在 CommandField 字段中显示“选择”按钮。 |
| SortExpression |
获取或设置数据源控件用来对数据进行排序的排序表达式。 (继承自 DataControlField) |
| UpdateImageUrl |
获取或设置为 CommandField 字段中的“更新”按钮显示的图像的 URL。 |
| UpdateText |
获取或设置 CommandField 字段中的“更新”按钮的标题。 |
| ValidateRequestMode |
获取或设置一个值,该值指定该控件是否验证客户端输入。 (继承自 DataControlField) |
| ValidationGroup |
获取或设置在单击 ButtonFieldBase 对象中的按钮时要验证的验证控件组的名称。 (继承自 ButtonFieldBase) |
| ViewState |
获取状态信息的字典,这些信息使您可以在同一页的多个请求间保存和还原 DataControlField 对象的视图状态。 (继承自 DataControlField) |
| Visible |
获取或设置指示是否呈现数据控件字段的值。 (继承自 DataControlField) |
方法
显式接口实现
| IDataSourceViewSchemaAccessor.DataSourceViewSchema |
获取或设置与此 DataControlField 对象关联的架构。 (继承自 DataControlField) |
| IStateManager.IsTrackingViewState |
获取一个值,该值指示 DataControlField 对象是否保存对其视图状态的更改。 (继承自 DataControlField) |
| IStateManager.LoadViewState(Object) |
将数据控件字段还原为保存过的前一视图状态。 (继承自 DataControlField) |
| IStateManager.SaveViewState() |
保存在页回发到服务器后对 DataControlField 视图状态所做的更改。 (继承自 DataControlField) |
| IStateManager.TrackViewState() |
使 DataControlField 对象跟踪对其视图状态所做的更改,以便这些更改可以存储在控件的 ViewState 属性中并且能够在同一页的不同请求间得以保持。 (继承自 DataControlField) |
适用于
另请参阅
- GridView
- Columns
- DetailsView
- Fields
- BoundField
- ButtonField
- ButtonFieldBase
- CheckBoxField
- DataControlField
- HyperLinkField
- TemplateField
- ButtonType
- CancelText
- DeleteImageUrl
- DeleteText
- EditImageUrl
- EditText
- InsertImageUrl
- InsertText
- NewImageUrl
- NewText
- SelectImageUrl
- SelectText
- ShowCancelButton
- ShowDeleteButton
- ShowEditButton
- ShowInsertButton
- ShowSelectButton
- UpdateImageUrl
- UpdateText
- ControlStyle
- FooterStyle
- HeaderStyle
- ItemStyle