覆盖自定义表单模板中的 SharePoint:SaveButton

发布于 2024-10-21 02:35:07 字数 2115 浏览 2 评论 0原文

首先让我澄清一下,并发布已经解释如何覆盖 SaveButton 的文章:

我已阅读并理解它们,我只是不知道如何在我的特定情况下完全实现它:

我有一个自定义渲染模板“CustomRender”,其中包含“真实表单”。真实表单的代码看起来围绕这些行:

<%@ Register TagPrefix="wssuc" TagName="ToolBar"
             src="~/_controltemplates/ToolBar.ascx" %>
<%@ Control Language="C#" AutoEventWireup="true"
    CodeBehind="RealForm.ascx.cs" Inherits="CustomNameSpace.CustomForm" %>
<p>Test</p>
<wssuc:ToolBar runat="server" id="toolbar">
    <TemplateButtons>
        <SharePoint:SaveButton runat="server" />
    </TemplateButtons>
</wssuc:ToolBar>

现在我想覆盖这个保存按钮。上面的网站声明我只需要编写另一个覆盖按钮的控件。例如:

public class NewSaveButton: SaveButton

{
    protected override bool SaveItem()

    {
        bool success = base.SaveItem();

        RedirectUrl = String.Concat(List.ParentWeb.ServerRelativeUrl, "/",
                      List.Forms[PAGETYPE.PAGE_DISPLAYFORM].Url, @"?ID=", 
                      ListItem.ID, @"&Source=", ListItem.ParentList.DefaultViewUrl);
        return success;

    }
}

现在我只是不知道如何在我的其他模板中注册这个模板。我是否不能只覆盖模板后面代码中的 SaveButton - 我该如何做到这一点并稍后引用它?

  • 选项一:表单的代码隐藏 (RealForm.ascx.cs) - 我可以将覆盖方法放在那里吗?如何在表单中引用该按钮(如何获取 )?
  • 选项二:另一个仅用于按钮的模板,例如 SaveButton.ascx” - 我如何通过 <%@ Register... %> 引用该模板,即我如何知道 PublicKeyToken 等通过功能部署时。这里也是一样:我的目标是为表单获取某种“”控件。

First let me clear the air and post articles which already explain how to override the SaveButton:

I have read those and understood them, I just don't know how to fully implement it in my particular case:

I have a custom rendering template "CustomRender" which includes the "real form". The code for the real form looks something around these lines:

<%@ Register TagPrefix="wssuc" TagName="ToolBar"
             src="~/_controltemplates/ToolBar.ascx" %>
<%@ Control Language="C#" AutoEventWireup="true"
    CodeBehind="RealForm.ascx.cs" Inherits="CustomNameSpace.CustomForm" %>
<p>Test</p>
<wssuc:ToolBar runat="server" id="toolbar">
    <TemplateButtons>
        <SharePoint:SaveButton runat="server" />
    </TemplateButtons>
</wssuc:ToolBar>

Now I want to override this save button. The sites above state that I just have to write another control which overrides the button. E.g.:

public class NewSaveButton: SaveButton

{
    protected override bool SaveItem()

    {
        bool success = base.SaveItem();

        RedirectUrl = String.Concat(List.ParentWeb.ServerRelativeUrl, "/",
                      List.Forms[PAGETYPE.PAGE_DISPLAYFORM].Url, @"?ID=", 
                      ListItem.ID, @"&Source=", ListItem.ParentList.DefaultViewUrl);
        return success;

    }
}

Now I just don't know how to register this template inside my other template. Could I not just override the SaveButton in the Code behind of my template - how would I do that and reference it later on?

  • Option one: Code-Behind of the form (RealForm.ascx.cs) - can I just put the override method in there? How can I reference the button then in the form (how do I get <NewSaveButton>)?
  • Option two: Another template just for the button, e.g. SaveButton.ascx" - how do I reference that via <%@ Register... %>, i.e. how do I know PublicKeyToken etc. when deployed via a Feature. And same thing here: My goal is to get some kind of "<NewSaveButton>" control for the form.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

執念 2024-10-28 02:35:07

执行此操作时,您将创建一个新的服务器控件,因此您需要在页面上(或者在本例中,在模板 .ascx 文件中)注册新控件。

<%@ Register TagPrefix="MyPrefix" Namespace="ControlNamespace" Assembly="MyFullyQualifiedAssembly" %>

在您的代码文件中,您可以将 ToolboxDataAttribute 添加到类中(仅当您从 Visual Studio 中的工具箱中拖放控件时才需要这样做)

[ToolboxData("<{0}:NewSaveButton runat=\"server\"></{0}:NewSaveButton>")]
public class NewSaveButton : SaveButton {}

现在,您应该能够替换保存按钮,其中包含以下内容:

<MyPrefix:NewSaveButton runat="server"></MyPrefix:NewSaveButton>

您基本上是按照 ASP.NET 的规则创建一个新的服务器控件(此处没有发生特定于共享点的内容)。

有关详细信息,请查看此页面:http ://msdn.microsoft.com/en-us/library/yhzc935f(v=VS.85).aspx

You're creating a new server control when you do this, so you'll need to register the new control on the page (or in this case, in the template .ascx file).

<%@ Register TagPrefix="MyPrefix" Namespace="ControlNamespace" Assembly="MyFullyQualifiedAssembly" %>

In your code file you can to add the ToolboxDataAttribute to the class (this is only necessary if you are dragging&dropping the control from the toolbox in visual studio)

[ToolboxData("<{0}:NewSaveButton runat=\"server\"></{0}:NewSaveButton>")]
public class NewSaveButton : SaveButton {}

Now, you should be able to replace the save button on the form with the following:

<MyPrefix:NewSaveButton runat="server"></MyPrefix:NewSaveButton>

You're basically creating a new server control following the rules of asp.net (no sharepoint specific stuff is happening here).

For more information, take a look at this page: http://msdn.microsoft.com/en-us/library/yhzc935f(v=VS.85).aspx

江湖正好 2024-10-28 02:35:07

在带有 SaveButton 的页面上,您可以执行以下技巧(在我的情况下,保存按钮添加在 DataFormWebPart 的 XSL 标记中):

// On your page with SaveButton you could do the following trick 
// (in my case save button is added in DataFormWebPart's XSL markup):

SPContext itemContext;
DataFormWebPart dataForm; // from designer's code behind

void Page_Init(object sender, EventArgs e)
{
    // NOTE: by some reason ItemContexts of controls in DFWP are differ,
    // so only SaveButton's OnSaveHandler is invoked
    itemContext = dataForm.Controls.FindControlRecursive<SaveButton>().ItemContext;
}

void Page_Load(object sender, EventArgs e)
{
    if (itemContext.FormContext.FormMode == SPControlMode.New ||
        itemContext.FormContext.FormMode == SPControlMode.Edit)
    {
        itemContext.FormContext.OnSaveHandler += OnSaveHandler;
    }
}

void OnSaveHandler(object sender, EventArgs eventArgs)
{
    // TODO: Add your code before saving the item
    SaveButton.SaveItem(saveButton.ItemContext, false, string.Empty);
    // TODO: Add your code after saving the item
}

FindControlRecursive() 扩展实现是

public static class ControlExtensions
{
    public static TControl FindControlRecursive<TControl>
    (
        this ControlCollection controls
    ) where TControl : Control
    {
        if (controls != null)
        {
            foreach (Control control in controls)
            {
                var foundControl = control as TControl 
                    ?? control.Controls.FindControlRecursive();
                if (foundControl != null)
                {
                    return foundControl;
                }
            }
        }
        return null;
    }
}

On your page with SaveButton you could do the following trick (in my case save button is added in DataFormWebPart's XSL markup):

// On your page with SaveButton you could do the following trick 
// (in my case save button is added in DataFormWebPart's XSL markup):

SPContext itemContext;
DataFormWebPart dataForm; // from designer's code behind

void Page_Init(object sender, EventArgs e)
{
    // NOTE: by some reason ItemContexts of controls in DFWP are differ,
    // so only SaveButton's OnSaveHandler is invoked
    itemContext = dataForm.Controls.FindControlRecursive<SaveButton>().ItemContext;
}

void Page_Load(object sender, EventArgs e)
{
    if (itemContext.FormContext.FormMode == SPControlMode.New ||
        itemContext.FormContext.FormMode == SPControlMode.Edit)
    {
        itemContext.FormContext.OnSaveHandler += OnSaveHandler;
    }
}

void OnSaveHandler(object sender, EventArgs eventArgs)
{
    // TODO: Add your code before saving the item
    SaveButton.SaveItem(saveButton.ItemContext, false, string.Empty);
    // TODO: Add your code after saving the item
}

The FindControlRecursive() extension implementation is

public static class ControlExtensions
{
    public static TControl FindControlRecursive<TControl>
    (
        this ControlCollection controls
    ) where TControl : Control
    {
        if (controls != null)
        {
            foreach (Control control in controls)
            {
                var foundControl = control as TControl 
                    ?? control.Controls.FindControlRecursive();
                if (foundControl != null)
                {
                    return foundControl;
                }
            }
        }
        return null;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文