这两种情况是否有区别:
(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
这不起作用,我错过了什么吗?我在regulare 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;
依然没有