Back to Insights

Customizing the control behaviour on a SharePoint list item form

SharePoint has great flexibility to customize list item forms which are quite complex pieces. List item forms 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 the user opens a "dispform.aspx".

I also 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 for the custom form I created I referenced a custom control that looked something like this:

<SharePoint:GoBackButton runat="server"/>

What you see above is the definition of a cancel button within the rendering template. I changed the control definition 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; 
            } 
          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 its 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!

Share