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.
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.