<br /><br /> <!--When Button1 is clicked, only validation controls that are a part of PersonalInfoGroup are validated.--> <asp:button id="Button1" text="Validate" causesvalidation=true validationgroup="PersonalInfoGroup" runat=Server />
<br /><br />
<asp:label id="CityLabel" text="Enter your city of residence:" runat=Server> </asp:label>  
<asp:textbox id="CityTextbox" runat=Server> </asp:textbox>   <asp:requiredfieldvalidator id="RequiredFieldValidator3" controltovalidate="CityTextBox" validationgroup="LocationInfoGroup" errormessage="Enter a city name." runat=Server> </asp:requiredfieldvalidator>
<br /><br /> <!--When Button2 is clicked, only validation controls that are a part of LocationInfoGroup are validated.--> <asp:button id="Button2" text="Validate" causesvalidation=true validationgroup="LocationInfoGroup" runat=Server /> </form> </body> </html>
<head id="Head1" runat="server">
<title>Button.ValidationGroup Example</title>
</head>
<body>
<form id="Form1" runat="server">
<h3>Button.ValidationGroup Example</h3> <asp:label id="NameLabel"
text="Enter your name:"
runat=Server>
</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>
</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 /> <!--When Button1 is clicked, only validation
controls that are a part of PersonalInfoGroup
are validated.-->
<asp:button id="Button1"
text="Validate"
causesvalidation=true
validationgroup="PersonalInfoGroup"
runat=Server />
<br /><br />
<asp:label id="CityLabel"
text="Enter your city of residence:"
runat=Server>
</asp:label>  
<asp:textbox id="CityTextbox"
runat=Server>
</asp:textbox>   <asp:requiredfieldvalidator id="RequiredFieldValidator3"
controltovalidate="CityTextBox"
validationgroup="LocationInfoGroup"
errormessage="Enter a city name."
runat=Server>
</asp:requiredfieldvalidator>
<br /><br /> <!--When Button2 is clicked, only validation
controls that are a part of LocationInfoGroup
are validated.-->
<asp:button id="Button2"
text="Validate"
causesvalidation=true
validationgroup="LocationInfoGroup"
runat=Server /> </form>
</body>
</html>
2. 验证用户名重名要回到服务器端验证,当然要回传了
不能这么验证。
如果要不回传,可以自己写验证控件或者用ajax
1.确定CustomValidator和你的提交Button在一个组里
2.验证是否用户名存在必须回传访问数据库.验证和数据的提交是同时的,所以会出现提交完成错误同时出来的情况.
解决如下:在你的访问数据库的方法里加一个if 语句,如下:
if(CustomValidator.isvaisd)
{
sqlconnecton new...
//你连接数据库的东西.
}
else
{
CustomValidator.errroMessage="用户名已存在!";
}
如果CustomValidator访问数据库的数据在同一方法中,方法如下:
在你的Command("insert ....")语句前进if 判断就行.