Question

I have a custom edit control (ECB) block which points to a custom ASPX page that has some code-behind. Developed as part of a larger Visual Studio solution.

For some reason the code behind for the page is running twice. This is causing problems with what the page should be displaying.

Why would the code behind in Page_Load method be running/firing twice?

Is this some how related to the issue others have previously faced with event receivers firing twice?

The simple page markup:

<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Import Namespace="Microsoft.SharePoint.ApplicationPages" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CancelLeaveRequest2.aspx.cs" Inherits="spLeaveBooking.Layouts.spLeaveBooking.CancelLeaveRequest2" DynamicMasterPageFile="~masterurl/default.master" %>

<asp:Content ID="PageHead" ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">

</asp:Content>

<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
    <asp:Panel ID="Panel1" runat="server" Visible="false">
        You cannot cancel this leave request either because it was rejected, a cancellation request is already pending, you created it a few seconds ago or you are not the person who originally created this leave request.
        <br /><br />
        If you created another leave request a few seconds ago, wait a couple of minutes and try again. If the problem persists, contact your SharePoint administrator.
    </asp:Panel>
</asp:Content>

<asp:Content ID="PageTitle" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">

</asp:Content>

<asp:Content ID="PageTitleInTitleArea" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server" >

</asp:Content>

The code behind:

using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Collections.Specialized;
using Microsoft.SharePoint.Utilities;

namespace spLeaveBooking.Layouts.spLeaveBooking
{
    public partial class CancelLeaveRequest2 : LayoutsPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                using (SPWeb web = SPContext.Current.Web)
                {
                    // Get query string parameters
                    string listGuid = Request.QueryString["ListId"];
                    string listItemId = Request.QueryString["ItemId"];

                    // Get the relevant list and list item
                    SPList list = web.Lists.GetList(new Guid(listGuid), false);
                    SPListItem item = list.GetItemById(int.Parse(listItemId));
                    bool error = false;

                    /* Some code */

                    if (error)
                        Panel1.Visible = true;
                    else
                        // Redirect back to the list
                        Response.Redirect(web.Url + "/Lists/" + list.Title);
                }
            }
        }

        private bool DoUpdate(SPWeb web, SPListItem item, string status)
        {
            /* Some code */
        }

        private void SendEmail(SPWeb web, SPListItem item)
        {
            /* Some code */
        }
    }
}

No correct solution

OTHER TIPS

You can avoid pageload function working on each postbacks by !IsPostBack

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //Your code here
        }
    }

A User control with this master page can also cause pageload execute twice...

check the links for more info Link 1 Link 2 Link 3

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top