Textbox using textmode password not showing text a

2020-05-17 07:17发布

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条回答
Deceive 欺骗
2楼-- · 2020-05-17 07:50

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

查看更多
Animai°情兽
3楼-- · 2020-05-17 07:55

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

TextBox.Attributes["value"] = "Whatever value goes here";
查看更多
戒情不戒烟
4楼-- · 2020-05-17 07:56

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");
查看更多
欢心
5楼-- · 2020-05-17 08:01

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

查看更多
淡お忘
6楼-- · 2020-05-17 08:09

When you set property of textbox to TextMode="Password" then in edit mode textbox display 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";
查看更多
贪生不怕死
7楼-- · 2020-05-17 08:09

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.

查看更多
登录 后发表回答