First let me clear the air and post articles which already explain how to override the SaveButton:
- How to override or customize the Sharepoint SaveButton?
- How to override functionality of the Ribbon Save button
- Override SharePoint Save Button to Provide Custom Functionality
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=&开发者_运维技巧quot;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 knowPublicKeyToken
etc. when deployed via a Feature. And same thing here: My goal is to get some kind of "<NewSaveButton>
" control for the form.
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
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;
}
}
精彩评论