tags:

views:

152

answers:

2

Has anyone ran into an issue where the Page_Load method is called twice? I have a button that has an onclick event.

+2  A: 

Have you tried the items lined out in this Stack Overflow question?

YUI Button Initiates Postback Twice?

We'll need to see your code to help you.

Things to check for:

  • AutoEventWireUp set to true (If you have a Page_Load method, set it to false)
  • Is the button with the onclick event an ASP.NET Button or a JavaScript Button? If it's a ASP.NET Button, try submit='false' as a button attribute. If it's a button that initiates javascript onclick, then try return false(); after the rest of the JavaScript in the onclick event.

Questions I have:

  • What is the purpose of the button?
  • What is the complete code surrounding it?
  • What are the page directives set to?
  • Are you using any third-party JavaScript?
  • Is AutoEventWireUp=true

I'm really just throwing darts at the wall on this one without your code, so this answer could change/be edited based on the code you add to your question.

George Stocker
The purpose of the button is to submit data to a DataSet. The page consists of a bunch of dynamically created controls that are added to a page. I do have the AutoEventWriteUp set to true. The application was a 1.1 app and I had upgraded it to 3.5.
Brandon Michael Hunter
Ok, great. Can you post your page_load code as well as your ASPX code surrounding this button? We can't really help you. I recommend setting '`autoeventwireup`' to false if you have an explicit `Page_Load` method with a `this.Load +=`
George Stocker
A: 

If your site has authentication and the double-page_load is happening on your login page, it's possible that your login.aspx page is using resources outside out the anonymous user's rights. For example if your web.config includes:

<authorization>
  <deny users="?"/>
</authorization>

and your login.aspx page uses a .css file in your /style folder, you need to add:

<configuration>
    <!-- Other configuration elements... -->
    <location path="style">
        <system.web>
            <authorization>
                <allow users="*"/>
            </authorization>
        </system.web>
    </location>
</configuration>

A good way to test if this is your problem is to use the "Net" tab inside of Firebug to observe the requests. You could also comment out any image and link elements that your login page references.

This behavior may also happen on non-login.aspx pages if the authenticated user isn't given rights to the referenced area of your website, but I haven't tested that.

CodeChef