Search code examples
asp.netfindcontrol

Why can't FindControl find a password field on my form?


If this is not possible, how can I get the password out of the field?

    dim pw1 as textbox, password as string
    pw1 = ctype(FindControl("PasswordStr"), textbox)
    password = pw1.text

Nope: System.NullReferenceException: Object reference not set to an instance of an object.

This code is in a sub that I am calling on a button click


Edited by: rockinthesixstring

Here's what the OP said his ASPX markup looks like

<form runat="server" id="form1">
  <p>
    <label for="passwordStr">Password</label>
    <input type="text" textmode="password" id="passwordStr" name="passwordStr" maxlength="50">  
  </p>
</form>

Solution

  • You are not using server control's by the looks of things (based on your comment)

    Use a control on the aspx page like below:

    <asp:TextBox TextMode="Password" ID="passwordInput" runat"server"></asp:TextBox>
    

    You can access a server control from the code behind file using

    passwordInput.Text