可以动态加载UserControl的ViewPanel控件

翻译|其它|编辑:郝浩|2007-03-07 11:10:27.000|阅读 1349 次

概述:

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>

程序代码如下所示:

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);
        }
    }
}


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com


为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP