Friday, February 1, 2013

Set value of input type password or textbox where TextMode="password".

Old school stuff, probably everyone knows it, still sharing because had a little bit trouble tackling this. Thought its very easy and it is,

Why we need this? Here is why, you have a cookie where you save username and password for later use, and of course the cookies are encrypted.

Now you retrieve the cookie found that a user login info is there and user checked the “Remember Me” stuff. You need to set to both username and password in the text fields and pre-populate those.

In my case the password field is html markup.


<input id="password" type="password" placeholder="Password" required="required" runat="server"
clientidmode="Static" value="" />
<asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator1"
Text="*" ErrorMessage="*" ControlToValidate="password" ForeColor="Red"></asp:RequiredFieldValidator>

Now I set the code from c# as below run the app didn’t worked.

1. password.Value = "1234567890";
2. password.Attributes.Add("value","1234567890");

After trying the first case I realized that its not going to work, then googled it found few suggestions that adding as attribute should work. No luck here also, I wonder why it didn’t worked in few of the posts developers are saying that it works, but in my case it didn’t. Finally take a D Tour and Used javascript the nail the bad boy.

Step one:

Step Two:

<script language="javascript" type="text/javascript">
function SetDefaultPass(parameters) {
try {
document.getElementById('password').value = parameters;
} catch (e) {

That’s its this fine finally it worked like magic. no issue now.

Hope this helps to some lame, like me some day :D.