Ayy*_*ash 7 asp.net master-pages nested
这两种情况之间是否存在差异:
(1)从普通孩子访问母版页上的属性
(2)从嵌套母版页访问母版页上的属性
我试图从内容页面访问母版页中的文本框,如下所示:
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
Run Code Online (Sandbox Code Playgroud)
它可以工作,但是当我在嵌套的母版页上应用相同的方法时:
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
Run Code Online (Sandbox Code Playgroud)
这不起作用,我错过了什么吗?我在resumere page_load处理程序中调用这两个代码...
我也注意到我无法在代码后面的嵌套母版页中设置文本框值,肯定有一些东西我不见了,它是什么?为了阐明这个问题,这里有一个例子:
嵌套母版页:
<%@ 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>
Run Code Online (Sandbox Code Playgroud)
代码背后:
Response.Wrote(tx2.Text);
Run Code Online (Sandbox Code Playgroud)
我没有,为什么我错过了什么?请注意,我也尝试了递归查找控件:
String str = ((TextBox)((Content)FindControl("Content2")).FindControl("tx2")).Text;
Run Code Online (Sandbox Code Playgroud)
依然没有
小智 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
Run Code Online (Sandbox Code Playgroud)
我在这里读了一些东西: http://www.odetocode.com/Articles/450.aspx ,发现中间的嵌套页面永远不会调用Page_Load!相反,它会触发一个加载事件,您可以捕获该事件来设置任何字段,因此答案是:在嵌套页面上执行以下操作:
protected override void OnLoad(EventArgs e)
{
myTextBox.Text = "anything";
base.OnLoad(e);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10078 次 |
| 最近记录: |