开发者

Why is a .aspx page loading twice when I click a button on the page? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current f开发者_JS百科orm. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜