这两个情形有什么区别:
(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
这个方法可以工作,但是当我在一个嵌套的母版页上应用相同的方法时:
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
这个不起作用,我有什么遗漏吗?我在普通的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>
代码后台:
Response.Wrote(tx2.Text);
我什么都没有得到,为什么?我错过了什么吗?请注意,我还尝试了递归查找控件:
String str = ((TextBox)((Content)FindControl("Content2")).FindControl("tx2")).Text;
仍然没有任何东西