日期:2014-05-18  浏览次数:20579 次

CustomValidator的提交问题
我的一个表单上有两个按钮,我希望只有在点击一个按钮的情况下验证表单,另一个则不要验证表单,该怎么设置
还有我在表单中加了个CustomValidator控件检查是否用户名重名,可是点击提交按钮后,提交成功,同时验证失败的信息也显示出来了,这是怎么回事,既然失败了怎么还可以提交啊?

------解决方案--------------------
<%@ page language= "C# " %>

<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>

&nbsp

<asp:textbox id= "NameTextBox "
runat=Server>
</asp:textbox>

&nbsp

<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>

&nbsp

<asp:textbox id= "AgeTextbox "
runat=Server>
</asp:textbox>

&nbsp

<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>

&nbsp

<asp:textbox id= "CityTextbox "
runat=Server>
</asp:textbox>

&nbsp

<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>


------解决方案--------------------
1. 设置button的CausesValidation=false <