so is it a postback or not - asp.net

I'm causing a postback by clicking an asp:button. I end up in a breakpoint on Page_Load, where IsPostBack == true. Then, one breakpoint later, I end up in Page_Load where IsPostBack == false.
What the heck is going on? I'm trying to understand someone else's code.

The first link click is a PostBack from the server and the correct behavior. It sounds like the second PostBack is triggered because of a Page.Redirect() or a Server.Transfer() back to the same page.
This is a best guess until you provide additional details.

Here is a blog post that may be helpful.
http://www.craigwardman.com/blog/2009/01/aspnet-multiple-page-load-problem.html
I was having the same issue because the src of one of my images was "" and that caused the page load to fire 2 times.

This sounds like 2 it could be 2 separate requests to the page - Check the IIS logs.
If not then its possibly a Server.Transfer(), or someone might be instantiating and calling the Page_Load method themselves for some reason (extremely unlikely, but easy to check by looking at the callstack)

Related

ASP.NET AJAX weirdness

Ok, I thought I understood these topics well, but I guess not, so hopefully someone here can clear this up.
Page.IsAsync seems to be broken. It always returns false.
But ScriptManager.IsInAsyncPostBack seems to work, sort of.
It returns true during the round trip for controls inside UpdatePanels. This is good; I can tell if it's a partial postback or a regular one.
ScriptManager.IsInAsyncPostBack returns false however for async Page Methods. Why is this? It's not a regular postback, I'm just calling a public static method on the page.
It causes a problem because I also realized that if you have a control with AutoPostBack = false, it won't trigger a postback on it's own, but if it has an event handler on the page, that event handler code WILL run on the next postback, regardless of how the postback occurred, IF the value has changed.
i.e. if I tweak a dropdown and then hit a button, that dropdown's handler code will fire. This is ok, except that it will also happen during Page Method calls, and I have no way to know the difference.
Any thoughts?
As Tjaart points out, Page.IsAsync has nothing to do with AJAX! See MSDN for a bit more info about IsAsync and see http://msdn.microsoft.com/en-us/magazine/cc163725.aspx for a fuller description of async pages].
Page methods are web services by a different name. The ScriptManager will emit the necessary JS boiler plate to make creating an XHR that invokes the web service very easy but that's all ScriptManager has to do with them really.
As the MSDN page states, ScriptManager.IsInAsyncPostBack will only be true if the request is in "partial rendering mode" so ScriptManager.IsInAsyncPostBack will be false when you are executing a page method because that request has not been spawned as a result of a partial postback (i.e. an UpdatePanel refreshing its contents).
Now it sounds like you are getting server side event handlers being executed as an apparent result of calling a page method from JS. AFAIAA, invoking a page method using javascript should not cause the page to go through its normal page lifecycle - so Page load, init etc. and these events should not be executing. So that is strange.
Suggestion: -
See Anz's comments and Dave's replies here encosia.
Could it be that you are having similar problems to Anz? i.e. The page method is invoked and but then your page is posting back immediatly after?
This is so because ASP.NET Ajax and ASP.NET Callbacks are two different things and are implemented differently. Unfortunately you have to use both Page.IsAsync and ScriptManager.IsInAsyncPostBack.
Page.IsASync probably returns whether the page was set as Async in the page directive
<%# Page Language="vb" Async="true" ...
The autopostback flag is so that you don't get a postback after every single control action, so the user can fill in an entire form and then only create the postback and trigger all the related code.
It's not really weirdness, they designed it this way so that the server side code will always be synchronized with the client side. So if you make a drop down list selection on the page and a postback occurs then that drop down list change executes it's own code along with the control that triggered the postback. You may want to read up more on the ASP .Net page lifecycle. it made things much more clear for me.

Page_PreRender fires twice on first load in session

When I run the web application, I notice that Page_PreRender is fired twice. This only happens the first time in a new session. It does not happen if I refresh the page, or on postbacks.
I use .NET framework 3.5 and the built in ajax functionality.
The problem is not
related to img tag with empty src
attribute (which I have seen other posts with similar problem
has mentioned). I know this because I see this in
both FireFox and IE. The posts I saw
about this stated that this was not a
problem in IE. I have also searched
and found no img tags with empty src
in the generated page source, so it
should not be this.
I have also made a simple test
page where I have included some of
the functionality, and this does not
happen.
Have anyone any suggestions on what happens?
Note:
It is the entire page cycle that is firing twice, not just render.
I've experienced it; it's probably not what you are experiencing, but I'll enter it here anyway; I've noticed it when the application does a Response.Redirect at the PreRender level, which a redirect does not stop current execution, but makes it appear the event happens twice...
Again, probably not related, but including it just in case.
I copied some code that states:
Page.LoadComplete += new EventHandler(Page_PreRender)
I did not realize that this code fire for second time my Page_PreRender event.
When I comment it, never fire twice.

Page_Load not being called on re-navigation

I am having a strange problem. Here is the scenario
Here are my files:
Project1.aspx
Project2.aspx
They are set up the exact same, including their Page_Load functions:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If (Not Page.IsPostBack) Then
setPrevIndex(-1)
...
End If
End Sub
They are both set up this way.
Here is where I am running into a problem. When I navigate to either of these pages, i need to make sure that prevIndex is set to -1 (via the function).
For Project1.aspx when I navigate
to the page, the Page_Load fires.
For Project1.aspx when I refresh the page, the Page_Load fires.
For Project1.aspx when I press "Go" in my browsers navigation bar, traveling back to the current page, the Page_Load fires.
For Project2.aspx when I navigate
to the page, the Page_Load fires.
For Project2.aspx when I refresh
the page, the Page_Load fires.
For Project2.aspx when I press
"Go" in my browsers navigation bar,
traveling back to the current page,
the Page_Load doesn't fire at all! The function isn't even
called.
Any ideas why??? What would cause this?
Please ask for clarification.
Update:
When I press "Go" in the URL pointing to the same URL, it seems like the masterpage is the only thing that re-loads, but the Load_Page event doesn't even fire...
Any other suggestions?
Thanks,
E
Try disabling output cache and see if the problem still occurs:
<system.web>
<caching>
<outputCache enableOutputCache="false"/>
</caching>
<system.web>
Use LiveHTTPHeaders or Fiddler to make sure the page is actually being requested the same way each time. This may be an issue with caching.
Load up your website locally and go to http://yourwebsite/trace.axd
This shows a server trace for each page, along with the server status. It also shows the complete page lifecycle with timings.
Clear the current trace and then repeat your 3 visits and reloads each to Project1.aspx and Project2.aspx
What does trace.axd show now? You should have 6 entries, each with the status code of 200 and the verb GET.
If you have fewer then your problem is caching of some sort.
If you have 6 then check the details for the last one - what does is show for the page event lifecycle? It will also show the complete WebForm control hierarchy, so if this related to the master page you will be able to tell.
Try Setting the previndex to -1 in Page Init Event. I am not sure why this is happening though.
Various cache-related things can cause the request not to be made, in particular when you merely press the "go" button, so you should check your cache-headers.
If caching is the issue, you can do something like:
//ask browser to revalidate:
context.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
//and hint that the page is outdated anyhow...
context.Response.Cache.SetMaxAge(TimeSpan.Zero);
Which should convince a browser to really get a new version every pageview. You could, for instance, set these variables in the Page_Load itself ;-). If you're not using https, then the following are risk-free too:
//prevents plugin based file-open in IE+https, otherwise fine:
context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
context.Response.Cache.SetNoStore();
I'll bet this solves it - and if not, I second Jeremy Steins suggestion that you use fiddler to verify that the request is really being made at all (and since you're a web-dev, get fiddler in any case, it's a handy tool to have available, and works for all browsers!).
Finally - can you tell whether any other code on the page runs when you click go? (i.e. is the entire page not running, or just Page_Load - the latter would suggest an event wire up error, which would be odd considering your load handler does sometimes fire).
It sounds like your page is cached. This would cause the Page_Load not to fire. Check that you have not set that anywhere.
Have you tried publishing the application to a different machine? It could be IIS doing something so try and eliminate that first. Assuming that your code is identical in both na only the page names differ (do a diff on the aspx and .cs files to verify) then move your application to a different server and re-test.
If it is still occurring the it really must be your browser doing something probably with respect to caching.
Try to recreate the scenario with stripped down functions on your server. If the problem persists, try to use some cache-countering methods. If not, that means it has to be your code.
If you have this (or similar) at the top of your page (or master page) this will cause it:
<%# OutputCache Duration="3600" VaryByParam="none" %>

ASP.Net button click event not firing

This is perhaps related to this question, but I have slightly more information.
I recently updated an ASP.Net application to .NET 3.5 after coding a few new pieces with Linq. Now my pages intermittently stop firing event handlers. I have it narrowed down to pages with Ajax on them, and I assume it's either the ScriptManager or the AjaxControlToolkit registration that is precipitating the problem.
The really screwy part is that pages seem to work fine about 2/3s of the time, and the controls lose their event handlers only 1/3 of the time. I wondered about some type of error occurring and the page not rendering correctly, but I cannot find evidence of this.
We were have exactly this issue and as you have said it was only happening some of the time and only on pages with Ajax requests. We found that disabling the HTTP Keep-Alive as suggested in the answer to
Why does Internet Explorer not send HTTP post body on Ajax call after failure? worked.
This can be done in IIS7 by following the instructions in How to configure HTTP KEEP-ALIVE in IIS 7
i have only used Telerik Ajax controls so far, and my observation is if an exception occours in a Ajax enabled control, nothing happens. No exception report, no action. nothing.
Maybe you should test you app without ajax to identify any problem spots
My initial thought would be that something is happening where the javascript is erroring out (un-caught) and aborting the async postback.
If you can get this to happen in a development environment, you might want to try error handling the window.onerror event and throwing some alert boxes up. It might help to see where possible errors occur.
have you tried:
ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(yourButton);
It's seem to me, you have issue with Update Panel.
Define the following properties of Update Panel if you are using more than one update panel and updation of one panel change the data of other update panel.
For the UpdatePanel
ChildrenAsTriggers = true
EnableViewState = true (if you are using this feature)
UpdateMode = Always
Use the same properties for other update panel in your same page.
If you find it useful, please mark it as your answer else let me know....
Try commenting out Update panel if any. Then try to click button there must be some error on the page. I've also faced similar issues.
In my case none of the events (either button or check box) were firing. I was able to overcome by this problem using following way.
In my master page I had closed a tag referring to an external .js file with /> notation instead of <script></script>.
for Ex: chanage <script src='../../Scripts/common/menu.Js' type='text/javascript' /> to <script src='../../Scripts/common/menu.Js' type='text/javascript' ></script>
Hope this will help you,
Ramesh

Double postback issue

I have a ASP.NET 1.1 application, and I'm trying to find out why when I change a ComboBox which value is used to fill another one (parent-child relation), two postbacks are produced.
I have checked and checked the code, and I can't find the cause.
Here are both call stacks which end in a page_load
First postback (generated by teh ComboBox's autopostback)
Postback call stack (broken)
Second postback (this is what I want to find why it's happening)
alt text (broken)
Any suggestion? What can I check?
It's a very specific problem with this code, I doubt it will be useful for someone else, but here it goes:
A check was added to the combo's onchange with an if, if the condition was met, an explicit call to the postback function was made.
If the combo was set to AutoPostback, asp.net added the postback call again, producing the two postbacks...
The generated html was like this:
[select onchange="javascript: if (CustomFunction()){__doPostBack('name','')}; __doPostBack('name','')"]
First thing I would look for is that you don't have the second ComboBox's AutoPostBack property set to true. If you change the value in the second combo with that property set true, I believe it will generate a postback on that control.
Do you have any code you could share? Double post backs plagued me so much in classic ASP back in the day that it was what finally prompted me to switch to .NET once and for all. Whenever I have problems like these for .NET, I go to every CONTROL and every PAGE element like load, init, prerender, click, SelectedIndexChanged, and the like and put a breakpoint.
Even if I don't have code there, I'll insert something like:
Dim i As Integer
i = 0
I am usually able to pinpoint some action that I wasn't expecting and fix as needed. I would suggest you do that here.
Good luck.
Check Request.Form["__EVENTTARGET"] to find the control initiating the postback - that may help you narrow it down.
Looking at the callstacks, and some Reflectoring (into ASP.NET 2 - I don't have 1.1 handy) - it looks like SessionStateModule.PollLockedSessionCallback is part of the HttpApplication startup routines. It may be possible that your app is being recycled - I'm pretty sure an event gets written into the Event log for that.
My only other suggestion would be Fiddler or something on the client to capture HTTP traffic.
This is very old post, but people still looking at this for solution exactly same as I did last week.
Like Grengby said Double events are primary reasons - but removing one of them is not allways an option. Atleast on my case and I had to resolve this on 3rd party's application.
I added following script and amended ASP form on masterpage:
<script>var Q = 0;</script>
<form id="Form1" runat="server" onsubmit="Q++; if(Q==1){return true;} else { return false;}">
This seems to be working and please forward your comments.
Arun
http://www.velocityreviews.com/forums/t117900-asp-net-multiple-postback-issue.html

Resources