10-21晚

母版页给我们带来了很多方便性,但也带来了一个问题:在设计母版页的时候,总会碰到需要在母版页中调用内容页的方法,但此时内容页尚未设计,如何调用呢?下面就让我用一个实例给大家讲解一下:
      母版页:MasterPage.master的页面代码如下:
<body>
    <form id="form1" runat="server">
    <table>
        <tr>
            <td>
                <asp:Button ID="CallContentMethod" runat="server" OnClick="CallContentMethod_Click"
                    Text="调用内容页方法" />
                <asp:Label ID="welcomeMessage" runat="server" Text="这里将显示内容页的欢迎信息"></asp:Label></td>
        </tr>
        <tr>
            <td>
                <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
                </asp:contentplaceholder>
            </td>
        </tr>
    </table>
    </form>
</body>
其中有一个按钮和一个文本,其中的文本用来显示内容页的欢迎信息,按钮用来调用内容页的方法,但此时内容页并不存在,那么按钮的单击事件代码如何写呢?
      这里我们需要引入一个页面基类的概念,想必大家都清楚;aspx页面只要求后台类是继承此Page类即可。所以我们先添加一个继承自Page的页面基类BasePage,然后让页面继承自我们BasePage并重写相关方法,利用运行时的动态性来解决此问题。相关代码如下:
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 CallContentMethod_Click(object sender, EventArgs e)
    {
        if (currentPage != null)
        {
            welcomeMessage.Text = currentPage.SayHello();
        }
    }
此处母版页中定义一个成员变量指向BasePage,在PageLoad中将实际运行的页面进行转型,这样在按钮的单击事件中就可以调用BasePage定义的SayHello方法了。又由于此方法是虚方法,在运行时绑定具体方法,即可实现我们的要求。
下面是内容页的代码:
Default.aspx.cs:
//注意类一定要继承自自定义的基类,否则母版页中转型会失败
public partial class Template_Default : BasePage
//重写欢迎信息方法
    public override string SayHello()
    {
        return "这是来自内容页的欢迎信息!";
    }

通过以上的步骤,我们就可以轻松实现在母版页中调用内容页的方法。如果想自动调用,只需要在母版页的Page_Load方法中转型后直接调用相关方法即可。

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/amanda1982/archive/2007/05/03/1595282.aspx

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

//Master页面
public partial class MasterPage : System.Web.UI.MasterPage
...{
    protected void Page_Load(object sender, EventArgs e)
    ...{

    }
    protected void btnMaster_Click(object sender, EventArgs e)
    ...{
        Label lbl = this.ContentPlaceHolder1.FindControl("lblContent") as Label;
        Response.Write("<script language='javascript'>alert('" + lbl.Text + "');</script>");
    }
}

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

//内容页面
public partial class Default2 : System.Web.UI.Page
...{
    protected void Page_Load(object sender, EventArgs e)
    ...{

    }
    protected void btnContent_Click(object sender, EventArgs e)
    ...{
        Label lbl = this.Master.FindControl("lblMaster") as Label;
        Response.Write("<script language='javascript'>alert('" + lbl.Text + "');</script>");
    }
}

--从Table 表中取出第 m 条到第 n 条的记录:(Not In 版本)
 2
 3 SELECT TOP n-m+1 *
 4 FROM Table
 5 WHERE (id NOT IN (SELECT TOP m-1 id FROM Table )) 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值