繁体   English   中英

如何在CustomControl中使用UserControl?

[英]How to Use UserControl Inside CustomControl?

我正在开发自定义GridView控件。在表头中有选择列表,就像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'不存在。 在此处输入图片说明

因此,请帮助我,问题出在哪里。 我做错了什么? 谢谢专家

这是答案。 首先,我创建一个类来注册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);
    }

这就是您所拥有的,希望对您有用。 感谢专家们的好评。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM