Export (0) Print
Expand All

HtmlInputPassword Class

Note: This class is new in the .NET Framework version 2.0.

Allows programmatic access to the HTML <input type= password> element on the server.

Namespace: System.Web.UI.HtmlControls
Assembly: System.Web (in system.web.dll)

[ValidationPropertyAttribute("Value")] 
public class HtmlInputPassword : HtmlInputText, IPostBackDataHandler
/** @attribute ValidationPropertyAttribute("Value") */ 
public class HtmlInputPassword extends HtmlInputText implements IPostBackDataHandler
ValidationPropertyAttribute("Value") 
public class HtmlInputPassword extends HtmlInputText implements IPostBackDataHandler

The HtmlInputPassword class is derived from the HtmlInputText class and is used to create a single-line text box that allows the user to enter a password. When using the HtmlInputPassword control, the Value property is posted to the server each time the form is submitted. The Value property for the HtmlInputPassword control is not persisted in view state.

The MaxLength property specifies the maximum number of characters that can be entered in the text box. The Size property allows you to specify the width of the text box. Use the Value property to determine the value entered in the text box by the user. The HtmlInputPassword control provides a ServerChange event that is raised when the Value property changes values between posts to the server. This allows you to create an event handler that performs a custom set of instructions each time the event is raised.

NoteNote

The HtmlInputPassword control does not provide a built-in way to post back to the server. You must provide another control on the Web page that supports posting back to the server, such as an HtmlButton or an HtmlInputSubmit control.

For a list of initial property values for an instance of HtmlInputPassword, see the HtmlInputPassword constructor.

The following code example demonstrates how to use the <input type=text>, <input type=password>, and <input type=submit> HTML controls declaratively on a Web Forms page.

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
  <body>
    <form id="Form1" runat="server">
        &nbsp;&nbsp;

        <p></p><div>Username</div> <br />
        <input type="text" runat="server" />

        <p></p><div>Password</div> <br />
        <input type="password" runat="server" />

        <p></p><input type="submit" runat="server" value="Submit" />

    </form>
  </body>
</html>

<%@ Page Language="VJ#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
  <body>
    <form id="Form1" runat="server">
        &nbsp;&nbsp;

        <p></p><div>Username</div> <br />
        <input type="text" runat="server" />

        <p></p><div>Password</div> <br />
        <input type="password" runat="server" />

        <p></p><input type="submit" runat="server" value="Submit" />

    </form>
  </body>
</html>

The following code example demonstrates how to add the same HTML controls programmatically during a call to the Page_Load method.

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
  
  protected void page_load(object sender, EventArgs e)
  {
    if (IsPostBack)
    {
      // Add code to process the Login.
    }
  }

  protected void Page_Init(object sender, EventArgs e)
  {
    HtmlInputText userText = new HtmlInputText();
    userText.MaxLength = 20;
    Placeholder1.Controls.Add(userText);

    HtmlInputPassword passwordText = new HtmlInputPassword();
    passwordText.MaxLength = 20;
    Placeholder2.Controls.Add(passwordText);

    HtmlInputSubmit submitButton = new HtmlInputSubmit();
    submitButton.Value = "Submit";
    Placeholder3.Controls.Add(submitButton);
  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <body>
    <form runat="server">

      <table cellpadding="2">
        <tr>
        <td>User Name
            <asp:placeholder
                runat="server"
                id="Placeholder1" />
        </td></tr>
        <tr>
        <td>Password
            <asp:placeholder
                runat="server"
                id="Placeholder2" />
        </td></tr>
        <tr><td><asp:placeholder
                runat="server"
                id="Placeholder3" />
        </td></tr>
      </table>
    </form>
  </body>
</html>

<%@ Page Language="VJ#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
  
  protected void Page_Load(Object sender, System.EventArgs e)
  {
    if (get_IsPostBack())
    {
      // Add code to process the Login.
    }

  }

  protected void Page_Init(Object sender, EventArgs e)
  {
    HtmlInputText userText = new HtmlInputText();
    userText.set_MaxLength(20);
    Placeholder1.get_Controls().Add(userText);

    HtmlInputPassword passwordText = new HtmlInputPassword();
    passwordText.set_MaxLength(20);
    Placeholder2.get_Controls().Add(passwordText);

    HtmlInputSubmit submitButton = new HtmlInputSubmit();
    submitButton.set_Value("Submit");
    Placeholder3.get_Controls().Add(submitButton);


  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <body>
    <form runat="server">

      <table cellpadding="2">
        <tr>
        <td>User Name
            <asp:placeholder
                runat="server"
                id="Placeholder1" />
        </td></tr>
        <tr>
        <td>Password
            <asp:placeholder
                runat="server"
                id="Placeholder2" />
        </td></tr>
        <tr><td><asp:placeholder
                runat="server"
                id="Placeholder3" />
        </td></tr>
      </table>
    </form>
  </body>
</html>

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft