PostBackOptions.ValidationGroup 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置一个控件组,当该控件组回发到服务器时,PostBackOptions 将引发对它的验证。
public:
property System::String ^ ValidationGroup { System::String ^ get(); void set(System::String ^ value); };
public string ValidationGroup { get; set; }
member this.ValidationGroup : string with get, set
Public Property ValidationGroup As String
属性值
一个控件组,当控件组回发到服务器时, PostBackOptions 对象将引发对它的验证。 默认值为空字符串 ("")。
示例
下面的代码示例演示如何使用 ValidationGroup 和 PerformValidation 属性来确保两个 TextBox 控件在生成回发事件之前在它们中输入了信息。
NameTextBox
和 AgeTextBox
控件是验证组的PersonalInfoGroup
一部分,但StateTextBox
控件不是。 因此,在生成回发事件之前,用户不必在 StateTextBox
控件中输入任何数据。
重要
此示例具有一个接受用户输入的文本框,这是一个潜在的安全威胁。 默认情况下,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">
<script runat="server">
void Page_Init(object sender, EventArgs e)
{
PostBackOptions myPostBackOptions = new PostBackOptions(FruitRadioButtonList);
myPostBackOptions.RequiresJavaScriptProtocol = true;
myPostBackOptions.PerformValidation = true;
myPostBackOptions.ValidationGroup = "PersonalInfoGroup";
string reference = Page.ClientScript.GetPostBackEventReference(myPostBackOptions);
FruitRadioButtonList.Attributes.Add("onclick", reference);
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>PerformValidation and ValidationGroup Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>PostBackOptions PerformValidation and ValidationGroup Example</h3>
<asp:label id="NameLabel"
text="Enter your name:"
runat="server"
AssociatedControlID="NameTextBox">
</asp:label>
 
<asp:textbox id="NameTextBox"
runat="server">
</asp:textbox>
 
<asp:requiredfieldvalidator id="RequiredFieldValidator1"
controltovalidate="NameTextBox"
validationgroup="PersonalInfoGroup"
errormessage="Enter your name."
runat="server">
</asp:requiredfieldvalidator>
<br /><br />
<asp:label id="AgeLabel"
text="Enter your age:"
runat="server"
AssociatedControlID="AgeTextBox">
</asp:label>
 
<asp:textbox id="AgeTextBox"
runat="server">
</asp:textbox>
 
<asp:requiredfieldvalidator id="RequiredFieldValidator2"
controltovalidate="AgeTextBox"
validationgroup="PersonalInfoGroup"
errormessage="Enter your age."
runat="server">
</asp:requiredfieldvalidator>
<br /><br />
<asp:label id="State"
text="Enter the state where you live:"
runat="server"
AssociatedControlID="StateTextBox">
</asp:label>
 
<asp:textbox id="StateTextBox"
runat="server">
</asp:textbox>
 
<br /><br />
<asp:Label id="FruitLabel"
text="Please select your preferred fruit:"
runat="server"
AssociatedControlID="FruitRadioButtonList">
</asp:Label>
<asp:RadioButtonList ID="FruitRadioButtonList" runat="server" >
<asp:ListItem>Apples</asp:ListItem>
<asp:ListItem>Oranges</asp:ListItem>
<asp:ListItem>Pears</asp:ListItem>
<asp:ListItem>Peaches</asp:ListItem>
<asp:ListItem>Grapes</asp:ListItem>
<asp:ListItem>Lemons</asp:ListItem>
<asp:ListItem>Limes</asp:ListItem>
<asp:ListItem>Plums</asp:ListItem>
</asp:RadioButtonList>
</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">
<script runat="server">
Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs)
Dim myPostBackOptions As PostBackOptions = New PostBackOptions(FruitRadioButtonList)
myPostBackOptions.RequiresJavaScriptProtocol = True
myPostBackOptions.PerformValidation = True
myPostBackOptions.ValidationGroup = "PersonalInfoGroup"
Dim reference As String = Page.ClientScript.GetPostBackEventReference(myPostBackOptions)
FruitRadioButtonList.Attributes.Add("onclick", reference)
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>PerformValidation and ValidationGroup Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>PostBackOptions PerformValidation and ValidationGroup Example</h3>
<asp:label id="NameLabel"
text="Enter your name:"
runat="server"
AssociatedControlID="NameTextBox">
</asp:label>
 
<asp:textbox id="NameTextBox"
runat="server">
</asp:textbox>
 
<asp:requiredfieldvalidator id="RequiredFieldValidator1"
controltovalidate="NameTextBox"
validationgroup="PersonalInfoGroup"
errormessage="Enter your name."
runat="server">
</asp:requiredfieldvalidator>
<br /><br />
<asp:label id="AgeLabel"
text="Enter your age:"
runat="server"
AssociatedControlID="AgeTextBox">
</asp:label>
 
<asp:textbox id="AgeTextBox"
runat="server">
</asp:textbox>
 
<asp:requiredfieldvalidator id="RequiredFieldValidator2"
controltovalidate="AgeTextBox"
validationgroup="PersonalInfoGroup"
errormessage="Enter your age."
runat="server">
</asp:requiredfieldvalidator>
<br /><br />
<asp:label id="State"
text="Enter the state where you live:"
runat="server"
AssociatedControlID="State">
</asp:label>
 
<asp:textbox id="StateTextBox"
runat="server">
</asp:textbox>
 
<br /><br />
<asp:Label id="FruitLabel"
text="Please select your preferred fruit:"
runat="server"
AssociatedControlID="FruitRadioButtonList">
</asp:Label>
<asp:RadioButtonList ID="FruitRadioButtonList" runat="server" >
<asp:ListItem>Apples</asp:ListItem>
<asp:ListItem>Oranges</asp:ListItem>
<asp:ListItem>Pears</asp:ListItem>
<asp:ListItem>Peaches</asp:ListItem>
<asp:ListItem>Grapes</asp:ListItem>
<asp:ListItem>Lemons</asp:ListItem>
<asp:ListItem>Limes</asp:ListItem>
<asp:ListItem>Plums</asp:ListItem>
</asp:RadioButtonList>
</form>
</body>
</html>
注解
ValidationGroup使用 属性指定验证组,以在生成回发事件时进行验证。 仅验证指定验证组中的验证控件。