Cancel button of ButtonSection does not return to previous page (SharePoint)

In SharePoint you can leverage OOTB controls to create professional and OOTB-looking pages. For example, below is a custom setting page that I have deployed to Central Admin.

1 Setting page

This page uses the OOTB InputFormSection and ButtonSection controls, as well as a few others.

The ButtonSection control in particular renders the standard OK and Cancel buttons, and its markup is as below:

<wssuc:buttonsection
			runat="server"
			topbuttons="true"
			bottomspacing="5"
			showsectionline="false"
			showstandardcancelbutton="true">
			<template_buttons>
				<asp:Button runat="server" class="ms-ButtonHeightWidth" OnClick="btnOK_OnClick" Text="<%$Resources:wss,multipages_okbutton_text%>" id="btnOKTop" accesskey="<%$Resources:wss,okbutton_accesskey%>"/>
			</template_buttons>
		</wssuc:buttonsection>

As you can see, you simply specify ShowStandardCancelButton to include the Cancel button on the form.

So for my page, I deployed it and found that the Cancel button takes the user back to the home page of the current web, rather than the previous page that they came from, which is not desirable.

I used ILSpy to look at the source code of this control and found the following:

  • If the page has a PageToRedirectOnCancel property, and the property has a value, then the Cancel button will take the user back to that page.
  • If CloseOnCancel is specified in the query string, and the value is set to 1, then the cancel button will execute the JavaScript window.close();. This is useful when your form is being displayed as a dialog.
  • Otherwise, the button will take the user back to the home page of the web.

My page inherits from LayoutsPageBase, which has the PageToRedirectOnCancel property. I simply override this in my class and provide the URL of the page I am expecting the user to come from and everything works as expected.

The control uses reflection to get the value for the PageToRedirectOnCancel property. So if the base class of your page does not have this property, e.g. if you are inheriting from PublishingLayoutPage, then you should be able to just define this property on your page and it should work.

Advertisements

About Bernado

Based in Australia, I am a freelance SharePoint and Dynamics CRM developer. I love developing innovative solutions that address business and everyday problems. Feel free to contact me if you think I can help you with your SharePoint or CRM implementation.
This entry was posted in SharePoint, SharePoint 2010, SharePoint 2013. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s