fix
Out of the box SharePoint comes with default list item forms and few articles ago I was going over how to customize them to include your content and logic.
As you will learn or already have learned from the article I was referring to – list item forms are quite complex pieces and have quite a collection of controls and templates that define how the form is rendered. One of the things I found a need for recently is the ability to modify the behavior of a cancel button when user open a dispform.aspx. Also I needed to add few other buttons of my own and make them call other pages that performed administrative functions on the item.
In my rendering template of the custom form I created – I referenced a custom control I created that looked something like this:
<SharePoint:GoBackButton runat=”server”/>
What you see above is the definition of a cancel button within rendering template. I changed the control defintion to this:
<MyProject:MyGoBackButton runat=”server”/>
I reference my DLL in the control and here is how my new control class was implemented:
public class MyGoBackButton : GoBackButton
{
protected override void CreateChildControls()
{
base.CreateChildControls();switch (ControlMode)
{
case SPControlMode.Display:
{
Button button = (Button)TemplateContainer.FindControl(“diidIOGoBack”);button.PostBackUrl = “http://” + HttpContext.Current.Request.Url.DnsSafeHost
+ “/_layouts/MyProject/MyCustomAction.aspx?Id=” + SPContext.Current.ItemId;string sourceUrl = string.Format(Constants.EditItemAbsoluteUrl,
HttpContext.Current.Request.Url.DnsSafeHost,
SPContext.Current.ItemId);button.OnClientClick = string.Empty;
button.PostBackUrl += “&Item=” + sourceUrl;}
break;
default:
Visible = false;
break;
}
}
Above I have defined my custom MyGoBackButton that inherits from SharePoint Cancel button . In this case, my custom button will have it’s special behaviour only on the DispForm and I define it by filtering the SPControlMode by Display. I then find the control by referencing its ID diidIOGoBack and overwrite it’s default redirect instead of going to the list to go to my custom URL. It’s important to remember to cancel default OnClientClick event, to prevent the javascript redirect to occur and overwrite your custom redirect.
Hope this helps with your list form customizations!