在回发事件后保留文本框文本

时间:2014-01-17 08:10:15

标签: c# .net validation textbox

我在一个页面中有3个文本框。其中一个textBox从用户那里获取新密码,并根据用户输入的密码,Label显示密码强度信息。但是在Label显示的消息之后,textBox文本被清除。有没有办法保留文本?我为文本框启用了自动后备,因为我需要使用Comparevalidator。 这是代码段 -

   protected void NewPassEntered(object sender, EventArgs e)
    {
        if (txtPassword.Text.Length < 4)
        {
            lblPassStr.Visible = Visible;
            lblPassStr.BackColor = System.Drawing.Color.OrangeRed;
            lblPassStr.Text = "Password should have more than four characters";
            txtPassword.Text = "";
        }
        else if ((txtPassword.Text.Length > 4) && (txtPassword.Text.Length < 6) && (txtPassword.Text.Contains("@")))
        {
            lblPassStr.Visible = Visible;
            lblPassStr.BackColor = System.Drawing.Color.Green;
            lblPassStr.Text = "Password Strength:Medium";

        }
        else if ((txtPassword.Text.Length > 4) && (txtPassword.Text.Length < 6))
        {
            lblPassStr.Visible = Visible;
            lblPassStr.BackColor = System.Drawing.Color.Yellow;
            lblPassStr.Text = "Password Strength:Weak";
        }
        else if ((txtPassword.Text.Length > 6) && (txtPassword.Text.Contains("@")))
        {
            lblPassStr.Visible = Visible;
            lblPassStr.BackColor = System.Drawing.Color.Blue;
            lblPassStr.Text = "Password Strength:Strong";
        }
    }
}

有没有办法有效地检查多个特殊字符?

aspx代码如下所示:

       <div  style="width:400px; height:250px;border-color:GoldenRod ;border-style:solid;border-width:thin;padding:20px 50px 50px 20px; position:relative; margin:100px 100px; margin-left:344px">

<table border="0" align="center" cellpadding="0" cellspacing="0" width="350" >
                                   <tr><td> <br /></td></tr>
                                    <tr>
                                        <td style="width:200px">
                                           <span class="labeltxt"> Old Password:     </span>
                                          <br /></td></tr><tr> <td style="width:200px"> <asp:TextBox ID="txtoldpass" 
                                                runat="server" CssClass="text" TextMode="Password" Width="300px" 
                                            Height="25" ></asp:TextBox>
                                                 <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="*Enter the old password" ControlToValidate="txtoldpass" Text="*" ForeColor="Red" InitialValue="">

                    </asp:RequiredFieldValidator><br />
                                        </td>

                                       </tr>
                                       <tr>
                                       <td><br /></td>
                                       </tr>

                                       <tr>
                                        <td style="width:200px">
                                            <span>New Password:    </span>
                                           <br /></td></tr><tr>
                                           <td style="width:200px"> 
                                            <asp:TextBox ID="txtPassword" runat="server" TextMode="Password" CssClass="text"  
                                                   Width="300px" Height="25"  
                                                   ontextchanged="NewPassEntered"></asp:TextBox>
                                     <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" Font-Italic="true" ErrorMessage="**Enter the new password!" ControlToValidate="txtPassword" Text="**" ForeColor="Red" InitialValue="">

                    </asp:RequiredFieldValidator>
                                        <br /></td>
                                    </tr>
                                    <tr>
                                    <td><br />
                                        <asp:Label ID="lblPassStr" runat="server" Text="Label" Visible="False"></asp:Label></td>
                                    </tr>

                                    <tr>
                                        <td style="width:200px">
                                            <span>Confirm Password:</span>
                                        <br /> </td></tr>

                                        <tr>
                                         <td style="width:200px">   <asp:TextBox ID="txtPassword1" runat="server" 
                                                 TextMode="Password" CssClass="text"  Width="300px" Height="25"></asp:TextBox>
                                     <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="***Enter the new password again!" ControlToValidate="txtPassword1" Text="***" ForeColor="Red" InitialValue="">

                    </asp:RequiredFieldValidator>
                                             <asp:CompareValidator ID="CompareValidator1" runat="server" 
                                                 ErrorMessage="Passwords do not Match!" ControlToCompare="txtPassword" 
                                                 ControlToValidate="txtPassword1"></asp:CompareValidator>
                                       <br /> </td>
                                    </tr>

                                    <tr>
                                    <td><br /><br /></td>
                                    </tr>

                                    <tr>
                                        <td>

                                            <asp:Button runat="server" ID="btnLogin" Text="Save" Height="25px" 
                                                Width="76px" CssClass="btn" BackColor="Goldenrod" onclick="btnLogin_Click"></asp:Button><asp:HyperLink ID="HyperLink1" runat="server">Cancel</asp:HyperLink>
                                        </td>
                                    </tr>
                                </table>
                                </div>
                               <asp:ValidationSummary ID="ValidationSummary1" Font-Italic="true" font-size="Small"  forecolor="Black" runat="server" />

2 个答案:

答案 0 :(得分:1)

如何使用EnableViewState =false

<asp:TextBox ID="txtoldpass" runat="server" EnableViewState ="False" CssClass="text" TextMode="Password"   
Width="300px" Height="25" ></asp:TextBox>

答案 1 :(得分:1)

不要仅回设整个页面来设置密码强度标签。使用Javascript就可以了。在这里使用javascript将有2个好处。 1)页面不会回发(通过Postback整页将刷新,只是因为单个字段整页回发不是一个好习惯) 2)无需维护文本框的状态,因为没有回发。