如何在asp.net中对验证进行分组

时间:2013-09-20 12:20:46

标签: asp.net validation

我有一个网页,其中包含注册表和登录表,用户可以在其中注册或登录。

我的问题是当我尝试在登录文本框上注册验证时停止请求进入服务器。此外,当我从该页面登录时,注册表单上的验证控件将控制停止请求转到服务器。我该怎么办?

我曾尝试使用更新面板,但这不起作用。

这是我的代码:

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <table width="400px" style="height:30px">
            <tr>
                <td class="style3" colspan="2">
                    <div class="register-bg">
                        <div class="padl20 padt5">
                            <span style="color:#2D432C">Registration Here</span>
                        </div>
                    </div>
                </td>
            </tr>
        </table>
        <table id="hpreg" style="height:380px; width: 358px;">
            <tr >
                <td style="width: 159px">
                    <b>Full Name</b>
                </td>
                <td style="width: 187px" class="wdth180">
                    <asp:TextBox ID="txt_nam" runat="server" Width="170px" CssClass="textfield"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
                    ControlToValidate="txt_nam" ErrorMessage="Please enter Name" ForeColor="Red">*</asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td style="width: 159px">
                    <b> Father Name</b>
                </td>
                <td class="wdth180" style="width: 187px">
                    <asp:TextBox ID="txt_fnam" runat="server" Width="170px" 
                    CssClass="textfield"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
                    ControlToValidate="txt_fnam" ErrorMessage="Please enter father name" 
                    ForeColor="Red">*</asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td style="width: 159px">
                    <b> Gender</b>
                </td>
                <td class="wdth180" style="width: 187px">
                    <asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatColumns="2" 
                    ToolTip="Enter Your Gender" Width="160px" CssClass="radio wdth130">
                    <asp:ListItem Value="M">Male</asp:ListItem>
                    <asp:ListItem Value="F">Female</asp:ListItem>
                </asp:RadioButtonList>
                </td>
            </tr>

.....

并在同一页面上登录html:

<asp:Content ID="Content2" runat="server" 
contentplaceholderid="ContentPlaceHolder2">
    <table width="240px" >
        <tr style="height:28px;">
            <td>
                <b><asp:TextBox runat="server"  ID="TextBox1" CssClass="textfield"/></b>
            </td>
            <td>
                <asp:TextBox runat="server" ID="TextBox2" TextMode="Password" CssClass="textfield"  />
            </td>
        </tr>
        <tr style="height:28px;">
            <td><asp:LinkButton ID="LinkButton1" runat="server">Forget Password</asp:LinkButton>
            </td>
            <td> <asp:Button ID="btn_login" CssClass="medimum-btn wdth45" runat="server" Text="Login" onclick="btn_login_Click" />
            </td>
        </tr>
        <tr style="height:28px;">
            <td></td>
            <td></td>
        </tr>
    </table>
    <div class="fleft">
    </div>
    <br />
    <div class="fleft padl5">
    </div>
    <div class="fleft padl5">
        <br />  
    </div>
</asp:Content>

2 个答案:

答案 0 :(得分:3)

您需要在验证器和摘要中使用ValidationGroup属性。

创建两个组,一个用于登录,另一个用于注册。

这是一些未经测试的代码,可以给你一个想法......

<asp:Panel runat="server" id="pnlLogon" DefaultButton="btnLogon">
  <asp:ValidationSummary runat="server" id="sumLogon"
    ValidationGroup="valLogonGroup" />
  Username: <asp:TextBox runat="server" id="txtUsername" />
  <asp:RequiredFieldValidator runat="server" ControlToValidate="txtUsername"
    ValidationGroup="valLogonGroup" ErrorMessage="Provide Username"/><br/>
  Password: <asp:TextBox runat="server" id="txtPassword" />
  <asp:RequiredFieldValidator runat="server" ControlToValidate="txtPassword"
    ValidationGroup="valLogonGroup" ErrorMessage="Provide Password"/><br/>
  <asp:Button runat="server" id="btnLogon" ValidationGroup="valLogonGroup"
    Text="Logon"/>
</asp:Panel>
<asp:Panel runat="server" id="pnlRegister" DefaultButton="btnRegister">
  <asp:ValidationSummary runat="server" id="sumRegister"
    ValidationGroup="valRegisterGroup" />
  Full Name: <asp:TextBox runat="server" id="txtName" />
  <asp:RequiredFieldValidator runat="server" ControlToValidate="txtName"
    ValidationGroup="valRegisterGroup" ErrorMessage="Provide Full Name"/><br/>
  Password: <asp:TextBox runat="server" id="txtPassword" />
  <asp:RequiredFieldValidator runat="server" ControlToValidate="txtPassword"
    ValidationGroup="valRegisterGroup" ErrorMessage="Provide Password"/><br/>
  <asp:Button runat="server" id="btnRegister" ValidationGroup="valRgisterGroup"
    Text="Register"/>
</asp:Panel>

我已将它们放入<asp:Panel>以利用DefaultButton。这意味着,如果您在enter的文本框中按return<asp:Panel>键,则会点击正确的<asp:Button> ...并且因为各个按钮已关联对于特定的ValidationGroup,只会触发该组的验证器。

答案 1 :(得分:1)

在这种情况下应使用验证组:

 <table width="240px">
     <tr style="height: 28px;">
         <td>
             <b><asp:TextBox runat="server" ID="TextBox1" CssClass="textfield"  
                             ValidationGroup="loginGrp"/></b>
         </td>
         <td>
             <asp:TextBox runat="server" ID="TextBox2" TextMode="Password" 
                          CssClass="textfield"  ValidationGroup="loginGrp" />
         </td>
     </tr>
    <tr style="height: 28px;">
         <td>
             <asp:LinkButton ID="LinkButton1" CauseValidation="false" 
                             runat="server">Forget Password</asp:LinkButton>
         </td>
         <td>
             <asp:Button ID="btn_login" CssClass="medimum-btn wdth45" runat="server" 
                         Text="Login" ValidationGroup="loginGrp"
                         OnClick="btn_login_Click" />
         </td>
     </tr>