为什么 FindControl 在我的表单上找不到密码字段?

发布于 2024-10-29 19:28:21 字数 590 浏览 3 评论 0原文

如果这是不可能的,我怎样才能从字段中获取密码?

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

不:System.NullReferenceException:未将对象引用设置为对象的实例。

这段代码位于我在单击按钮时调用的子程序中


编辑者:rockinthesixstring

这是OP所说的他的ASPX标记的样子

<form runat="server" id="form1">
  <p>
    <label for="passwordStr">Password</label>
    <input type="text" textmode="password" id="passwordStr" name="passwordStr" maxlength="50">  
  </p>
</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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一个人的夜不怕黑 2024-11-05 19:28:21

如果密码字段不在中继器等另一个“容器”中,那么您只需访问它即可。

您的密码字段的 ID 是什么?

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

您可以像这样访问它:

pw1 = txtPassword.Text;

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;
·深蓝 2024-11-05 19:28:21

从外观上看,您没有使用服务器控件(根据您的评论)

在 aspx 页面上使用控件,如下所示:

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

您可以使用以下命令从代码隐藏文件访问服务器控件

passwordInput.Text

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
谁许谁一生繁华 2024-11-05 19:28:21

如果您的密码字段只是页面上的 ASP.NET 控件(未嵌套在 GridView ItemTemplate 等其他控件中),则可以执行以下操作:

string password = PasswordStr.Text;

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;
一个人的旅程 2024-11-05 19:28:21

由于我们不知道您的 ASPX 是什么样子,所以我们有点在黑暗中拍摄。

假设你有一个看起来像这样的 aspx

<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>

你会发现像这样的控件

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

你最初发布的代码正在寻找 Form 中的控件,这意味着,如果你有另一个控件 (FormVw< /code> 例如),那么您的代码将找不到嵌套的文本框。


编辑

你说你的表单看起来像这样

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

将其更改为这样

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

然后访问密码字段,如下所示

string password = passwordStr.Text;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文