1

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>
Chase Florell
  • 46,378
  • 57
  • 186
  • 376
rg89
  • 341
  • 1
  • 2
  • 18

4 Answers4

2

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;
Jack Marchetti
  • 15,536
  • 14
  • 81
  • 117
1

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
Andrew
  • 9,967
  • 10
  • 64
  • 103
1

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;
Town
  • 14,706
  • 3
  • 48
  • 72
  • This will work if `PasswordStr` is not nested in another control. – Chase Florell Apr 05 '11 at 15:49
  • You can find it on PageLoad as well, providing it exists. The OP is saying that the compiler isn't finding the control. – Chase Florell Apr 05 '11 at 15:51
  • @rg89: You need runat="server" on your controls to be able to use FindControl, and to access your controls in this way in the code-behind. See my comment on your original question, or Jack Marchetti's answer. – Town Apr 05 '11 at 15:52
0

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;
Chase Florell
  • 46,378
  • 57
  • 186
  • 376
  • my page looks like this essentially:

    i don't use visual studio.
    – rg89 Apr 05 '11 at 15:52
  • if your form is that simple, you can simply do what others have said. `string password = PasswordStr.Text;`. `FindControl` is only useful if you have nested controls. – Chase Florell Apr 05 '11 at 15:53