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 Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。 如需詳細資訊,請參閱 Script Exploits Overview (指令碼攻擊概觀)。
<%@ 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使用 屬性來指定驗證群組,以驗證何時產生回傳事件。 只會驗證指定之驗證群組中的驗證控制件。