Click here to Skip to main content
13,148,214 members (52,620 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

33.9K views
6 bookmarked
Posted 2 Jan 2010

Retain Password Field in ASP.NET Post Back

, 26 Apr 2010
Rate this:
Please Sign up or sign in to vote.
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

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

License

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

Share

About the Author

M.Premraj
Software Developer (Senior)
India India
my technical blog http://kbrecord.com/

You may also be interested in...

Pro

Comments and Discussions

 
GeneralMy vote of 1 Pin
Satyavir Yadav from New Delhi22-May-13 19:09
memberSatyavir Yadav from New Delhi22-May-13 19:09 
GeneralMy vote of 1 Pin
AWdrius26-Apr-10 5:04
memberAWdrius26-Apr-10 5:04 
GeneralThis makes the password visible Pin
Paulo Zemek12-Feb-10 4:52
memberPaulo Zemek12-Feb-10 4:52 
GeneralMy vote of 1 Pin
keefb10-Feb-10 9:05
memberkeefb10-Feb-10 9:05 
QuestionWhy it is needed? Pin
Anurag Gandhi3-Feb-10 18:22
memberAnurag Gandhi3-Feb-10 18:22 
AnswerRe: Why it is needed? Pin
M.Premraj3-Feb-10 19:23
memberM.Premraj3-Feb-10 19:23 
GeneralRe: Why it is needed? Pin
Bassam Saoud4-Feb-10 6:33
memberBassam Saoud4-Feb-10 6:33 
GeneralRe: Why it is needed? Pin
Antoun Gorgy6-Feb-10 23:27
memberAntoun Gorgy6-Feb-10 23:27 
GeneralRe: Why it is needed? Pin
Brij9-Feb-10 7:31
memberBrij9-Feb-10 7:31 
GeneralRe: Why it is needed? Pin
moroandrea22-Feb-10 5:44
membermoroandrea22-Feb-10 5:44 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170924.2 | Last Updated 26 Apr 2010
Article Copyright 2010 by M.Premraj
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid