找不到文本框的页面 [英] Cannot find TextBox on Page

查看:135
本文介绍了找不到文本框的页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面上的文本框,但是当我使用

I have a textbox on a page but when i use

TextBox formTextBox = Page.FindControl(textBox) as TextBox;

它回来。有没有解决的办法?我知道该控件的页面上,但我不能找到它。

it comes back null. Is there a way around this? I know the control is on the page but i cant find it.

感谢

推荐答案

如果你使用 MasterPages 这个控制是坐在<$ C $页面C>的ContentPlaceHolder ,你不能通过的FindControl 直接,因为在唯一的控制页面的 ControlCollection中得到参考控制是母版本身。
这就说得通了。你不能保证一个ID是唯一的,当控制与母版页的顶层,因为其他内容网页,可能也有这个ID的控制和的FindControl 能回到今天比明天另一个控制。

If you're using MasterPages and this control is in a page sitting in a ContentPlaceholder, you cannot get the reference to the control via FindControl directly, since the only control in the page's ControlCollection is the MasterPage itself. That makes sense. You cannot guarantee an ID to be unique when the control is on the top level of a page with MasterPage, because other ContentPages might as well have a control with this ID and FindControl could today return another control than tomorrow.

如果你看一下 NamingContainer 要找到控制的,你看,在情况下,母版这是的ContentPlaceHolder 并在一个正常的页面情况下,它是页面本身。

If you look at the NamingContainer of the Control you want to find, you see that in case of a MasterPage it is the ContentPlaceHolder and in case of a "normal" Page it is the Page itself.

所以,你需要去的母版的的ContentPlaceHolder参考第一之前,你可以通过找到的FindControl控制:

So you need to get a reference to the MasterPage's ContentPlaceholder first before you could find the control via FindControl:

Page.Master.FindControl("ContentPlaceHolder1").FindControl("TextBox1");

http://msdn.microsoft.com/en-us/library/ xxwa0ff0.aspx

可是你为什么不干脆直接引用您的控制权?例如:

But why don't you simply reference your control directly? For example:

this.TextBox1.Text = "Hello World";

顺便说一句,这是从我自己对类似的问题答案的。

这篇关于找不到文本框的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆