我有两个页面page1.aspx和page2.aspx,两个都有部分类的代码.
如何从page2.aspx访问page1.aspx上的公共属性消息?
如何从page2.aspx访问page1.aspx上的公共属性消息?
public string message { get; set; }
解决方法
如果一个是主页面,另一个是使用主页面的页面.
主页
<body> <form id="form1" runat="server"> <div> <asp:Literal runat="server" ID="txtOnMaster"></asp:Literal> <br /> <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server"> </asp:ContentPlaceHolder> </div> </form> </body>
和背后的代码
public partial class Dokimes_StackOverFlow_MasterPage : System.Web.UI.MasterPage { public string TextToMaster { get { return txtOnMaster.Text; } set { txtOnMaster.Text = value; } } protected void Page_Load(object sender,EventArgs e) { // here I find the control in the client page Control FindMe = ContentPlaceHolder1.FindControl("txtOut"); // and if exist I set the text to client from the master if (FindMe != null) { ((Literal)FindMe).Text = "Get from Master Page"; } } }
现在是具有previus母版页的Page1.aspx
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> <asp:Literal runat="server" ID="txtOut"></asp:Literal> </asp:Content>
和代码
protected void Page_Load(object sender,EventArgs e) { // here I set the text on master page from client ((Dokimes_StackOverFlow_MasterPage)Master).TextToMaster = "Set from Client"; }