我正在开发自定义GridView Control.Which在标题中选择列表,就像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.HttpException'发生在System.Web.dll中但未在用户代码中处理 附加信息:文件' /AADQuickCustomFilter.ascx'不存在。
所以请帮助我,问题出在哪里。我做错了什么? 谢谢专家
答案 0 :(得分:0)
这是答案。 在First我创建类来注册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);
}
这就是你得到的所有,希望有用。 感谢专家的好评。