Click here to Skip to main content

Retain Password Field in ASP.NET Post Back

Normally, whenever a form is posted backed, the password field will clear. But we dont want it to do that. In that scenario, we can add a little bit of code will avoid the issue.Retain Password In VB.NETProtected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)...
Sign Up to vote bad good
Add a reason or comment to your vote: x
Votes of 3 or less require a comment
Normally, whenever a form is posted backed, the password field will clear. But we dont want it to do that.
 
In that scenario, we can add a little bit of code will avoid the issue.
 
Retain Password In VB.NET
 
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If IsPostBack Then
            If Not String.IsNullOrEmpty(txtPassword.Text.Trim()) Then
                txtPassword.Attributes.Add("value", txtPassword.Text)
            End If
End If
End Sub
 
In C#
protected void Page_Load(object sender, EventArgs e)
{
 
      if (IsPostBack)
      {
            if (!(String.IsNullOrEmpty(txtPassword.Text.Trim()))
            {
                  txtPassword.Attributes["value"]= txtPassword.Text;
            }
      }
}
 
There are many other methods also to retain values between postback, whether it is password or any field value stored in any control i.e. viewstate, session, etc
 
However retaining password is considered to be security hole so it is not preferable to retain password between post backs.
 
Happy coding
Posted 2 Jan '10
M.Premraj1.1K
Edited 26 Apr '10


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Your Filters
Interested
Ignored
     
  1. Christian Graus (1,099)
  2. SAKryukov (611)
  3. CRDave1988 (432)
  4. Abhinav S (215)
  1. SAKryukov (10,303)
  2. Christian Graus (7,108)
  3. OriginalGriff (5,156)
  4. Abhinav S (4,550)
  5. thatraja (4,371)

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 PinmemberAWdrius6:04 26 Apr '10  
GeneralThis makes the password visible PinmemberPaulo Zemek5:52 12 Feb '10  
GeneralMy vote of 1 Pinmemberkeefb10:05 10 Feb '10  
QuestionWhy it is needed? PinmemberAnurag Gandhi19:22 3 Feb '10  
AnswerRe: Why it is needed? PinmemberM.Premraj20:23 3 Feb '10  
GeneralRe: Why it is needed? PinmemberBassam Saoud7:33 4 Feb '10  
GeneralRe: Why it is needed? PinmemberAntoun Gorgy0:27 7 Feb '10  
GeneralRe: Why it is needed? PinmemberBrij8:31 9 Feb '10  
GeneralRe: Why it is needed? Pinmembermoroandrea6:44 22 Feb '10  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy | Mobile
Web04 | 2.5.120210.1 | Last Updated 26 Apr 2010
Copyright © CodeProject, 1999-2012
All Rights Reserved. Terms of Use
Layout: fixed | fluid