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 !