使用母版页时访问 C# 中的内容控件
大家好,
我正在 ASP.NET 中构建一个页面,并在此过程中使用母版页。
我的母版页中有一个内容占位符名称“cphBody”,它将包含该母版页作为母版页的每个页面的正文。
在 ASP.NET 网页中,我有一个 Content 标记(引用“cphBody”),其中还包含一些控件(按钮、Infragistics 控件等),并且我想在 CodeBehind 文件中访问这些控件。 但是,我无法直接执行此操作(this.myControl ...),因为它们嵌套在 Content 标记中。
我找到了 FindControl 方法的解决方法。
ContentPlaceHolder contentPlaceHolder = (ContentPlaceHolder) Master.FindControl("cphBody");
ControlType myControl = (ControlType) contentPlaceHolder.FindControl("ControlName");
效果很好。 但是,我怀疑这不是一个很好的设计。 你们知道一种更优雅的方法吗?
谢谢你!
纪尧姆·热尔韦.
Good day everyone,
I am building a page in ASP.NET, and using Master Pages in the process.
I have a Content Place Holder name "cphBody" in my Master Page, which will contain the body of each Page for which that Master Page is the Master Page.
In the ASP.NET Web page, I have a Content tag (referencing "cphBody") which also contains some controls (buttons, Infragistics controls, etc.), and I want to access these controls in the CodeBehind file. However, I can't do that directly (this.myControl ...), since they are nested in the Content tag.
I found a workaround with the FindControl method.
ContentPlaceHolder contentPlaceHolder = (ContentPlaceHolder) Master.FindControl("cphBody");
ControlType myControl = (ControlType) contentPlaceHolder.FindControl("ControlName");
That works just fine. However, I am suspecting that it's not a very good design. Do you guys know a more elegant way to do so?
Thank you!
Guillaume Gervais.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我会尽量避免使用 FindControl,除非别无选择,而且通常有更简洁的方法。
如何在子页面顶部包含母版页的路径
,这将允许您直接从母版页代码后面调用代码。
然后,从后面的母版页代码中,您可以使属性返回您的控件,或者使母版页上的方法获取您的控件等。
指的是母版页上的标签用法
:
I try and avoid FindControl unless there is no alternative, and there's usually a neater way.
How about including the path to your master page at the top of your child page
Which will allow you to directly call code from your master page code behind.
Then from your master page code behind you could make a property return your control, or make a method on the master page get your control etc.
Refers to a label on the master page
Usage:
Rick Strahl 在这里有一个很好的解释(和示例代码) - http://www .west-wind.com/Weblog/posts/5127.aspx
Rick Strahl has a good explanation (and sample code) here - http://www.west-wind.com/Weblog/posts/5127.aspx
没什么可做的不同。 只需在子页面上编写此代码即可访问母版页标签控件。
Nothing to do different. Just write this code on child page to access the master page label control.
我使用此代码递归访问文件:
I use this code for acess to files recursively:
嗨,我只是想分享我的解决方案,发现这适用于访问 << 内部的“控件”。 asp:面板> 它位于“ContentPage”上,但来自“MasterPage”后面的 C# 代码。 希望它能帮助一些人。
添加一个< asp:面板> 在您的 ContentPage 中添加 ID="PanelWithLabel" 和 runat="server"。
在面板内,添加
asp:标签> 使用 ID="MyLabel" 进行控制。
在母版页代码隐藏中编写(或复制/粘贴以下内容)一个函数,如下所示:(这将访问面板内的标签控件,这些控件都位于内容页上,从母版页代码隐藏并更改它的文本是母版页上文本框的文本:)
Hi just thought i'd share my solution, found this works for accessing a 'Control' that is inside an < asp:Panel> which is on a 'ContentPage', but from C# code-behind of the 'MasterPage'. Hope it helps some.
add an < asp:Panel> with an ID="PanelWithLabel" and runat="server" to your ContentPage.
inside the Panel, add an < asp:Label> control with ID="MyLabel".
write (or copy / paste the below) a function in your MasterPage Code-behind as follows: (this accesses the label control, inside the Panel, which are both on the ContentPage, from the Master page code-behind and changes its text to be that of a TextBox on the Master page :)