Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Password field is cleared after postback

210 views
Skip to first unread message

dana lees

unread,
Nov 23, 2005, 2:25:37 AM11/23/05
to
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


Ben Amada

unread,
Nov 23, 2005, 3:36:40 AM11/23/05
to
dana lees wrote:

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

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

Ben


dana lees

unread,
Nov 23, 2005, 3:42:22 AM11/23/05
to
I have tried that and it didn't work. The field stayed empty...

"Ben Amada" <b...@REpoMOweVErpick.com> wrote in message
news:OaFdNkA8...@TK2MSFTNGP12.phx.gbl...

Ben Amada

unread,
Nov 23, 2005, 3:59:16 AM11/23/05
to
dana lees wrote:

> 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


dana lees

unread,
Nov 23, 2005, 4:09:06 AM11/23/05
to
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...

"Ben Amada" <b...@REpoMOweVErpick.com> wrote in message

news:u2Kz1wA8...@tk2msftngp13.phx.gbl...

Ben Amada

unread,
Nov 23, 2005, 4:52:41 AM11/23/05
to
dana lees wrote:

> 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


Ben Amada

unread,
Nov 23, 2005, 4:56:23 AM11/23/05
to
dana lees wrote:

> 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


dana lees

unread,
Nov 23, 2005, 5:04:49 AM11/23/05
to
It worked, when using

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

Thank you very much :-)

"Ben Amada" <b...@REpoMOweVErpick.com> wrote in message

news:%23G3ExQB...@TK2MSFTNGP11.phx.gbl...

Santhi Maadhaven

unread,
Nov 23, 2005, 5:34:03 AM11/23/05
to
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().

REMOVE @removeopenmymindremovemetoo.andmenet Karl Seguin

unread,
Nov 23, 2005, 7:38:09 AM11/23/05
to

REMOVE @removeopenmymindremovemetoo.andmenet Karl Seguin

unread,
Nov 23, 2005, 7:47:10 AM11/23/05
to
Humm..I don't know why, but I thought this thread hadn't been answered
yet...*blush*

"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:%232IbIrC...@TK2MSFTNGP15.phx.gbl...

0 new messages