为什么 FindControl 在我的表单上找不到密码字段?
如果这是不可能的,我怎样才能从字段中获取密码?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果密码字段不在中继器等另一个“容器”中,那么您只需访问它即可。
您的密码字段的 ID 是什么?
您可以像这样访问它:
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?
You access it like this:
从外观上看,您没有使用服务器控件(根据您的评论)
在 aspx 页面上使用控件,如下所示:
您可以使用以下命令从代码隐藏文件访问服务器控件
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:
You can access a server control from the code behind file using
如果您的密码字段只是页面上的 ASP.NET 控件(未嵌套在 GridView ItemTemplate 等其他控件中),则可以执行以下操作:
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:
由于我们不知道您的 ASPX 是什么样子,所以我们有点在黑暗中拍摄。
假设你有一个看起来像这样的 aspx
你会发现像这样的控件
你最初发布的代码正在寻找
Form
中的控件,这意味着,如果你有另一个控件 (FormVw< /code> 例如),那么您的代码将找不到嵌套的文本框。
编辑
你说你的表单看起来像这样
将其更改为这样
然后访问密码字段,如下所示
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
You would find the control like this
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
Change it to this
Then access the password field like this