动态加载用户控件的组件!(转)
我们写用户控件的目的就是放到页面中去。根据不同的条件,我们可以改变加载的用户控件!
其实原理就是MasterPage的原理。这个MasterPage会在vs2005中提供,但是现在也可以实现。
在我的项目中就用到:
MasterPage就四个类,其中容器就动态加载用户控件。这种功能在vs2005中将无处不在。
下面简单介绍一下动态加载叶面组件:
我们只需要改变用户组件的地址就可以动态改变加载的用户组件了。
在程序中动态改变SkinPath就可以了。
组件源码如下:
using System;
//using System.Drawing;
using System.Collections;
using System.Collections.Specialized;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.IO;
using System.Web.Security;
using Region.Components;
namespace Region.Controls {
[ParseChildren(true)]
/// <summary>
/// 页面组件,从用户组件获取
/// </summary>
public abstract class SkinnedControl : WebControl, INamingContainer {
string skinFilename = null;
string skinName = null;
string skinPath;
// string returnURL = null;
/// <summary>
/// 添加子组件
/// </summary>
protected override void CreateChildControls() {
if(SkinPath == null || SkinPath == "" || SkinPath.Trim() == "")
return ;
Control skin;
skin = LoadSkin();
InitializeSkin(skin);
Controls.Add(skin);
}
/// <summary>
/// 获取摸板组件
/// </summary>
/// <returns>组件</returns>
protected Control LoadSkin()
{
Control skin;
try
{
skin = Page.LoadControl(SkinPath);
}
catch (FileNotFoundException)
{
throw new Exception("找不到文件:[ " + SkinPath + " ] .");
}
return skin;
}
/// <summary>
/// 初始化组建摸板
/// </summary>
/// <param name="skin">组件</param>
protected abstract void InitializeSkin(Control skin);
/// <summary>
/// 用户组件名称
/// </summary>
public string SkinFilename
{
get
{
return skinFilename;
}
set
{
skinFilename = value;
}
}
/// <summary>
/// 组件名称
/// </summary>
protected string SkinName
{
get
{
return skinName;
}
set
{
skinName = value;
}
}
/// <summary>
/// 用户组件相对路径及组件名称
/// </summary>
public string SkinPath
{
get
{
return skinPath;
}
set
{
skinPath = value;
SkinFilename = value.TrimStart('/');
}
}
}
}
比如我的一个应用:
switch (context.PageId)
{
case "1_1" :
base.SkinPath = "~/Themes/SignManager/TermList.ascx";
break ;
case "1_1_1" :
base.SkinPath = "~/Themes/SignManager/TermEdit.ascx";
break ;
case "1_2" :
base.SkinPath = "~/Themes/SignManager/MemberList.ascx";
break ;
case "1_2_1" :
base.SkinPath = "~/Themes/SignManager/MemberEdit.ascx";
break ;
case "2_1" :
base.SkinPath = "~/Themes/SignManager/CardSend.ascx";
break ;
case "3_1" :
base.SkinPath = "~/Themes/SignManager/MemberNoEdit.ascx";
break ;
case "4_1" :
base.SkinPath = "~/Themes/AcountEdit.ascx";
break ;
default :
base.SkinPath = "";
break;
}
基于组建的开发,我们可省去叶面上很多重复性的地方。只需在重复叶面的地方引用组件就可以了
比如:
这样我们就可以 以用户组件为元素,组件不同的叶面。叶面也可以重用!
<%@ Register TagPrefix="Region" Namespace="Region.Controls" Assembly="Region.Controls" %><%@ Register TagPrefix="Region" Namespace="Region.Controls" Assembly="Region.Controls" %><%@ Register TagPrefix="Region" Namespace="Region.Controls" Assembly="Region.Controls" %><%@ Register TagPrefix="igtbl" Namespace="Infragistics.WebUI.UltraWebGrid" Assembly="Infragistics.WebUI.UltraWebGrid.v3" %><%@ Page %><%@ Register TagPrefix="igtbl" Namespace="Infragistics.WebUI.UltraWebGrid" Assembly="Infragistics.WebUI.UltraWebGrid.v3" %><%@ Page %><%@ Register TagPrefix="igtbl" Namespace="Infragistics.WebUI.UltraWebGrid" Assembly="Infragistics.WebUI.UltraWebGrid.v3" %><%@ Page %>Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=60677
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Web;
using System.Web.UI;
using System.Web.Caching;
using System.Web.UI.Design;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.ComponentModel;
using System.ComponentModel.Design;
using Region.Components;
namespace Region.Controls {
/// <summary>
/// 加载用户组件,加载不同的组件
/// </summary>
public class LoadSkin : SkinnedControl {
public LoadSkin() : base()
{
}
/// <summary>
/// 初始化组建摸板,进行必要的动态设置或更新
/// </summary>
override protected void InitializeSkin(Control skin) {
skin.DataBind();
}
/// <summary>
/// Design time hack to get the control to render.
/// </summary>
public override ControlCollection Controls {
get {
EnsureChildControls();
return base.Controls;
}
}
}
}