CommandField.CausesValidation 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置一个值,该值指示当用户单击 CommandField 字段中的按钮时是否执行验证。
public:
virtual property bool CausesValidation { bool get(); void set(bool value); };
public override bool CausesValidation { get; set; }
member this.CausesValidation : bool with get, set
Public Overrides Property CausesValidation As Boolean
属性值
如果在用户单击 CommandField 字段中的按钮时执行验证,则为 true
,否则为 false
。 默认值为 true
。
示例
下面的代码示例演示如何使用 CausesValidation 属性来防止在单击字段中的 CommandField 按钮时发生验证。
重要
此示例具有一个接受用户输入的文本框,这是一个潜在的安全威胁。 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。 有关详细信息,请参阅脚本侵入概述。
<%@ 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 CausesValidation Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>CommandField CausesValidation Example</h3>
<!-- Normally, the validation controls declared in the -->
<!-- EditItemTemplate of each TemplateField field column would -->
<!-- prevent the user from leaving a field value empty; however, -->
<!-- because the CausesValidation property of the CommandField -->
<!-- field column is set to false, validation does not occur when -->
<!-- the user clicks a button in the CommandField field column. -->
<asp:gridview id="CustomerGridView"
datasourceid="CustomersSqlDataSource"
autogeneratecolumns="false"
datakeynames="CustomerID"
cellpadding="10"
runat="server">
<columns>
<asp:commandfield showeditbutton="true"
causesvalidation="false"
headertext="Edit"/>
<asp:boundfield datafield="CustomerID"
headertext="Customer ID"
readonly="true"/>
<asp:templatefield headertext="Company Name"
itemstyle-verticalalign="Top">
<itemtemplate>
<%#Eval("CompanyName")%>
</itemtemplate>
<edititemtemplate>
<asp:textbox id="CompanyNameTextBox"
text='<%#Bind("CompanyName")%>'
width="90"
runat="server"/>
<br/>
<asp:requiredfieldvalidator id="CompanyNameRequiredValidator"
controltovalidate="CompanyNameTextBox"
display="Dynamic"
text="Please enter the company name."
runat="server"/>
</edititemtemplate>
</asp:templatefield>
<asp:templatefield headertext="Address"
itemstyle-verticalalign="Top">
<itemtemplate>
<%#Eval("Address")%>
</itemtemplate>
<edititemtemplate>
<asp:textbox id="AddressTextBox"
text='<%#Bind("Address")%>'
width="90"
runat="server"/>
<br/>
<asp:requiredfieldvalidator id="AddressRequiredValidator"
controltovalidate="AddressTextBox"
display="Dynamic"
text="Please enter the address."
runat="server"/>
</edititemtemplate>
</asp:templatefield>
<asp:templatefield headertext="City"
itemstyle-verticalalign="Top">
<itemtemplate>
<%#Eval("City")%>
</itemtemplate>
<edititemtemplate>
<asp:textbox id="CityTextBox"
text='<%#Bind("City")%>'
width="90"
runat="server"/>
<br/>
<asp:requiredfieldvalidator id="CityRequiredValidator"
controltovalidate="CityTextBox"
display="Dynamic"
text="Please enter the city."
runat="server"/>
</edititemtemplate>
</asp:templatefield>
</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] From [Customers]"
updatecommand="Update [Customers] Set [CompanyName]=@CompanyName, [Address]=@Address, [City]=@City 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 CausesValidation Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>CommandField CausesValidation Example</h3>
<!-- Normally, the validation controls declared in the -->
<!-- EditItemTemplate of each TemplateField field column would -->
<!-- prevent the user from leaving a field value empty; however, -->
<!-- because the CausesValidation property of the CommandField -->
<!-- field column is set to false, validation does not occur when -->
<!-- the user clicks a button in the CommandField field column. -->
<asp:gridview id="CustomerGridView"
datasourceid="CustomersSqlDataSource"
autogeneratecolumns="false"
datakeynames="CustomerID"
cellpadding="10"
runat="server">
<columns>
<asp:commandfield showeditbutton="true"
causesvalidation="false"
headertext="Edit"/>
<asp:boundfield datafield="CustomerID"
headertext="Customer ID"
readonly="true"/>
<asp:templatefield headertext="Company Name"
itemstyle-verticalalign="Top">
<itemtemplate>
<%#Eval("CompanyName")%>
</itemtemplate>
<edititemtemplate>
<asp:textbox id="CompanyNameTextBox"
text='<%#Bind("CompanyName")%>'
width="90"
runat="server"/>
<br/>
<asp:requiredfieldvalidator id="CompanyNameRequiredValidator"
controltovalidate="CompanyNameTextBox"
display="Dynamic"
text="Please enter the company name."
runat="server"/>
</edititemtemplate>
</asp:templatefield>
<asp:templatefield headertext="Address"
itemstyle-verticalalign="Top">
<itemtemplate>
<%#Eval("Address")%>
</itemtemplate>
<edititemtemplate>
<asp:textbox id="AddressTextBox"
text='<%#Bind("Address")%>'
width="90"
runat="server"/>
<br/>
<asp:requiredfieldvalidator id="AddressRequiredValidator"
controltovalidate="AddressTextBox"
display="Dynamic"
text="Please enter the address."
runat="server"/>
</edititemtemplate>
</asp:templatefield>
<asp:templatefield headertext="City"
itemstyle-verticalalign="Top">
<itemtemplate>
<%#Eval("City")%>
</itemtemplate>
<edititemtemplate>
<asp:textbox id="CityTextBox"
text='<%#Bind("City")%>'
width="90"
runat="server"/>
<br/>
<asp:requiredfieldvalidator id="CityRequiredValidator"
controltovalidate="CityTextBox"
display="Dynamic"
text="Please enter the city."
runat="server"/>
</edititemtemplate>
</asp:templatefield>
</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] From [Customers]"
updatecommand="Update [Customers] Set [CompanyName]=@CompanyName, [Address]=@Address, [City]=@City Where [CustomerID] = @CustomerID"
connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server">
</asp:sqldatasource>
</form>
</body>
</html>
注解
CausesValidation使用 属性指定是否在单击字段中的按钮CommandField时执行验证。 当此属性设置为 true
时,默认情况下会验证页面上的所有验证控件。 若要将验证限制为仅特定组验证控件,请创建一个验证组, ValidationGroup 然后将 属性设置为验证组的名称。 有关验证组的详细信息,请参阅 BaseValidator.ValidationGroup。