简体   繁体   English

如何访问嵌套母版页内的控件? 为什么它与内容页面的行为不同?

[英]How to access controls inside a nested master page? why does it behave differently from content pages?

Is there a difference between these two scenarios: 这两种情况之间是否存在差异:

(1) Accessing a property on a master page from a regular child (1)从普通孩子访问母版页上的属性

(2) Accessing a property on a master page from a nested master page (2)从嵌套母版页访问母版页上的属性

I tried to access a textbox in the master page from a content page like this: 我试图从内容页面访问母版页中的文本框,如下所示:

TextBox a;
a = (TextBox)Master.FindControl("ayyash"); // Master is declared in MasterType directive
defaultTextbox.Text = a.Text; // defaultTextBox is a textbox control inside default.aspx

it works, but then when I applied the same method on a nested master page: 它可以工作,但是当我在嵌套的母版页上应用相同的方法时:

TextBox a;
a = (TextBox)Master.FindControl("ayyash"); // Master is declared in MasterType directive
myTextBox.Text = a.Text; // myTextBox is a textbox control inside child.master

this does not work, am I missing something? 这不起作用,我错过了什么吗? I call both codes inside regulare page_load handler... 我在resumere page_load处理程序中调用这两个代码...

I also noticed I cannot set textbox value inside the nested master page from code behind, there is definitely something im missing, what is it? 我也注意到我无法在代码后面的嵌套母版页中设置文本框值,肯定有一些东西我不见了,它是什么? To shed light on this issue, here is an example: 为了阐明这个问题,这里有一个例子:

Nested Master Page: 嵌套母版页:

<%@ Master Language="C#" MasterPageFile="MasterPage.master" AutoEventWireup="false" CodeFile="MasterPage2.master.cs" Inherits="MasterPage2" %>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

<asp:textbox id="tx2" runat="server" text="this is two"></asp:textbox>
<asp:contentplaceholder id="newstuff" runat="server"></asp:contentplaceholder>
</asp:Content>

Code behind: 代码背后:

Response.Wrote(tx2.Text);

I get NOTHING, why what did I miss? 我没有,为什么我错过了什么? note that I also tried the recursive find control: 请注意,我也尝试了递归查找控件:

String str = ((TextBox)((Content)FindControl("Content2")).FindControl("tx2")).Text;

still nothing 依然没有

ContentPlaceHolder cp = (ContentPlaceHolder)this.Master.Master.FindControl("ContentPlaceHolder1");
  //base content place holder id

Label objLabel3 = (Label)cp.FindControl("lblNested");
  //lblNested is id in nested master page

I read few things here: http://www.odetocode.com/Articles/450.aspx and found out that the nested page in the middle never calls Page_Load! 我在这里读了几件东西: http//www.odetocode.com/Articles/450.aspx ,发现中间的嵌套页面从不调用Page_Load! instead, it fires a load event that you can catch to set whatever fields, so the answer was in: on nested page do the following: 相反,它会触发一个load事件,你可以捕获它来设置任何字段,所以答案是:在嵌套页面上执行以下操作:

protected override void OnLoad(EventArgs e)
    {
        myTextBox.Text = "anything";
        base.OnLoad(e);
    }

This should work without any problems, so something else must be wrong. 这应该没有任何问题,所以其他一些必须是错的。 I just tried it inside a simple test project and I have no problems finding a control on the master page in both cases. 我只是在一个简单的测试项目中尝试过,在两种情况下我都可以在母版页上找到控件。

I would check (again) if you refer to the correct master page inside your nested master page. 如果您在嵌套母版页中引用正确的母版页,我会再次检查。 What you could also check is the runtime type of the Master property inside your nested master page. 您还可以检查嵌套母版页中Master属性的运行时类型。 It should be the type of your master page. 它应该是您的母版页的类型。

EDIT : I thought the issue was about finding a control in the root master page from a nested master page and this should work without any problems. 编辑 :我认为问题是从嵌套母版页在根母版页中找到一个控件,这应该没有任何问题。 For finding a control inside a content placeholder in a nested master page, take a look at the following forum post . 要在嵌套母版页中的内容占位符中查找控件,请查看以下论坛帖子

You can have absolute control of your content in both master page and nested page from your content page using the directives: 您可以使用指令从内容页面对主页面和嵌套页面中的内容进行绝对控制:

<%@ MasterType VirtualPath="your_master.master" %>
<%@ Reference VirtualPath="~/your_master.master" %>

See the excellent article from K. Scott Allen in Ode To Code 请参阅K. Scott Allen在Ode To Code中的精彩文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从嵌套母版页中的内容页访问主母版页中的控件 - How to access controls in Main Master page from content page in Nested Master Page 如何从 NESTED 内容页面访问母版页公共属性 - How to access master page public property from a NESTED content page 如果Web应用程序具有嵌套的母版页,如何从另一个用户控件动态访问母版页上的控件 - How to access controls on a master page dynamically from another user control, if the web application has nested master page 如何从母版页访问页面控件? - How to access page controls from master page? asp.net如何从具有嵌套母版页的内容页更改父母版页中的控件 - asp.net how to change a control which is in parent master page, from content page with nested master pages 在嵌套母版页中查找控件 - Finding controls inside nested master pages 在内容页面的嵌套母版页面中读取用户控件属性 - Reading User control property inside Nested Master pages in Content page 如何从 asp.net 中的母版页访问内容页面控件 - How to access content page controls from master page in asp.net 从嵌套母版页调用内容页方法 - Calling a content page method from nested master pages 如何使用master从asp:content页面访问母版页上的用户控件? - How do you access user controls on a masterpage from the asp:content page using the master?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM