28

I have a few buttons on a web form, and when the user clicks them they will update the the textbox. This worked till I added the textmode = password. Now the textbox doesn't show the text anymore. I debugged the app, and the text property is getting the value, but once again it is not showing.

Here is what I have tried:

 protected void btn_punch_7_Click(object sender, EventArgs e)
    {

        const string string_punch_Number_7 = "7";
        var text = txt_punch.Text;
        text += string_punch_Number_7;

        txt_punch.Text = text;


    }

    protected void btn_punch_8_Click(object sender, EventArgs e)
    {
        const string string_punch_Number_8 = "8";
        var text = txt_punch.Text;
        text += string_punch_Number_8;

        txt_punch.Text = text;

    }

I have also tired this:

public partial class WebForm3 : System.Web.UI.Page
{
    public string string_punch;
    protected void Page_Load(object sender, EventArgs e)
    {
        MultiView1.SetActiveView(View1);

        txt_punch.Width = 300;
        txt_punch.Height = 50;
        txt_punch.MaxLength = 4;
        txt_punch.Attributes.Add("OnChange", string_punch);

    }

    protected void btn_punch_7_Click(object sender, EventArgs e)
    {

        const string string_punch_Number_7 = "7";
        string_punch = txt_punch.Text;
        string_punch += string_punch_Number_7;

        txt_punch.Text = string_punch;


    }

    protected void btn_punch_8_Click(object sender, EventArgs e)
    {
        const string string_punch_Number_8 = "8";
        string_punch = txt_punch.Text;
        string_punch += string_punch_Number_8;

        txt_punch.Text = string_punch;

    }
7
  • You shouldn't force the text to be visible on a password field. If you force the text, then anybody can get the password by looking at the html for your site. Commented May 24, 2013 at 16:47
  • @bastos.sergio How big a problem is that? The HTML will not be stored anywhere. Unless the person typing the password will save the page to disk just before submitting. Commented May 24, 2013 at 18:55
  • It doesn't need to be stored... Try this, right click anywhere on a site and choose the option "view source code". That's all you need to view the html... Commented May 24, 2013 at 19:44
  • sure, but that can only happen if the person typing the password walks away before submitting. I don't think there's a lot of risk. Commented May 24, 2013 at 20:56
  • @bastos.sergio I thought the point of a password box, or textmode to be set to password, is to mask the password? Commented May 24, 2013 at 22:28

6 Answers 6

65

How desperate are you?

If you're not desperate enough to try anything, anything to get it to work, don't read on. This will not be nice. OK? OK.

The trick is to make the web app think that it's not a password box. In other words, don't use TextMode="password".
Then in Page_Load, put txt_punch.Attributes["type"] = "password"

That's it. The browser will know it's a password field (and show asterisks or dots), but the server side won't know, so it will send the content to the client as it it were plain text. Of course, this will also put the password in the page source...

Sign up to request clarification or add additional context in comments.

6 Comments

I am not that sure I am that desperate, lol.I have thought about this, before I posted the question. I really wanted to do this in the code behind, or security and what not. But if all else fails. Thanks for you input btw.
Absolutely easy but after I saw this :)
This is such a great solution especially if you need to check whether the text has changed. Using the "TextBox.Attributes.Add("value", "yourPassword");" fires your onTextChanged event. Great.
this works, thank you, just that this, (as in microsoft) is messed up man, what is the point of password type, if not used correctly it will reset password to blank on update...
@visual It blanks out the password because 1) otherwise it needs to send the password over the line again, which is a security risk, 2) as I said, the password ends up in plain text in the page source (and in the browser's cache on the computer's hard disk), which is a security risk. 3) The password appears like ●●●●●●● on the screen, so the user can't see if they had typos, and they will need to type it again anyway! 4) It's not just Microsoft, everybody does it like this.
|
16

When you set the TextMode property of a TextBox to Password the default behaviour is for it not to display the value of the Text property.

This is to prevent unmasked passwords being shown in the HTML source of the page.

One solution is to use an attribute to hold the password value:

TextBox.Attributes.Add("value", "yourPassword");

Source

5 Comments

@nate - missed that, sorry. Don't know what else to suggest.
@nate I don't see where you tried that. I did come up with an alternate solution though...
The one where you give the textbox an attribute onchange="78"?
Works nicely. textBox.Attributes["value"] = "password"; in Page_Load instead of textBox.Text = "password";.
Didn't work very long. The content is lost on every postback. That thing is broken. Gotta use <asp:TextBox type="password"/> instead of <asp:TextBox TextMode="Password"/>. Then the Text property can be used normally.
2

When you set property of textbox to TextMode="Password" then in edit mode text box display is blank. If you fill value like

TextBox.Text = "Password";

then not working and you need to enter password again to save form. You can use Attributes to fill value in textbox like below code.

TextBox.Attributes["value"] = "your password value";

Comments

1

Based off option one of Kola's answer you can try this:

TextBox.Attributes["value"] = "Whatever value goes here";

Comments

0

This is an asp.net BUG!! The email validation algorithm is very strict, resulting in the exclusion of some valid emails. If you switch the TextMode from "Password" tp "SingleLine", your code will work.

1 Comment

Bad answer!!!!!
0

we cant directly assign values to password text box. for assign values to text box password we should use textbox attributes

Textbox1.Attributes.Add("value",variable/"string");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.