Password field is cleared after postback

D

dana lees

Hello,

I am developing a C# asp.net application.
I have a webform which contains 2 dropdowns and a textbox with
type="password".
On "SelectedIndexChanged" event of the first dropdown, there is a postback,
in order to fill the second dropdown according to the value selected in the
first dropdown.
The problem is that the password field which has been filled up before is
cleared after postback.
I know this is for security, but is there a way to keep the password??

Thank you
 
B

Ben Amada

dana said:
Hello,

I am developing a C# asp.net application.
I have a webform which contains 2 dropdowns and a textbox with
type="password".
On "SelectedIndexChanged" event of the first dropdown, there is a
postback, in order to fill the second dropdown according to the value
selected in the first dropdown.
The problem is that the password field which has been filled up before is
cleared after postback.
I know this is for security, but is there a way to keep the password??

Thank you

Yes, you can add a "value" attribute to the password textbox on postback:

txtPassword.Attributes.Add("value", txtPassword.Text)

Ben
 
B

Ben Amada

dana said:
I have tried that and it didn't work. The field stayed empty...

Hmm... not sure why that doesn't work. Are you using .NET v2.0? I've done
it in v1.1 without any problems.

Another option is to manually set the value of the password field via
JavaScript. There are several ways to do this.

One way is to create a hidden input field. On postback, put the password in
the hidden input field (i.e. txtHiddenPwd.Value = txtPwd.Text). Then with a
startup script or via the body tag's onload attribute, set the value of the
password textbox to the value of the hidden input field.

Ben
 
D

dana lees

I am using version 1.1.
On debug, I see that i do have the value of the password after postback, i
just can't set the textbox with that value.

I have tried also javascript:

document.all.txtPassword.InnerText = "some text";

and this doesn't work as well...

I don't need to use a hidden textbox becuase i do have the value after
postback, i just can't set the textbox's value to that value...
 
B

Ben Amada

dana said:
I am using version 1.1.
On debug, I see that i do have the value of the password after postback, i
just can't set the textbox with that value.

I have tried also javascript:

document.all.txtPassword.InnerText = "some text";

and this doesn't work as well...

I don't need to use a hidden textbox becuase i do have the value after
postback, i just can't set the textbox's value to that value...

Server-side you can't set the textbox's value to the password. However,
with JavaScript (client-side) you should be able to set the value of the
textbox with or without a hidden input field. Without a hidden input field,
you could simply add a runat="server" attribute to the <body> tag, then on
the server-side during a postback, you can add an "onload" attribute to the
body tag specifying the password value. Like this:

===== HTML =====
<body id="myBody" runat="server">

===== Code-Behind =====
myBody.Attributes.Add("onload", _
"document.getElementById('txtPassword').value = '" & _
txtPassword.Text & "'")

(the C# syntax should be similar)

Also, in the code-behind, if Visual Studio doesn't recognize the "myBody"
control (which happens sometimes), you'll probably have to manually add the
control declaration to the code-behind:

Dim myBody As HtmlControls.HtmlGenericControl

.... or instead of adding an "onload" attribute to the body tag, you could
use the RegisterStartupScript method to set the password textbox value.

Ben
 
B

Ben Amada

dana said:
I have tried also javascript:

document.all.txtPassword.InnerText = "some text";

and this doesn't work as well...

By the way, what do you mean that it "doesn't work as well"? How about:

document.getElementById('txtPassword').value = "some text";

Ben
 
D

dana lees

It worked, when using

document.getElementById('txtPassword').value = "some text";

Thank you very much :)
 
G

Guest

Dana,

As Ben suggested you can use assword.Attributes.Add("value",
txtPassword.Text) to set the password .But instead of txtPassword.Text you
should retrieve password text using
txtPassword.Attributes["value"].ToString().
 
K

Karl Seguin

Humm..I don't know why, but I thought this thread hadn't been answered
yet...*blush*
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top