[英]How to find the Parent of a CustomControl from inside the CustomControl
[英]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.