创建加载用户控件的 SharePoint 2013 Web 部件
Create SharePoint 2013 web part that loads user controls
我需要创建一个将用户控件加载到其中的 Web 部件。
我找到的所有答案都已过时或无效。
我正在使用 visual studio 2015 和 SharePoint 2013。
我用我的用户控件和其他 Web 部件创建了一个项目,但我不知道如何 link 它们...
有什么帮助吗?
谢谢
如果您的 webpart 不是可视化 webpart,您可以像这样加载用户控件(在 webpart cs 文件中):
namespace ProjectName.Folder.Custom_Webpart
{
[ToolboxItemAttribute(false)]
public class Custom_Webpart : System.Web.UI.WebControls.WebParts.WebPart
{
private const string _ascxPath = @"~/_CONTROLTEMPLATES/15/ProjectName/Folder/Custom_UserControl.ascx";
protected override void CreateChildControls()
{
var control = Page.LoadControl(_ascxPath) as Custom_UserControl;
//this.ChromeType = PartChromeType.None;
Controls.Add(control);
}
}
}
如果您的 Web 部件是可视化 Web 部件,您可以通过在 Web 部件 .ascx 文件中插入以下指令来加载用户控件:
<%@ Register Src="/_controltemplates/15/ProjectName/Folder/Custom_UserControl.ascx" TagPrefix="Custom" TagName="CustomControl" %>
<Custom:CustomControl runat="server" />
我需要创建一个将用户控件加载到其中的 Web 部件。 我找到的所有答案都已过时或无效。
我正在使用 visual studio 2015 和 SharePoint 2013。 我用我的用户控件和其他 Web 部件创建了一个项目,但我不知道如何 link 它们...
有什么帮助吗? 谢谢
如果您的 webpart 不是可视化 webpart,您可以像这样加载用户控件(在 webpart cs 文件中):
namespace ProjectName.Folder.Custom_Webpart
{
[ToolboxItemAttribute(false)]
public class Custom_Webpart : System.Web.UI.WebControls.WebParts.WebPart
{
private const string _ascxPath = @"~/_CONTROLTEMPLATES/15/ProjectName/Folder/Custom_UserControl.ascx";
protected override void CreateChildControls()
{
var control = Page.LoadControl(_ascxPath) as Custom_UserControl;
//this.ChromeType = PartChromeType.None;
Controls.Add(control);
}
}
}
如果您的 Web 部件是可视化 Web 部件,您可以通过在 Web 部件 .ascx 文件中插入以下指令来加载用户控件:
<%@ Register Src="/_controltemplates/15/ProjectName/Folder/Custom_UserControl.ascx" TagPrefix="Custom" TagName="CustomControl" %>
<Custom:CustomControl runat="server" />