如何在asp.net的web页面中包含另外一个web页面

来源:百度知道 编辑:UC知道 时间:2024/05/13 06:34:47
如何在asp.net的web页面中包含另外一个web页面
asp里面是<!--#include file="xxx"-->
那.net里面呢?
是在引用了母板页面的content里面引用其它页面

用自定义控件,把要包含进去的内容写成一个自定义控件,
然后在主页面中调用自定义控件
例:
新建一个自定义控件c_topwin.ascx
内容和编写aspx文件一样
然后在要调用的页面最上面加入:
<%@ Register TagPrefix="topwin" TagName="c_topwin" Src="/control/c_topwin.ascx" %>

TagPrefix:控件前缀
TagName:控件名
Src:控件路径

最后在要加入控件的地方加入:
<topwin:c_topwin runat="server" id="c_topwin1"/>



<topwin:c_topwin runat="server" id="c_topwin1"></topwin>

另外也可以在程序中动态加载自定义控件
可使用PlaceHolder先占位,然后用PlaceHolder1.Controls.Add(Page.LoadControl("/control/c_topwin.ascx"));

但是动态加载的自定义控件在PostBack后内容会消失,解决方法如下:
加入PlaceHolder 名为ctl_main,在页面的Page_Load方法中:
if(!IsPostBack)
{
Control ctl=Page.LoadControl("/control/c_topwin.ascx");
ctl.ID="ctl_loaded";
this.ctl_main.Controls.Add(ctl);
ViewState["