这两种情况之间是否存在差异:
(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
这不起作用,我错过了什么吗?我在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>
代码背后:
Response.Wrote(tx2.Text);
我没有,为什么我错过了什么?请注意,我也尝试了递归查找控件:
String str = ((TextBox)((Content)FindControl("Content2")).FindControl("tx2")).Text;
依然没有
解决方法
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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。