rg89
rg89

Reputation: 371

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>

Upvotes: 1

Views: 793

Answers (4)

Chase Florell
Chase Florell

Reputation: 47377

Since we don't know what your ASPX looks like, we're kinda shooting in the dark.

ASSUMING you have an aspx that looks like this

<form id="form1" runat="server" DefaultFocus="formVw$txtName">
    <div>
        <asp:FormView ID="formVw" runat="server">
            <ItemTemplate>
                Name: 
                <asp:TextBox ID="txtName" runat="server"
                        Text='<%# Eval("FirstName") + " " + Eval("LastName") %>' />
            </ItemTemplate>
        </asp:FormView>
    </div>
</form>

You would find the control like this

TextBox tb = this.FindControl("form1$formVw$txtName") as TextBox;
if (tb != null)
{
    //Access TextBox control
}

The code you originally posted is looking for the control within the Form which means, if you have another control (FormVw for example), then your code wont find the nested textbox.


EDIT

You said your form looks like this

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

Change it to this

<form runat="server" id="form1">
  <p>
    <label for="passwordStr">Password</label>
    <asp:TextBox runat="server" TextMode="password" ID="passwordStr" maxlength="50">  
  </p>
</form>

Then access the password field like this

string password = passwordStr.Text;

Upvotes: 0

Andrew
Andrew

Reputation: 10033

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

Upvotes: 1

Town
Town

Reputation: 14906

If your password field is simply an ASP.NET Control on your page (not nested in another control such as a GridView ItemTemplate), you can just do this:

string password = PasswordStr.Text;

Upvotes: 1

Jack Marchetti
Jack Marchetti

Reputation: 15754

If the password field isn't in another "container" like a repeater, then you can simply just access it.

What is the ID of your password field?

<asp:TextBox ID="txtPassword" TextMode="password" runat="server" />

You access it like this:

pw1 = txtPassword.Text;

Upvotes: 2

Related Questions