LinkButton.CausesValidation 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置一个值,该值指示在单击 LinkButton 控件时是否执行验证。
public:
property bool CausesValidation { bool get(); void set(bool value); };
public:
virtual property bool CausesValidation { bool get(); void set(bool value); };
[System.ComponentModel.Bindable(false)]
public bool CausesValidation { get; set; }
[System.Web.UI.Themeable(false)]
public virtual bool CausesValidation { get; set; }
[<System.ComponentModel.Bindable(false)>]
member this.CausesValidation : bool with get, set
[<System.Web.UI.Themeable(false)>]
member this.CausesValidation : bool with get, set
Public Property CausesValidation As Boolean
Public Overridable Property CausesValidation As Boolean
属性值
如果在单击 LinkButton 控件后执行验证,则为 true
;否则为 false
。 默认值是 true
。
实现
- 属性
示例
以下示例演示如何使用 CausesValidation 属性来防止发生页面验证。 请注意 方法 Validate 如何独立激活每个验证控件。
重要
此示例具有一个接受用户输入的文本框,这是一个潜在的安全威胁。 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。 有关详细信息,请参阅脚本侵入概述。
<%@ 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 xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title> LinkButton CausesValidation Example </title>
<script runat="server">
void SubmitButton_Click(Object sender, EventArgs e)
{
// Determine which button was clicked.
switch(((LinkButton)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> LinkButton 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:LinkButton 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">
<asp:LinkButton 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>
<%@ Page Language="VB" AutoEventWireup="True" %>
<!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> LinkButton CausesValidation Example </title>
<script runat="server">
Sub SubmitButton_Click(sender As Object, e As EventArgs)
' Determine which button was clicked.
Select Case (CType(sender, LinkButton)).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 Then
Message.Text = "You have chosen to run a query for the following city: " & _
CityTextBox.Text
End If
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 Then
Message.Text = "You have chosen to run a query for the following state: " & _
StateTextBox.Text
End If
Case Else
' If the button clicked isn't recognized, erase the message on the page.
Message.Text = ""
End Select
End Sub
</script>
</head>
<body>
<form id="form1" runat="server">
<h3> LinkButton 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:LinkButton 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">
<asp:LinkButton 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>
注解
默认情况下,在单击控件时 LinkButton 执行页面验证。 页面验证确定与页面上的验证控件关联的输入控件是否都通过验证控件指定的验证规则。
可以使用 属性指定 (或确定) 是否同时CausesValidation在客户端和服务器上LinkButton执行验证。 若要防止执行验证,请将 CausesValidation 属性设置为 false
。
对于“重置”或“清除”按钮,此属性通常设置为 false
,以防止在单击该按钮时执行验证。
当 属性的值 CausesValidation 设置为 true
时,还可以使用 ValidationGroup 属性指定控件导致验证的验证组 LinkButton 的名称。
无法通过主题或样式表主题设置此属性。 有关详细信息,请参阅 ThemeableAttribute 和 ASP.NET 主题和皮肤。