27

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;

    }
Suhaib Janjua
  • 3,538
  • 16
  • 59
  • 73
nate
  • 1,418
  • 5
  • 34
  • 73
  • 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. – bastos.sergio May 24 '13 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. – Mr Lister May 24 '13 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... – bastos.sergio May 24 '13 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. – Mr Lister May 24 '13 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? – nate May 24 '13 at 22:28
  • @bastos.sergio Also I don't see the password that I typed into the textbox in the source of the page when I view it in the browser. – nate May 24 '13 at 22:36
  • What you type doesn't get stored in the html (it's stored in memory). If you set the password via codebehind, like the examples below, you will see it in the html. Remember, there's a reason why sites like facebook, hotmail, etc never load your original password when you ask to change it. Once you set your password via attributes like *TextBos.Attributes.Add("value", "yourPassword");* you're leaving your site wide open for hackers. – bastos.sergio May 27 '13 at 10:31

6 Answers6

64

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...

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
  • 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. – nate May 24 '13 at 22:16
  • Absolutely easy but after I saw this :) – Subrata Sarkar Jun 17 '14 at 02:52
  • 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. – djfranzwa Jul 29 '14 at 13:05
  • 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 Jun 06 '17 at 06:48
  • @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. – Mr Lister Jun 06 '17 at 07:08
  • @MrLister, thanks for the explanation, it makes sense, i suppose best logic would be, if update is pressed and password field value is empty, then do not do the update or show "validation" message, "password is required" for update. – visual Jun 08 '17 at 00:55
15

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

ChrisF
  • 134,786
  • 31
  • 255
  • 325
  • @nate - missed that, sorry. Don't know what else to suggest. – ChrisF May 24 '13 at 16:27
  • @nate I don't see where you tried that. I did come up with an alternate solution though... – Mr Lister May 24 '13 at 18:52
  • The one where you give the textbox an attribute `onchange="78"`? – Mr Lister May 25 '13 at 07:11
  • Works nicely. `textBox.Attributes["value"] = "password";` in Page_Load instead of `textBox.Text = "password";`. – ygoe Apr 15 '14 at 07:54
  • 3
    Didn't work very long. The content is lost on every postback. That thing is broken. Gotta use `` instead of ``. Then the Text property can be used normally. – ygoe Apr 15 '14 at 08:17
1

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

TextBox.Attributes["value"] = "Whatever value goes here";
Barry Michael Doyle
  • 9,333
  • 30
  • 83
  • 143
1

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";
T.S.
  • 18,195
  • 11
  • 58
  • 78
A.M. Patel
  • 334
  • 2
  • 9
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.

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");
Hafsal Rh
  • 191
  • 1
  • 3