所以,这就是我们想要做的事情:我们想要一个带有自定义框架的通用Web部件,然后在其中动态加载其他Web部件(无框架)。你觉得这有可能吗?有一点像 Jan Tielens SmartPart,仅适用于ASP.Net用户控件,但适用于其他Web部件...;)
编辑:我们现在已经能够做到这一点。解决方案实际上非常简单。看看代码:
public class WebPartWrapper : System.Web.UI.WebControls.WebParts.WebPart {
protected override void CreateChildControls() {
Panel pnl = new Panel();
this.Controls.Add(pnl);
WebPart dynamicPart = WebPartFactory.CreateWebPart("RSSViewer");
pnl.Controls.Add(dynamicPart);
}
}
这样简单...我们也使用反射将webparts存储为Xml等,但这不是重点。
我不这么认为。我曾经尝试了一段时间,它抱怨只能在Page Init中添加WebPartZone项目。我认为当初始化你的“容器”WebPart时,由于保留页面已经初始化,所以添加更多区域为时已晚。
public class WebPartWrapper : System.Web.UI.WebControls.WebParts.WebPart {
protected override void CreateChildControls() {
Panel pnl = new Panel();
this.Controls.Add(pnl);
var factory = new WebPartFactory()
WebPart dynamicPart = factory.CreateWebPart("RSSViewer", this.Guid);
pnl.Controls.Add(dynamicPart);
}
}
public class WebPartFactory {
public WebPart CreateWebpart(string webpartName, Guid parentWebPartGuid)
{
var config = ConfigurationFactory.LoadConfiguration(webpartName);
Assembly webPartAssembly = Assembly.Load(config.Assembly);
Type webPartType = webPartAssembly.GetType(config.Class);
object actualWebPart = Activator.CreateInstance(webPartType);
foreach (var item in config.Properties)
{
PropertyInfo webPartProperty = webPartType.GetProperty(item.Name);
object webPartPropertyValue = Convert.ChangeType(itemValue, Type.GetType(item.Type));
if (!String.IsNullOrEmpty(item.Value))
webPartProperty.SetValue(actualWebPart, webPartPropertyValue, null);
}
RunMethod("set_StorageKeyInternal", actualWebPart, new object[] { parentWebPartGuid });
return actualWebPart as WebPart;
}
private void RunMethod(string methodName, object objectInstance, object[] methodParameters)
{
BindingFlags flags = BindingFlags.Instance | BindingFlags.Public |
BindingFlags.NonPublic;
Type t = objectInstance.GetType();
MethodInfo m = GetMethod(t, methodName, flags);
if (m != null)
{
m.Invoke(objectInstance, methodParameters);
}
}
private MethodInfo GetMethod(Type instanceType, string methodName, BindingFlags flags)
{
MethodInfo m = instanceType.GetMethod(methodName, flags);
if (m != null)
{
return m;
}
if (instanceType.GetType() == typeof(object) || instanceType.BaseType == null)
{
return null;
}
return GetMethod(instanceType.BaseType, methodName, flags);
}
}
这段代码需要一些解释...请原谅我如果不编译,我不得不删除一些原始代码,这是非常具体的实现。我也没有显示“config”类,它只是一个用于配置webparts的容器,只是一堆属性。有两个问题我想更详细地讨论:
parentWebPartGuid - 这是托管webpart的Guid(UniqueId?)。出于某种原因,我们必须使用反射(它是私有属性)将“StorageKeyInternal”设置为此值。你可能没有设置它,但至少对于我们必须设置它的大多数webparts。
config.Properties - 这是配置值(我们在自定义的.xml文件中设置它们,但随时随地都可以获得)。它看起来有点像 这个..
在我们的框架中,我们也支持诸如动态属性值之类的东西,但那是另一天...希望这一切都有意义并且可以帮助某人。
有(至少)两种方法可以做到这一点:使用iframe HTML元素,或者只是一个内容由JavaScript更改的div(可能使用Ajax)。
[注意]我的答案是通用的(即在网页设计方面),我不知道你的技术背景如何,所以也许我应该删除这个答案......
没有机会获得WebPartFactory类的源代码吗?或者可能有更多关于它的信息?可能是伪代码?如果自定义Web部件位于库中,则可以使用与RSSViewer正确相同的方式引用它?我只是不确定如何去做你在这里所做的事情,我非常想更好地理解如何做到这一点。
谢谢!
当想要在另一个自定义webpart中实例化自定义webpart时,我在.ascx中使用以下代码
<%@ Register tagPrefix="uc1" Namespace="Megawork.Votorantim.Intranet.Webparts_Intranet.LikeButton" Assembly="Megawork.Votorantim.Intranet, Version=1.0.0.0, Culture=neutral, PublicKeyToken=769156d154035602" %>
可以从webconfig或包文件(在清单选项卡中)从SafeControls行复制Namespace值和Assembly值:)
当我想实例化它dinammicaly(事实上)是在.cs中使用以下代码
//This is the namespace of the control that will be instantiated dinamically
string type = "My.Custom.Namespace.WebpartToBeAdded.WebpartToBeAdded";
// Instantiate the control dinamically based on his type
System.Web.UI.WebControls.WebParts.WebPart genericWP = (System.Web.UI.WebControls.WebParts.WebPart)Activator.CreateInstance(Type.GetType(type));
// sets the page to the genericWP (i dont know if this is required)
genericWP.Page = this.Page;
// Note: if you want to call custom methods of the dinamically instantiated controls (like a custom load method) you will need to create an interface and make your dinamically instantiated webpart implement it. You will need to do it in that file that have the following code: private const string _ascxPath @"~/_CONTROLTEMPLATES/...". Then you can do the following
//IMyInterface ig = (IMyInterface)genericWP;
//ig.MyCustomLoadMethod(someParam);
// Adds the controls to a container, an asp panel by example.
panelDinamicControls.Controls.Add(genericWP);