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
4 个解决方案
#1
5
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
#2
4
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! 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:
我在这里读了几件东西:http://www.odetocode.com/Articles/450.aspx,发现中间的嵌套页面从不调用Page_Load!相反,它会触发一个你可以捕获的load事件来设置任何字段,所以答案是:在嵌套页面上执行以下操作:
protected override void OnLoad(EventArgs e)
{
myTextBox.Text = "anything";
base.OnLoad(e);
}
#3
2
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. It should be the type of your master page.
我会再次检查你是否在嵌套母版页中引用了正确的母版页。您还可以检查嵌套母版页中Master属性的运行时类型。它应该是您的母版页的类型。
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.
编辑:我认为问题是从嵌套母版页在根母版页中找到一个控件,这应该没有任何问题。要在嵌套母版页中的内容占位符中查找控件,请查看以下论坛帖子。
#4
0
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中的精彩文章
#1
5
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
#2
4
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! 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:
我在这里读了几件东西:http://www.odetocode.com/Articles/450.aspx,发现中间的嵌套页面从不调用Page_Load!相反,它会触发一个你可以捕获的load事件来设置任何字段,所以答案是:在嵌套页面上执行以下操作:
protected override void OnLoad(EventArgs e)
{
myTextBox.Text = "anything";
base.OnLoad(e);
}
#3
2
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. It should be the type of your master page.
我会再次检查你是否在嵌套母版页中引用了正确的母版页。您还可以检查嵌套母版页中Master属性的运行时类型。它应该是您的母版页的类型。
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.
编辑:我认为问题是从嵌套母版页在根母版页中找到一个控件,这应该没有任何问题。要在嵌套母版页中的内容占位符中查找控件,请查看以下论坛帖子。
#4
0
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中的精彩文章