zoukankan      html  css  js  c++  java
  • 动态加载用户控件(整理)

    微软示例:
    就像您可以通过编程方式在 Web 窗体页上创建任意 ASP.NET 服务器控件的实例,您也可以通过使用包含页的 LoadControl 方法来做到这一点。但您首先必须使用@ Control 指令的className 属性将强类型与用户控件相关联。之所以需要这样是因为LoadControl 方法返回 Control 类的类型,并且您需要将该用户控件转换为合适的强类型,以便设置该控件的各个属性。
    以下代码使用 className 属性将 MyUserControl.ascx 文件中保存的用户控件转换为强类型。

    <%@ Control className="MyUserControl" %>


    以编程方式创建用户控件的实例

    1. 使用@ Reference 指令在要包含用户控件的 Web 窗体页的顶部注册该用户控件。当以编程方式创建用户控件时,只有您创建了对该控件的引用后,用户控件的强类型才可用于该 Web 窗体页。例如,以下代码创建对 MyUserControl.ascx 文件中所创建用户控件的引用。
      <%@ Reference Control="MyUserControl.ascx" %>
      注意 当以声明方式在 Web 窗体页中创建用户控件的实例时,请使用@ Register 指令。
    2. 在代码隐藏类文件中,或是在包含 .aspx 文件的代码声明块中,创建用户控件的实例。根据需要分配属性值,并使用 Add 方法将该控件添加到包含页的 ControlCollection 对象上。这使该控件可用于该页的继承的 Control.Controls 属性。在以下示例中,创建 MyUserControl.ascx 的实例并将其 BackColor 属性设置为 beige。
      [C#]
      Control c1 = LoadControl("MyUserControl.ascx");
      ((MyUserControl)c1).BackColor = "beige";
      Page.Controls.Add(c1);
      [Visual Basic]
      Dim c1 As UserControl = LoadControl("MyUserControl.ascx")
      CType(c1, MyUserControl).BackColor = "beige"
      Page.Controls.Add(c1)
      注意 当您使用 Add 方法将控件添加到 ControlCollection 对象时,这些控件将按处理顺序放置在集合中。如果您希望将控件添加到集合中的特定位置,请使用 AddAt 方法并指定您要存储该控件的索引位置。



    地址:
    ms-help://MS.VSCC/MS.MSDNVS.2052/cpguide/html/cpconinstantiatingusercontrolsprogrammatically.htm

    鸿雪示例:
    注: 本方法是我同事李强原创

    步骤一: 把ascx控件拖入设计窗口,然后转入html模式,保留定义
    如果你需要多个ascx的话,请全部拖入。
    但是把相应的内容删除。(因为我们需要动态加载)
    在需要填入控件的地方,放个容器,比如td,并设定在服务器端运行

        <TD  id="tdpan" runat=server></TD>

    2:动态调用
    UserControl myusercontrol = (UserControl) LoadControl ("../includes/pageNavigater.ascx") ;
                Type myusertype = myusercontrol.GetType();
    //下面是给ascx赋值
                PropertyInfo  myuserinfo1 = myusertype.GetProperty("RelatedDatagrid");//) .GetProperty("RelatedDatagrid");
                myuserinfo1.SetValue(myusercontrol,gridhwcy  ,null);

                
                PropertyInfo  mypassinfo = myusertype.GetProperty("torefresh");
                mypassinfo.SetValue(myusercontrol,true,null );
                
                PropertyInfo  myuserdatasource = myusertype.GetProperty("RelatedDataSource");
                myuserdatasource.SetValue(myusercontrol,dv,null);

                tdpan.Controls.Clear();
                tdpan.Controls.Add(myusercontrol);

    如果还有什么不明白,请和  H.xue@163.net 联系          
    地址:
    http://www.dev-club.com/club/bbs/showAnnounce.asp?id=1506069     

    ASP.net中动态加载用户控件

      UserControl control = (_pager.LoadControl(String.Format("~/WebUC/{0}.ascx", _ctrlId))) as UserControl;
      control.ID = "uc_" + _ctrlId;
      _panelControl.Controls.Add(control);

    ASP.net中动态加载用户控件时一些问题的总结


    经常见到有人说在ASP.net中不要使用动态控件,我想主要的原因在于使用动态控件会带来一些问题,在做项目的过程中,我将由动态加载控件引发的总是作了一个小小的总结。

    1、在使用LoadControl加载控件后,用户控件中的某些控件不再响应事件。
    这个问题主要是由于将控件加载放在if (!Page.IsPostBack)之内引起的,放在外面即可。在思归的blog上对此问题进行了详细的说明。

    2、用户控件中某些控件的响应出现问题,如某个按钮第一次选择时不触发CLICK事件,第二次可以了。
    这是由于没有给控件设置ID引起的,控件ID的作用在下面详细讲述。 如 
      Control userControl=(Control)Page.LoadControl(“Test.ascx”);
      userControl.ID=“Test”;
      AddControl(userControl);

    3、如果用户控件中包括DataGrid控件,那么加载控件后可能出现不响应DataGrid事件的问题。
    这好像是一个bug,必须要将加载的控件进行强制转换,如:
      Test userControl=(Test)Page.LoadControl(“Test.ascx”); 
    注意:上面使用的是Test类型,而不是Control!
    我在以前的Blog中曾提到过这个问题,这种方式将使系统的扩展性降低。 我有一个解决方案可以和大家讨论(运用策略模式):
      public class BaseControl : System.Web.UI.UserControl
      {
        public virtual BaseControl ProcessThisControl();
      }
    所有的用户控件从BaseControl 继承,如果有Datagrid控件,由overide ProcessThisControl方法,如:
      return this as Test;

    按如下方式加载控件:
      BaseControl userControl=(BaseControl )Page.LoadControl(“Test.ascx”);
    userControl.ProcessThisControl();


    4、在用户控件中如何使用JavaScript

    大家都知道,使用客户端的脚本将大大提高页面的响应速度,同时可以避免频繁地刷新页面。所以使用javascript来实现页面中部分控制是一个比较好的方式,但是在用户控件中如果访问某一个子控件呢?

    使用方式如下:
      document.all.<%= TestControl.ClientID%><%= lstUser.ClientID%>.disabled=true;     //将TestControl设置为不可用
    如果在C#脚本中应该这样写:
      Page.RegisterStartupScript("OnInitControl","<SCRIPT LANGUAGE='JavaScript'>document.all.Test_TestControl.disabled=true;</SCRIPT>");   //Test为用户控件,TestControl为用户控件中的子控件。

    现在说一下控件ID,在访问aspx文件时,IIS会将aspx的脚本进行编译。编译的时候将用户控件中的内容写在同一个页面中,为了防止页面中的控件与用户控件中的控件名称相同,在编译的时候对用户控件中的控件名称修改为 :   用户控件名:子控件 ,控件ID则修改为 用户控件ID_子控件ID。在动态加载控件时,如果不对控件的ID进行赋值,则控件ID为上一次加载的控件ID,因此在加载用户控件后应该立即对其设置ID。

    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);
            }
        }
    }
  • 相关阅读:
    Lucene in action 笔记 case study
    关于Restful Web Service的一些理解
    Lucene in action 笔记 analysis篇
    Lucene in action 笔记 index篇
    Lucene in action 笔记 term vector
    Lucene in action 笔记 search篇
    博客园开博记录
    数论(算法概述)
    DIV, IFRAME, Select, Span标签入门
    记一个较困难的SharePoint性能问题的分析和解决
  • 原文地址:https://www.cnblogs.com/superfeeling/p/3471689.html
Copyright © 2011-2022 走看看