Why is a .aspx page loading twice when I click a button on the page? [closed]
When clicking on a button in a .aspx page, the page loads twice. In the first load, the page's IsPostBack property is true and on the second load, this property will become false, which is a problem for my website.
Does anyone have any ideas why it is loading twice?
Following are the possible means of postback twice:
1- please check that you are not explicitly making postback from client-side.
2- Please check that in your page img tag src attribute is not empty. Because when you create an img element and leave its src attribute empty, it will automatically set as your root directory (e.g. “http://www.mysite.com/”). Therefore, when the Page_Load event fire for the first time, with the original post back (POST request) the Page.IsPostBack will be set with “true”. But when the server response will be parsed at the client side, another GET request will be fired to the server, requesting that image (that its src was set to the root url by default) and this is why the Page.IsPostBack property will be initialized with “false” value for the second time.
For your button to work, it must use a PostBack to notify the server that an event has occurred.
The way the ASP.Net lifecycle works, the page must be loaded before a post back event (like clicking a button) is handled, so that it knows how to handle that event. So when you click a button, the page calls the following events
PreInit -> Init -> InitComplete -> PreLoad -> Load -> Your event handler
If your button causes a redirect, at this point, the page will be redirected and restart the page life cycle.
Without knowing exactly what your button is doing, it's hard to say if you can avoid the second page load. If you want to share your event code, I can update this answer
精彩评论