IceDawg
IceDawg

Reputation: 327

Clearing a Password text box in ASP.NET

I have two boxes one with username [text] and one with password [password].

I'm using this code in the CodeBehind:

  protected void Button2_Click1(object sender, EventArgs e)
    {
        TextBox1.Text = "";
        TextBox2.Text = "";
    }

[asp.net/html]

<asp:TextBox ID="TextBox2" runat="server" Height="24px" Style="text-align: center" Width="209px" TextMode="Password"></asp:TextBox>

The first text box clears but the second doesn't. Is there a special way to do this in ASP.NET?

Upvotes: 2

Views: 2900

Answers (3)

user1594702
user1594702

Reputation:


textbox1.text = string.empty;
textbox2.text = string.empty;

Upvotes: 0

R.C
R.C

Reputation: 10565

When the TextMode property of the <asp:TextBox /> is set to Password the value set in the Text property will not display at runtime, Or you can say it won't have any effect to set the Text property. This is by design to prevent the unmasked password from being displayed in the HTML source of the page.

As a solution, use this::

this.TextBox2.Attributes["value"] = "";

OR::

this.TextBox2.Attributes.Add("value", "");

Check here for full details.

Upvotes: 2

Wilfredo P
Wilfredo P

Reputation: 4076

Hi I don't know what happend in your HTML but I wrote this:

<asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox>
    <asp:TextBox ID="TextBox2" runat="server" Height="24px" Style="text-align: center" Width="209px" TextMode="Password"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />

And the codebehind:

protected void Button1_Click(object sender, EventArgs e)
        {
            TextBox1.Text = "";
            TextBox2.Text = "";
        }

And It works, the only thing is when the button does not have the onclick only It will clean the password texbox, check it and maybe this is your problem.

Upvotes: 3

Related Questions