Click here to Skip to main content
15,892,927 members
Articles / Web Development / ASP.NET
Tip/Trick

Retain Password Field in ASP.NET Post Back

Rate me:
Please Sign up or sign in to vote.
2.61/5 (41 votes)
26 Apr 2010CPOL 46.8K   6   10
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)...
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

VB
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#
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

License

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


Written By
Software Developer (Senior)
India India
my technical blog http://kbrecord.com/

Comments and Discussions

 
GeneralMy vote of 1 Pin
Satyavir Yadav India22-May-13 19:09
Satyavir Yadav India22-May-13 19:09 
GeneralMy vote of 1 Pin
AWdrius26-Apr-10 5:04
AWdrius26-Apr-10 5:04 
GeneralThis makes the password visible Pin
Paulo Zemek12-Feb-10 4:52
mvaPaulo Zemek12-Feb-10 4:52 
GeneralMy vote of 1 Pin
Keith Barrow10-Feb-10 9:05
professionalKeith Barrow10-Feb-10 9:05 
QuestionWhy it is needed? Pin
Anurag Gandhi3-Feb-10 18:22
professionalAnurag Gandhi3-Feb-10 18:22 
AnswerRe: Why it is needed? Pin
M.Premraj3-Feb-10 19:23
M.Premraj3-Feb-10 19:23 
GeneralRe: Why it is needed? Pin
Bassam Saoud4-Feb-10 6:33
Bassam Saoud4-Feb-10 6:33 
GeneralRe: Why it is needed? Pin
AntounPG6-Feb-10 23:27
AntounPG6-Feb-10 23:27 
GeneralRe: Why it is needed? Pin
Brij9-Feb-10 7:31
mentorBrij9-Feb-10 7:31 
GeneralRe: Why it is needed? Pin
moroandrea22-Feb-10 5:44
moroandrea22-Feb-10 5:44 
Despite this sounds like an "interesting" technique, I absolutely agree with the other guys. It may constitute a risk and make flaw all the security policies previously adopted.

This is a bit more complex document, but I really suggest to the author of this tip to have a read on it to clarify some concepts about a "secure web site in asp.net."

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

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