【问题标题】:Retaining textbox text after postback event回发事件后保留文本框文本
【发布时间】:2014-01-17 08:10:14
【问题描述】:

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

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

【问题讨论】:

  • 为什么不使用客户端javascript来检查/验证密码强度?

标签: c# .net validation textbox


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    使用EnableViewState =false 怎么样?

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-05
      相关资源
      最近更新 更多