母版页:MasterPage.master:

<body>
    <form id="form1" runat="server">    
                <asp:Button ID="aa" runat="server" OnClick="aa_Click"
                    Text="调用内容页方法" />

<asp:Label ID="message" runat="server" Text="这里将显示信息"></asp:Label></td>
    </form>
</body>

添加类 BasePage.cs:

public class BasePage :Page
{
    /// <summary>
    /// 输出每个内容页方法的内容
    /// </summary>
    public virtual string SayHello()
    {
        return "这是页面基类返回的信息!";
    }
}

母版页的后台代码如下:
    BasePage currentPage = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        currentPage = Page as BasePage;
    }
    protected void aa_Click(object sender, EventArgs e)
    {
        if (currentPage != null)
        {
            message.Text = currentPage.SayHello();
        }
    }

 

内容页(子页面)

Default.aspx.cs:
//注意类一定要继承自自定义的基类,否则母版页中转型会失败
public partial class Child_Default : BasePage
//重写欢迎信息方法
    public override string SayHello()
    {
        return "这是来自子页的信息!";
    }

posted on 2014-05-23 15:19  凝枫霜颖  阅读(223)  评论(0编辑  收藏  举报