Page_Load twice

This forum is only for questions or discussions about working with the mojoPortal source code in Visual Studio, obtaining the source code from the repository, developing custom features, etc. If your question is not along these lines this is not the right forum. Please try to post your question in the appropriate forum.

Please do not post questions about design, CSS, or skinning here. Use the Help With Skins Forum for those questions.

This forum is for discussing mojoPortal development

This forum is only for questions or discussions about working with the mojoPortal source code in Visual Studio, obtaining the source code from the repository, developing custom features, etc. If your question is not along these lines this is not the right forum. Please try to post your question in the appropriate forum.

You can monitor commits to the repository from this page. We also recommend developers to subscribe to email notifications in the developer forum as occasionally important things are announced.

Before posting questions here you might want to review the developer documentation.

Do not post questions about design, CSS, or skinning here. Use the Help With Skins Forum for those questions.
This thread is closed to new posts. You must sign in to post in the forums.
4/29/2010 11:17:00 PM
Gravatar
Total Posts 190

Page_Load twice

Hi All,

I tried adding this code:

if (Request.Browser.IsMobileDevice)
{
  if (Request.Cookies["mobileUser"] == null)
  {
    HttpCookie appCookie = new HttpCookie("mobileUser");
    appCookie.Value = "true";
    appCookie.Path = "/";
    Response.Cookies.Add(appCookie);
    Response.Redirect("/Mobile/");
  }
}
 

to the default.aspx.cs file at the top of the Page_Load method. The idea is that mobile users would, at first, be directed to the mobile site and then be able to link back to the main site if their device can handle it. What I'm finding is that Page_Load is getting executed twice. You can actually see the mobile page for a second but the the code executes a second time, finds the cookie, and goes right back to the main home page. I even tried taking out the cookie setter and putting it in the home page of the mobile site, but still got the same effect. I've tried tweaking numerous things but nothing seems to be working. Any help?

Thanks,

John

4/30/2010 6:52:19 AM
Gravatar
Total Posts 18439

Re: Page_Load twice

Hi John,

I tried to replicate this on my machine debugging under IIS 7 but I could not.

  1. Created a folder mobile in the mojoPortal.Web project
  2. added a Default.aspx page to the fodler from VS 2010 with text hello mobile
  3. added your code to the top of page load in Default.aspx.cs in the root of the web
  4. changed the logic to !Request.Browser.IsMobileDevice since I'm debugging on a desktop not a mobile device
  5. set a break point on the first line of your code and launched the debugger

The break point was hit only 1 time indicating the event only fired 1 time, it set the cookie and redirected as expected and I saw the hello mobile message. 

As you know, I don't recommend putting custom code into existing mojoPortal code. Something like this could be done using a custom httpmodule and some logic to inspect the url along with the cookie logic to determine if it should redirect or not.

Best,

Joe

4/30/2010 10:17:04 AM
Gravatar
Total Posts 190

Re: Page_Load twice

I probably could've explained a bit more. I'm not really thinking this is specifically a mojoPortal issue. I was also having this issue with a custom module I created using the WYSIWYG editor. If I use the normal asp:Button that generates an input type form element, it works fine. If I use a <button> element on the form, it causes the page to load twice and I was getting two records inserted when adding something new. I did some testing with the button vs input in a project completely outside mojoPortal and was getting the same results. I just haven't been able to figure it out. I keep getting this occassional double page load method execution in random situations on my machine and it also seems to carry over to test and production web servers. And it also doesn't seem to do this on every computer. I checked around with a few other people and some see it and some don't. It doesn't seem to be browser specific but nothing I've tried seems to make a difference and I was just really hoping somebody here might have run into this issue and had a suggestion.

4/30/2010 12:25:36 PM
Gravatar
Total Posts 18439

Re: Page_Load twice

Hi John,

Is it possible that you have AutoEventWireup=true in the .aspx or .ascx and then are also wiring up the events in code behind? If the event gets wired up more than once it will fire more than once.

You might also try different browsers as I once encountered an issue where postback happened twice under Firefox if I had a button inside an update panel and was using logic to disable the button after clicking. So it was case where I was disabling the button to prevent double postback but in this case it was causing double postback.

Best,

Joe

4/30/2010 12:52:55 PM
Gravatar
Total Posts 190

Re: Page_Load twice

I looked at the default.aspx page and noticed that the autoeventwireup is false. In my layout.master of my skin it is set to true. I tried setting that one false and that broke everything. I do have wireup set to true on a module that I created but I'm not explicitly doing any event binding in the code for those pages. Would the wireup on ascx files included in the page cause the default page_load to run twice? Is it then in general necessary that any modules you create for mojoPortal should have AutoEventWireup set to false? (I don't think that would be a problem, but certainly good to know)

4/30/2010 1:01:54 PM
Gravatar
Total Posts 18439

Re: Page_Load twice

Hi John,

No, each of these things has their own page load events, wiring up events in a master page or user control should not affect events at the page level, each has its own scope. 

Best,

Joe

4/30/2010 3:02:07 PM
Gravatar
Total Posts 190

Re: Page_Load twice

Well, that's a relief. That's initially how I understood it. I thought maybe my whole grasp of ASP.NET was about to go out the window. Oh well. I was hoping someone would have recognized my dilemna and just offered an, "oh, just go check that box over there." That seldom seems to be my luck. I'm just stumped as to why I keep running into these doulbe page loads. Maybe it's time to call MS. Or maybe it's time to stick a firecracker in my PC and get a new one.

Thanks again.

8/18/2011 6:04:23 PM
Gravatar
Total Posts 14

Re: Page_Load twice

Hello,

For the people who are facing this issue

I faced this problem today, I resolved by removing the event binding line from OnInit() method  for the user control as this.

 

protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            //this.Load += new EventHandler(Page_Load);

 

        }

 

I don't know why this is happening, but I noticed that the use control is used in the layout.master as a regular user control not as Mojo feature. this could be the reason.

I am using Dot Net Framework 4

 

Hope this helps

Thanks

Tarek

 

 

You must sign in to post in the forums. This thread is closed to new posts.