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

解决方案 »

  1.   

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

  2.   

    1. 设置button的CausesValidation=false
    2. 验证用户名重名要回到服务器端验证,当然要回传了
       不能这么验证。
       如果要不回传,可以自己写验证控件或者用ajax
      

  3.   

    第一个问题,可以设另外那个按钮的验证属性为False
      

  4.   

    CustomValidator控件不就是用来验证重名或数据长度限制的吗?
      

  5.   

    有两点:
    1.确定CustomValidator和你的提交Button在一个组里
    2.验证是否用户名存在必须回传访问数据库.验证和数据的提交是同时的,所以会出现提交完成错误同时出来的情况.
       解决如下:在你的访问数据库的方法里加一个if 语句,如下:
      if(CustomValidator.isvaisd)
    {
    sqlconnecton new...
    //你连接数据库的东西.
    }
    else
    {
    CustomValidator.errroMessage="用户名已存在!";
    }
    如果CustomValidator访问数据库的数据在同一方法中,方法如下:
    在你的Command("insert ....")语句前进if 判断就行.