可以动态加载UserControl的ViewPanel控件
2006-12-10 13:12 Cat Chen 阅读(4398) 评论(35) 编辑 收藏 举报using System;
using System.Data;
using System.Configuration;
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;
namespace CatChen.Cocollab.Web.UI.Controls
{
public class ViewPanel : System.Web.UI.Control, System.Web.UI.INamingContainer
{
private string _virtualPath;
private Control _view;
public string VirtualPath
{
get { return this._virtualPath; }
set
{
string oldValue = this._virtualPath;
this._virtualPath = value;
if (this.Page != null && value != oldValue)
{
this.ChildControlsCreated = false;
this.EnsureChildControls();
}
}
}
public Control View
{
get { return this._view; }
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
if (!string.IsNullOrEmpty(this.VirtualPath))
{
this.EnsureChildControls();
}
}
protected override void LoadViewState(object savedState)
{
Pair pair = savedState as Pair;
if (pair != null)
{
base.LoadViewState(pair.First);
this.VirtualPath = pair.Second as string;
}
}
protected override void CreateChildControls()
{
this.Controls.Clear();
if (string.IsNullOrEmpty(this.VirtualPath))
{
return;
}
if (this.Page == null)
{
throw new Exception("ViewPanel.Page is null.");
}
this._view = this.Page.LoadControl(this.VirtualPath);
if (this._view == null)
{
throw new Exception("ViewVirtualPath cannot be loaded.");
}
this._view.ID = "ucView";
this.ClearChildState();
this.Controls.Add(this._view);
}
protected override object SaveViewState()
{
return new Pair(base.SaveViewState(), this.VirtualPath);
}
}
}
using System.Data;
using System.Configuration;
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;
namespace CatChen.Cocollab.Web.UI.Controls
{
public class ViewPanel : System.Web.UI.Control, System.Web.UI.INamingContainer
{
private string _virtualPath;
private Control _view;
public string VirtualPath
{
get { return this._virtualPath; }
set
{
string oldValue = this._virtualPath;
this._virtualPath = value;
if (this.Page != null && value != oldValue)
{
this.ChildControlsCreated = false;
this.EnsureChildControls();
}
}
}
public Control View
{
get { return this._view; }
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
if (!string.IsNullOrEmpty(this.VirtualPath))
{
this.EnsureChildControls();
}
}
protected override void LoadViewState(object savedState)
{
Pair pair = savedState as Pair;
if (pair != null)
{
base.LoadViewState(pair.First);
this.VirtualPath = pair.Second as string;
}
}
protected override void CreateChildControls()
{
this.Controls.Clear();
if (string.IsNullOrEmpty(this.VirtualPath))
{
return;
}
if (this.Page == null)
{
throw new Exception("ViewPanel.Page is null.");
}
this._view = this.Page.LoadControl(this.VirtualPath);
if (this._view == null)
{
throw new Exception("ViewVirtualPath cannot be loaded.");
}
this._view.ID = "ucView";
this.ClearChildState();
this.Controls.Add(this._view);
}
protected override object SaveViewState()
{
return new Pair(base.SaveViewState(), this.VirtualPath);
}
}
}