0

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 */
        }
    }
}
0

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

  • Just tried this and it seems that on both occasions it is not a post back, therefore the code runs twice. – Submits May 6 '15 at 16:12
  • Any other suggestions? – Submits May 7 '15 at 9:06
  • did you check the links . there are other solution that users have tried . Please go through the links and see if you find something that works – Manish Oswal May 7 '15 at 9:54
  • Yes I tried some of the suggestions, without success. For example, setting AutoEventWireup to FALSE stops the code behind from running all together. – Submits May 7 '15 at 9:56
  • Try setting AutoEventWireup to true in the @Page directive, then removing Handles MyBase.Page_Load, Me.Load from the Page_Load, in all the solutions files. see the comment from the post in link 2 I did not set AutoEventWireup attribute to true, but I removed ",Me.Load" and that sovled my problem. Now I am investigating how and when ",Me.Load" was inserted into my code. – Manish Oswal May 7 '15 at 10:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.