Click here to Skip to main content
11,477,044 members (72,411 online)
Click here to Skip to main content

Retain Password Field in ASP.NET Post Back

, 26 Apr 2010 CPOL 26K 6
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/

Comments and Discussions

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

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 | Terms of Use | Mobile
Web03 | 2.8.150520.1 | Last Updated 26 Apr 2010
Article Copyright 2010 by M.Premraj
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid