Pages

Tuesday 21 August 2012

How to maintain Text of a Password field which vanishes after postback in Asp.Net?

       In some situations, if you are having a Password Field in a page, then its text will vanish after each postback  occurred in the page. So, in those situation there are two ways to maintain its text after postback in Asp.Net.
       In the following Example, I have taken two TextBoxes(One for User ID and other one for Password). Then I have a DropDownList from where a User can select his user type and I have set its "AutoPostBack" property to "true".
Procedure-1 : (Using UpdatePanel)
1. Create a .aspx page.
2. Add two TextBoxes(One for UserID and another for Password), One DropDownList and a Button for Login.
3. Set the "TextMode" property of the TextBox meant for Password to "Password".
4. Set the "AutoPostBack" property of the DropDownList to "true".
5. Add an UpdatePanel and place the DropDownList inside its ContentTemplate.

<asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <table class="style1">
            <tr>
                <td class="style2">
                    User ID :
                </td>
                <td>
                    <asp:TextBox ID="txtUserID" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    Password :
                </td>
                <td>
                    <asp:TextBox ID="txtPassword" runat="server" TextMode="Password"> 
                                                       </asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    User Type :
                </td>
                <td>
                    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                        <ContentTemplate>
                            <asp:DropDownList ID="ddlUserType" runat="server" 
                                                                  AutoPostBack="true">
                                <asp:ListItem>Administrator </asp:ListItem>
                                <asp:ListItem>General Employee</asp:ListItem>
                                <asp:ListItem>Visitor</asp:ListItem>
                            </asp:DropDownList>
                        </ContentTemplate>
                    </asp:UpdatePanel>
                </td>
            </tr>
            <tr>
                <td class="style2">
                </td>
                <td>
                    <asp:Button ID="btnLogin" runat="server" Text="Log In" />
                </td>
            </tr>
        </table>
Now, that's it. Now the Password will not vanish after postback.
Procedure-2 : (Without using UpdatePanel)
1. Create a .aspx page.
2. Add two TextBoxes(One for UserID and another for Password), One DropDownList and a Button for Login.
3. Set the "TextMode" property of the TextBox meant for Password to "Password".
4. Set the "AutoPostBack" property of the DropDownList to "true".
<table>
       <tr>
            <td>
                 User ID :
            </td>
             <td>
                 <asp:TextBox ID="txtUserID" runat="server"></asp:TextBox>
            </td>
       </tr>
       <tr>
            <td>
                 Password :
            </td>
            <td>
             <asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>
           </td>
      </tr>
      <tr>
           <td>
               User Type :
           </td>
           <td>
               <asp:DropDownList ID="ddlUserType" runat="server" AutoPostBack="true">
                   <asp:ListItem>Administrator </asp:ListItem>
                   <asp:ListItem>General Employee</asp:ListItem>
                   <asp:ListItem>Visitor</asp:ListItem>
               </asp:DropDownList>
          </td>
      </tr>
      <tr>
          <td>
          </td>
          <td>
                <asp:Button ID="btnLogin" runat="server" Text="Log In" />
          </td>
     </tr>
</table>
5. Then, go the code behind and write the following line in the Page_Load Event.
protected void Page_Load(object sender, EventArgs e)
{
    txtPassword.Attributes.Add("value", txtPassword.Text);
}




Regards,
Prajanuranjan....

No comments:

Post a Comment

Total Pageviews