可以将Web部件加载到另一个内部吗?

时间:2009-01-07 08:52:58

标签: sharepoint web-parts

所以,这就是我们想要做的事情:我们想要一个带有自定义框架的通用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等,但这不是重点。

5 个答案:

答案 0 :(得分:4)

我不这么认为。我曾经尝试了一段时间,它抱怨只能在Page Init中添加WebPartZone项目。我认为,当初始化你的“容器”WebPart时,由于保留页面已经初始化,添加更多区域为时已晚。

答案 1 :(得分:4)

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的容器,只是一堆属性。我想更详细地讨论两个问题:

  1. parentWebPartGuid - 这是托管webpart的Guid(UniqueId?)。出于某种原因,我们必须使用反射(它是私有属性)将“StorageKeyInternal”设置为此值。您可以放弃而不设置它,但至少对于我们必须设置它的大多数webpart而言。

  2. config.Properties - 这是配置值(我们将它们设置在自定义.xml文件中,但随时随地都可以获得)。它看起来有点像this ..

  3. 在我们的框架中,我们也支持动态属性值等内容,但这是另一天...希望这一切都有意义,可以帮助某人。

答案 2 :(得分:2)

有(至少)两种方法可以做到这一点:使用iframe HTML元素,或者只是一个内容由JavaScript更改的div(可能是Ajax)。

[注意]我的答案是通用的(即在网页设计方面),我不知道你的技术背景如何,所以也许我应该删除这个答案......

答案 3 :(得分:2)

没有机会获得WebPartFactory类的源代码吗?或者可能有更多关于它的信息?可能是伪代码?如果自定义Web部件位于库中,则可以使用与RSSViewer正确相同的方式引用它?我只是不确定如何去做你在这里所做的事情,我非常想更好地理解如何做到这一点。

谢谢!

答案 4 :(得分:0)

当想要在另一个自定义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);
相关问题