【问题标题】:How to Use UserControl Inside CustomControl?如何在 CustomControl 中使用 UserControl?
【发布时间】:2017-02-05 10:43:30
【问题描述】:

我正在开发自定义 GridView 控件。它在标题中有选择列表,就像 excel 一样。作为像过滤器一样的excel,选项之一是“自定义过滤器”。在这种情况下,我想展示一个我设计为 ascx 的用户控件的小控件。 现在当我想在我的自定义控件中使用该用户控件时。我用这个代码:

 protected override void RenderContents(HtmlTextWriter writer)
    {

        UserControl uc = new UserControl();
        AADQuickCustomFilter CustomFilter = (AADQuickCustomFilter)uc.LoadControl("~/AADQuickCustomFilter.ascx");
        TextWriter tw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(tw);
        CustomFilter.RenderControl(hw);

        writer.Write(tw.ToString());
        base.RenderContents(writer);
    }

不幸的是,我收到了错误 System.Web.dll 中发生了“System.Web.HttpException”类型的异常,但未在用户代码中处理 附加信息:文件“/AADQuickCustomFilter.ascx”不存在。

所以请帮帮我,问题出在哪里。我做错了什么? 感谢专家

【问题讨论】:

  • 这里我覆盖了我的自定义控件的 RenderControl
  • 你必须从你的站点的根目录指定路径,你的 ascx 在那里吗?
  • 请帮我截取代码@CrudaLilium
  • 你的代码应该没问题,也许不要在开头使用“~/”,但这看起来你的 ascx 是库的一部分而不是你的站点,你能把你的 ascx 复制到你网站的根目录,看看它是否有效?
  • 您已将它包含在您的网格自定义控件中,对吗?这不会在您的应用程序的根目录中。由于我已经有十年没有使用此功能了,所以我无法提供太多指导,但是您需要将用户控件嵌入到 dll 中,并将其作为嵌入式资源引用。尽管将它创建为另一个服务器控件然后覆盖它的渲染方法以生成您需要的所有 html/css/js 可能会更容易。仅供参考,您需要将所有 .js 和 .css 文件嵌入到 dll 中,并将它们作为嵌入文件引用。

标签: c# asp.net dll user-controls custom-controls


【解决方案1】:

这是答案。 首先,我创建类以将 ascx 文件注册为嵌入资源,如下代码:

    using System.Web.Hosting;
using System.Web.Caching;
using System.Collections;
using System;
using System.IO;
using System.Web;
using System.Reflection;

namespace AADGridView
{
    public class AssemblyResourceProvider : VirtualPathProvider
    {
        string mResourcePrefix;

        public AssemblyResourceProvider() : this("EmbeddedWebResource")
        {
        }

        public AssemblyResourceProvider(string prefix)
        {
            mResourcePrefix = prefix;
        }

        private bool IsAppResourcePath(string virtualPath)
        {
            String checkPath = VirtualPathUtility.ToAppRelative(virtualPath);
            return checkPath.StartsWith("~/" + mResourcePrefix + "/",
                   StringComparison.InvariantCultureIgnoreCase);
        }

        public override bool FileExists(string virtualPath)
        {
            return (IsAppResourcePath(virtualPath) ||
                    base.FileExists(virtualPath));
        }

        public override VirtualFile GetFile(string virtualPath)
        {
            if (IsAppResourcePath(virtualPath))
                return new AssemblyResourceVirtualFile(virtualPath);
            else
                return base.GetFile(virtualPath);
        }

        public override CacheDependency
               GetCacheDependency(string virtualPath,
               IEnumerable virtualPathDependencies,
               DateTime utcStart)
        {
            if (IsAppResourcePath(virtualPath))
                return null;
            else
                return base.GetCacheDependency(virtualPath,
                       virtualPathDependencies, utcStart);
        }
    }

    class AssemblyResourceVirtualFile : VirtualFile
    {
        string path;

        public AssemblyResourceVirtualFile(string virtualPath) : base(virtualPath)
        {
            path = VirtualPathUtility.ToAppRelative(virtualPath);
        }

        public override Stream Open()
        {
            string[] parts = path.Split('/');
            string assemblyName = parts[2];
            string resourceName = parts[3];
            assemblyName = Path.Combine(HttpRuntime.BinDirectory, assemblyName);
            Assembly assembly = Assembly.LoadFile(assemblyName);
            if (assembly == null) throw new Exception("Failed to load " + assemblyName);
            Stream s = assembly.GetManifestResourceStream(resourceName);
            if (s == null) throw new Exception("Failed to load " + resourceName);
            return s;
        }
    }
}

然后像这样覆盖自定义控件的构造函数:

 public AADExcelFilterGridView() : base()


    {System.Web.Hosting.HostingEnvironment.RegisterVirtualPathProvider(new AssemblyResourceProvider(ResourcePrefix));
}

然后

 static string mResourcePrefix = "EmbeddedWebResource";

    public static string ResourcePrefix
    {
        get
        {
            return mResourcePrefix;
        }

        set
        {
            mResourcePrefix = value;
        }
    }

现在,只需将您的用户控件添加到您的自定义控件渲染中,就像上面一样:

 protected override void RenderContents(HtmlTextWriter writer)
    {

        UserControl uc = new UserControl();
        AADQuickFilter CustomFilter = (AADQuickFilter)uc.LoadControl("~/EmbeddedWebResource/AADGridView.dll/AADGridView.AADQuickFilter.ascx");
        TextWriter tw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(tw);
        CustomFilter.RenderControl(hw);

        writer.Write(tw.ToString());
        base.RenderContents(writer);
    }

这就是你所得到的,希望有用。 感谢非常好的专家们。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-29
    • 2013-02-24
    • 2014-01-03
    • 2016-07-26
    • 1970-01-01
    • 1970-01-01
    • 2011-06-24
    • 2011-04-21
    相关资源
    最近更新 更多