使用母版页时如何从当前页中找到控件 [英] How to find control from current page when use masterpage

查看:92
本文介绍了使用母版页时如何从当前页中找到控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 当我在不使用母版页的情况下进行页面处理时.
我只是通过
找到控制权

Hi When i am working on page without using master page.
I find control by simply

this.page.findControl("txtcontrol")



但是,当我将母版页和其他用户控件集成到该页面并尝试找到具有该控件的控件时.我正在null ..

帮助我.

在调试计数时快速查看this.page.conrols.count时,还有一件事总是为"1".
与以前一样,没有母版页计数的情况将作为页面控件的数量.

给我关于这个的任何想法

谢谢



but when i integrate master page and other user control to that page and try to find control with same. i am getting null..

help me on this.

one more thing when i am see this.page.conrols.count in quick view at a time of debug count always "1".
where as in previous case without masterpage count will as number of page controls.

give me any idea about this

Thanks

推荐答案

您只看到"1"控件,因为您正在搜索母版页,而不是其内容.

要在具有主页面的页面上找到控件,您必须执行以下操作:

You''re only seeing "1" control because you''re searching te Master Page, not its Content.

To find a control on a page that has a master, you have to do something more like this:

this.Master.FindControl("MainContentPlaceHolder").FindControl("MyControl")



当然,可以根据需要调整ID.

控件还具有一个唯一的ClientID,如下所示:



Adjusting the IDs as necessary, of course.

Controls also have a unique ClientID that looks like this:

ctl00_MainContentPlaceHolder_MyControl



在浏览器中执行查看源代码",以查看控件在运行时实际被命名的名称.您可以使用该ClientID进行FindControl,但第一种方法更干净.



Do a View Source in your browser to see what your controls are actually being named at runtime. You could do a FindControl using that ClientID, but it''s cleaner to do it the first way.


您需要在代码中搜索控件供用户控制,而不是母版页.母版页永远不需要知道有关从母版页显示的用户控件内部的任何信息,并且除了公共占位符名称之外,用户控件对母版页应该一无所知.如果您需要这样做,那么我建议您页面的固有设计不正确,并且解决您的问题的方法是纠正此设计问题,而不是尝试在错误的位置做事情.
You need to search for the control within the code behind for the user control, not the master page. The master page should never need to know anything about internals of the user controls that are being shown from the master page and the user controls should know nothing of the master page other than the public placeholder names. If you need to do this, then I would suggest that the inherent design of your pages is incorrect and that the solution to your problem is a correction of this design issue as opposed to trying to do things in the wrong place.


这篇关于使用母版页时如何从当前页中找到控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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