ASP.NET如何访问公共属性? [英] ASP.NET how to access public properties?

查看:197
本文介绍了ASP.NET如何访问公共属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个页面Page1.aspx的和page2.aspx,都与部分类身后code。
我如何进入公共财产上的消息从page1.aspx这个page2.aspx?

 公共字符串消息{搞定;组; }


解决方案

如果一个是母版页,另一种是使用主的页面。

母版页

 <身体GT;
    <表ID =form1的=服务器>
    < D​​IV>
        < ASP:文字=服务器ID =txtOnMaster>< / ASP:文字>
        < BR />
        < ASP:的ContentPlaceHolder ID =ContentPlaceHolder1=服务器>
        < / ASP:&的ContentPlaceHolder GT;
    < / DIV>
    < /表及GT;
< /身体GT;

和背后

在code

 公共部分类Dokimes_StackOverFlow_MasterPage:System.Web.UI.MasterPage
{
    公共字符串TextToMaster
    {
        {返回txtOnMaster.Text; }
        集合{txtOnMaster.Text =价值; }
    }
    保护无效的Page_Load(对象发件人,EventArgs的发送)
    {
        //这里我发现在客户端页面控制
        控制FindMe = ContentPlaceHolder1.FindControl(txtOut);        //如果存在,我将文本设置为客户从主
        如果(FindMe!= NULL)
        {
            ((文字)FindMe)。文本=从母版页获取;
        }
    }
}

和现在的Page1.aspx的具有previus母版页

 < ASP:内容ID =内容2ContentPlaceHolderID =ContentPlaceHolder1=服务器>
    < ASP:文字=服务器ID =txtOut>< / ASP:文字>
< / ASP:内容>

和code

 保护无效的Page_Load(对象发件人,EventArgs的发送)
{
    //这里我设置从客户主网页上的文字
    ((Dokimes_StackOverFlow_MasterPage)主).TextToMaster =从客户端设置;
}

I have two pages page1.aspx and page2.aspx, both have code behind with partial classes. How do i access public property message on page1.aspx from page2.aspx ?

public string message { get; set; }

解决方案

If the one is the Master page, and the other is the page that use the master.

The Master Page

<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>

and the code behind

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";
        }
    }
}

and now the Page1.aspx that have the previus master page

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:Literal runat="server" ID="txtOut"></asp:Literal>  
</asp:Content>

and the code

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";
}

这篇关于ASP.NET如何访问公共属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆