的FindControl和作INamingContainer [英] FindControl and INamingContainer

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

问题描述

我想这个字仔细的问题,所以乐于助人的人不会跳,并花时间告诉我,我已经知道(我不想浪费自己的时间)。信息

I want to word this question carefully, so helpful people don't jump in and spend their time telling me information that I already know (I don't want to waste their time).

我想了解的FindControl如何在ASP.NET Web应用程序项目(其中C#的文件作为参考codeBehind的那些,不是codeFILE,在标记)。

I want to understand how FindControl works in ASP.NET web application projects (the ones where the c# files are referenced as CodeBehind, NOT CodeFile, in the markup).

code背后有两个文件,​​这坐标记文件之间。例如。 Default.aspx的将有Default.aspx.cs和Default.aspx.designer.cs

Code behind have two files which sit between the markup file. E.g. Default.aspx will have Default.aspx.cs and Default.aspx.designer.cs

如果你把一个页面上的一个按钮,它被添加到设计文件。例如:
保护全球:: System.Web.UI.WebControls.LinkBut​​ton LinkBut​​ton1;

If you put a button on a page, it is added to the designer file. For example: protected global::System.Web.UI.WebControls.LinkButton LinkButton1;

如果你想获得该控件的引用,它可立即作为默认类的成员。例如。 this.LinkBut​​ton1.Text =点击我;

If you want to get a reference to that control, it is immediately available as a member of the Default class. E.g. this.LinkButton1.Text = "Click Me";

如果您在查看该页面的跟踪,它被赋予一个唯一的ID为每行为INamingContainers(这里页):$​​ ctl00 $ ContentPlaceHolder1 LinkBut​​ton1

If you look at a trace for the page, it is given a unique id as per the behaviour for INamingContainers (here, the Page): ctl00$ContentPlaceHolder1$LinkButton1

我不明白的是为什么空是由语句返回:
控制C = Page.FindControl(LinkBut​​ton1);

The thing I don't understand is why a null is returned by the statement: Control c = Page.FindControl("LinkButton1");

我意识到这是不必要的,因为该按钮已经可以为默认类别。这是因为它出现在Default.aspx.designer.cs文件中的成员。

I realise this is unnecessary, as the button is already available to the Default class. And this is because it appears as a member in the Default.aspx.designer.cs file.

我不明白的事情是的为什么会返回空。因为页实现作INamingContainer,并且按钮具有相关于预期在作INamingContainer的控制的ID。这不是正是这种事情的FindControl找到?

The thing I do not understand is why null is returned. Because the Page implements INamingContainer, and the button has an ID which correlates to that expected of a control in an INamingContainer. Isn't this exactly the kind of thing FindControl finds?

推荐答案

本的行为是新的我,也许是因为我不会尝试搜索是直接访问反正控制。我想这也可能是为什么ASP.NET甚至允许这样做,是因为它的速度更快,更安全的使用现有的参考,而不是找到它(或没有)的原因。

This behaviour was new to me, maybe because i wouldn't try to search for a control that is directly accessible anyway. I think this might also be the reason why ASP.NET not even allow this, because it's faster and safer to use an existing reference than to find it (or not).

FindControl方法可以用来访问其ID的控制不
  在设计时。该方法只搜索页面的
  立即,或顶层,容器;它不递归搜索
  在命名包含在页面上的容器控件。访问
  处于从属的命名容器的控制,调用的FindControl
  该容器的方法。

The FindControl method can be used to access a control whose ID is not available at design time. The method searches only the page's immediate, or top-level, container; it does not recursively search for controls in naming containers contained on the page. To access controls in a subordinate naming container, call the FindControl method of that container.

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

修改:在我检查这种行为,我注意到如果与母版页面中使用只返回,因为只有在页面的的ControlCollection 控制是母版本身。
这就说得通了。你不能保证一个ID是唯一的,当控制与母版页的顶层,因为其他内容网页,可能也有这个ID的控制和的FindControl 能回到今天比明天另一个控制。

Edit: After i checked this behaviour, i've noticed that null is only returned if used on a Page with MasterPage, 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("LinkButton1");

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

这篇关于的FindControl和作INamingContainer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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