3

Here is a problem that has been driving me nuts for a couple of months. It seems to be very intricate, so I made a very simple example to demonstrate it.

I have a website using ASP .Net and hosted on a microsoft server, using IIS and a SQL Server database. The website is working fine when debugged on a local server, or when accessed from inside the server.

BUT, whenever I'm using the website from client-side (= accessing it from my computer when it is deployed on my remote server), the pages of the website seems to be loading twice every time. Meaning :

  • At first loading, the Page_Load event is fired twice.
  • Every time I'm triggering a runat="server" control, the Page_Load event AND the associated server event are triggering twice.

So, I made a little webpage, hosted on the same website but not related to any of its other pages (not using the Site.Master either). Here it is :

Client-side code :

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestPostback.aspx.cs" Inherits="Rosalie.TestPostback" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button runat="server" ID="btnTest" OnClick="btnTest_Click" Text="Test" />
    </div>
    </form>
</body>
</html>

Server-side code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace Rosalie
{
    public partial class TestPostback : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Directory.Exists(Server.MapPath("Logs")))
                Directory.CreateDirectory(Server.MapPath("Logs"));

            StreamWriter sw = new StreamWriter(Server.MapPath("Logs/Trace.txt"), true);
            sw.WriteLine(DateTime.Now + " " + getPostBackControlName() + " PageLoad\n");
            sw.Flush();
            sw.Close();
        }

        protected void btnTest_Click(object sender, EventArgs e)
        {
            if (!Directory.Exists(Server.MapPath("Logs")))
                Directory.CreateDirectory(Server.MapPath("Logs"));

            StreamWriter sw = new StreamWriter(Server.MapPath("Logs/Trace.txt"), true);
            sw.WriteLine(DateTime.Now + " " + getPostBackControlName() + " ButtonEvent\n");
            sw.Flush();
            sw.Close();
        }

        // Method returning the ID of the control triggering the postback
        private string getPostBackControlName()
        {
            ...
        }
}

As you can see, the goal is to write in a file everytime a server event method is called and so have a trace of the server behaviour.

Now, here is the result file when I run this page on my local server, or when I access it from inside the release server, and I do one single click on the test button :

02/10/2018 15:05:44  PageLoad
02/10/2018 15:05:46 btnTest PageLoad
02/10/2018 15:05:46 btnTest ButtonEvent

You can see that the page was loaded, then a postback was triggered by my click on the button and triggered the associated event. This is a perfectly fine and normal behaviour for this webpage.

Now, here is the result file when executing the exact same steps but from a remote client computer :

02/10/2018 15:36:19  PageLoad
02/10/2018 15:36:20  PageLoad
02/10/2018 15:36:28 btnTest PageLoad
02/10/2018 15:36:28 btnTest ButtonEvent
02/10/2018 15:36:28 btnTest PageLoad
02/10/2018 15:36:28 btnTest ButtonEvent

As you can see, the Page_Load and btnTest_Clicked method are fired twice in a row, as if the webpage was loaded twice every time the server was called.

Given the simplicity of the code, I really think this has something to do with the website configuration or the server environment, but I'm not really an expert in such things.

I searched a solution on the Internet multiple times, spending hours trying out stuff related to pure programmation, but nothing seems to work out. I'm desperate for solutions. Thank you !

  • https://stackoverflow.com/questions/4785825/page-load-is-firing-twice-in-asp-net-page – Hary Oct 02 '18 at 15:16
  • @mbharanidharan88 Already came across this post, and found no satisfying answer. Plus, it doesn't talk about control events but only page_load (and code is in VB, not C#). – Clément S. Oct 02 '18 at 15:21
  • Because Postbacks may be due to several reasons and some reasons are mentioned there. You can give a try... – Hary Oct 02 '18 at 15:22
  • Sadly I already did. And it does not explain the difference of behaviour inside or outside of the server, which is the major problem I have. Thanks anyway – Clément S. Oct 02 '18 at 15:24
  • May be with Chrome Developer Tools you can fiddle the request and analyse – Hary Oct 02 '18 at 15:25
  • Using Chrome Dev Tools I only see one POST request sent to the server on a click, with a 200 Status code in response. Everything seems to be alright on this side – Clément S. Oct 02 '18 at 15:32
  • Did you checked the network tab with filter `All` ? – Hary Oct 02 '18 at 15:33
  • You rolled your own logging. Don't reinvent the wheel! We have excellent libraries such as [Serilog](https://serilog.net/) and [NLog](https://nlog-project.org/) for that. – mason Oct 02 '18 at 15:36
  • Yes, here is the result : https://image.ibb.co/gz5T3e/Sans_titre.png – Clément S. Oct 02 '18 at 15:37

0 Answers0