Button..::.CausesValidation 属性

获取或设置一个值,该值指示在单击 Button 控件时是否执行验证。

ASP.NET
<
属性值
类型: System..::.Boolean

如果在单击 Button 控件时执行验证,则为 true;否则为 false。默认值为 true

实现

IButtonControl..::.CausesValidation

<!---->

默认情况下,单击 Button 控件时执行页验证。页验证确定页上与验证控件关联的输入控件是否均通过该验证控件所指定的验证规则。

通过使用 CausesValidation 属性,可以指定或确定当单击 Button 控件时,是否同时在客户端和服务器上执行验证。若要禁止执行验证,请将 CausesValidation 属性设置为 false

ctde5wf3.alert_note(zh-cn,VS.90).gif说明:

当使用 PostBackUrl 属性回发至不同页面时,应将 CausesValidation 属性设置为 false。在回发至不同页面时,应对验证进行显式检查。有关示例,请参见 PostBackUrl 属性的“备注”部分。

对于 resetclear 按钮,此属性通常设置为 false,以防止在单击其中某个按钮时执行验证。

CausesValidation 属性的值设置为 true 时,还可以使用 ValidationGroup 属性来指定 Button 控件引发验证时所对应的验证组的名称。

无法通过主题或样式表主题设置此属性。有关更多信息,请参见 ThemeableAttributeASP.NET 主题和外观概述

<!---->

下面的代码示例演示如何使用 CausesValidation 属性防止发生页验证。注意,Validate 方法会单独激活各个验证控件。

<%@ Page Language="C#" AutoEventWireup="True" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >

<head id="Head1" runat="server">
<title> Button CausesValidation Example </title>
<script runat="server">

void SubmitButton_Click(Object sender, EventArgs e)
{

// Determine which button was clicked.
switch(((Button)sender).ID)
{

case "CityQueryButton":

// Validate only the controls used for the city query.
CityReqValidator.Validate();

// Take the appropriate action if the controls pass validation.
if (CityReqValidator.IsValid)
{
Message.Text = "You have chosen to run a query for the following city: " +
CityTextBox.Text;
}

break;

case "StateQueryButton":

// Validate only the controls used for the state query.
StateReqValidator.Validate();

// Take the appropriate action if the controls pass validation.
if (StateReqValidator.IsValid)
{
Message.Text = "You have chosen to run a query for the following state: " +
StateTextBox.Text;
}

break;

default:

// If the button clicked isn't recognized, erase the message on the page.
Message.Text = "";

break;

}

}

</script>

</head>

<body>

<form id="form1" runat="server">

<h3> Button CausesValidation Example </h3>

<table border="1" cellpadding="10">
<tr>
<td>
<b>Enter city to query.</b> <br />
<asp:TextBox ID="CityTextBox"
runat="server"/>
<asp:RequiredFieldValidator ID="CityReqValidator"
ControlToValidate="CityTextBox"
ErrorMessage="<br />Please enter a city."
Display="Dynamic"
EnableClientScript="False"
runat="server"/>
</td>
<td valign="bottom">
<asp:Button ID="CityQueryButton"
Text="Submit"
CausesValidation="False"
OnClick="SubmitButton_Click"
runat="server"/>
</td>
</tr>

<tr>
<td>
<b>Enter state to query.</b> <br />
<asp:TextBox ID="StateTextBox"
runat="server"/>
<asp:RequiredFieldValidator ID="StateReqValidator"
ControlToValidate="StateTextBox"
ErrorMessage="<br />Please enter a state."
Display="Dynamic"
EnableClientScript="False"
runat="server"/>
</td>
<td valign="bottom">
<!-- 这里把 CausesValidation="false" 默认时为true 会对整个页面验证 (就是验证标签所做的验证) 这里如果没有OnClick="SubmitButton_Click" 则不会执行验证-->
<asp:Button ID="StateQueryButton"
Text="Submit"
CausesValidation="false"
OnClick="SubmitButton_Click"
runat="server"/>
</td>
</tr>

</table>

<br /><br />

<asp:Label ID="Message"
runat="Server"/>

</form>

</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值