Related Posts Plugin for WordPress, Blogger...

About

Follow Us

Thursday, 26 March 2015

Introduction:
While working with Password type textboxes i.e. Textbox with TextMode =”Password”, when postback occurs, the value for these textboxes will be lost. We need to enter password again, which is not user friendly.

In this article i am going to explain with example How to maintain the password value in the TextBox controls during/on/while/even after the occurrence of postback event in asp.net using both C# and VB.Net languages.


Source Code:

   <fieldset style="width:280px";>
    <legend><strong>Maintain password after postback</strong></legend>
    <table>
    <tr><td>UserName</td><td>
        <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
        </td></tr>
    <tr><td>Password</td><td>
        <asp:TextBox ID="txtPwd" runat="server" TextMode="Password"></asp:TextBox>
        </td></tr>
        <tr><td>Confirm Password</td><td>
        <asp:TextBox ID="txtConfirmPwd" runat="server" TextMode="Password"></asp:TextBox>
        </td></tr>       
        <tr><td>Country</td><td>
            <asp:DropDownList ID="ddlCountry" runat="server" AutoPostBack="True">
                <asp:ListItem>India</asp:ListItem>
                <asp:ListItem>USA</asp:ListItem>
                <asp:ListItem></asp:ListItem>
            </asp:DropDownList>
        </td></tr>       
    <tr><td></td><td>
        <asp:Button ID="btnSubmit" runat="server" Text="Submit"
            onclick="btnSubmit_Click" /></td></tr>
    </table>
    </fieldset>

 C#.Net code to keep the password in Postback
write the code as :
protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
      {
            if (!(String.IsNullOrEmpty(txtPwd.Text.Trim())))
            {
                 txtPwd.Attributes["value"]= txtPwd.Text;              
            }
            if (!(String.IsNullOrEmpty(txtConfirmPwd.Text.Trim())))
            {
                txtConfirmPwd.Attributes["value"] = txtConfirmPwd.Text;
            }
      }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
     // Write your code here;      
    }

 Vb.Net Code  retain  the password in textbox  after Postback
In the code behind file(.aspx.vb) write the code as:

  Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If IsPostBack Then
            If Not ([String].IsNullOrEmpty(txtPwd.Text.Trim())) Then
                txtPwd.Attributes("value") = txtPwd.Text
            End If
            If Not ([String].IsNullOrEmpty(txtConfirmPwd.Text.Trim())) Then
                txtConfirmPwd.Attributes("value") = txtConfirmPwd.Text
            End If
        End If
    End Sub

    Protected Sub btnSubmit_Click(sender As Object, e As System.EventArgs) Handles btnSubmit.Click
     // Write your code here      

    End Sub

0 comments:

Post a Comment