zoukankan      html  css  js  c++  java
  • 疑问:AddWebPart 不能添加动态加载的自定义控件吗?

    AddWebPart 不能添加动态加载的自定义控件吗?

    做了一个自定义控件。强名称。动态加载添加到页面上是可以的。

               
    Assembly asembly = Assembly.LoadFile(@"C:\Inetpub\wwwroot\WebApplication2\WebApplication2\UILib\MyWebControls.dll");
                object obj = asembly.CreateInstance("MyWebControls.TestText");
                con = LoadControl(obj.GetType(), null);
                con.ID = "ddd";

                this.Panel1.Controls.Add(con);

    但是,如果添加到 WebPartManager里就会出问题:

                Assembly asembly = Assembly.LoadFile(@"C:\Inetpub\wwwroot\WebApplication2\WebApplication2\UILib\MyWebControls.dll");
                object obj = asembly.CreateInstance("MyWebControls.TestText");
                con = LoadControl(obj.GetType(), null);
                con.ID = "ddd";

                this.WebPartManager1.AddWebPart( this.WebPartManager1.CreateWebPart(con), this.WebPartZone1, 0);

    报错:
    不能添加 MyWebControls.TestText 类型的控件。类型必须能够被 BuildManager.GetType(string typeName)加载。

    请问这是怎么回事,怎么解决?肯请解答。多谢。

    源码下载地址: http://ldhyyiqi.cn/upload/动态添加自定义用户控件到%20WebPart.rar

    用Reflector 5.0 分析类 WebPartMananger 如下:(只贴主要代码)

    Reflector code.

    分析到这里,就有很多问题了.
    typeof(
    BuildManager).Assembly.GetType(typeName, false, ignoreCase);    
    这个函数是报错的.单纯的拿这个函数来讲, 我想在指定的程序集里创建对象,但这个函数没有指定程序集.所以只能说,走到这一步,是不对的.
    对此,问题似乎有些眉目,
    如何让 AddWebPart 函数走正确的路.去正确创建.
    顺带着的问题是. typeof(
    BuildManager).Assembly.GetType 这个函数如何使用.

    暂时分析到这里.欢迎有相关经验的朋友提出宝贵意见。



    问题解决。创建方法如下:
    1.把DLL放到 BIN 文件夹下。
    2.把生成的代码改为:

                Type t = BuildManager.GetType("MyWebControls.TestText", true);
                object obj = t.Assembly.CreateInstance("MyWebControls.TestText");
                WebControl con = (WebControl)obj;
                con.ID = "ddd";
                Response.Write(t.FullName);

                GenericWebPart gweb = this.WebPartManager1.CreateWebPart(con) ;
               Response.Write( gweb.IsStatic ) ;

               this.WebPartManager1.AddWebPart(gweb, this.WebPartZone1, 0);

    现在,还不清楚是怎么回事。呵。让我给撞上了。哪位朋友给解释一下,BuildManager.GetType 的寻找路径方式?多谢。

    如果我想把 DLL 文件放到自定义的文件夹下,该怎么办呢?


    (需设置 AppDomainSetup.PrivateBinPath 。)

  • 相关阅读:
    弄清变量名字空间
    Perl中文编码的处理
    了解魔符的含义
    Log::Minimal 小型可定制的log模块
    Perl – 文件测试操作符
    在源代码中使用Unicode字符
    editplus乱码charset的奇怪问题
    ASP.NET程序中常用代码汇总(一)
    ASP.NET程序中常用代码汇总(三)
    ASP.NET程序中常用代码汇总(二)
  • 原文地址:https://www.cnblogs.com/newsea/p/861417.html
Copyright © 2011-2022 走看看