__eventtarget set to ScriptManager id - asp.net

I have multiple UpdatePanels on a page, each filled by somewhat expensive controls. On async postbacks, all UpdatePanels are initialized, but only the updates UpdatePanel is sent to the client. Now I would like to initialize only the UpdatePanel that actually requires an update.
http://ryanfarley.com/blog/archive/2005/03/11/1886.aspx suggests decoding the __EVENTTARGET parameter to find the control that caused the post back. forums.asp.net/p/1385862/2947336.aspx suggests decoding the Request.Form value corresponding the the ScriptManager unique ID. Both seems to work fine. However, our production system (IIS 6, .NET 3.5) frequently reports requests where __EVENTTARGET is set to the unique ID of the ScriptManager (MyScriptManager). In these cases the ScriptManager parameter also decodes to MyScriptManager|MyScriptManager instead of UpdatePanelId|EventTargetId.
It has been observed for Firefox 3.0 and 3.5 as well as IE 6, 7 and 8. However, I was unable to reproduce it. Does anyone have a hint what causes our clients' browsers to post back these values?

Have you tried setting the updatepanel's updatemode to "conditional" ? I think this would solve your problem.
More information here: http://codeclimber.net.nz/archive/2007/05/24/updatemode-default-value-for-the-updatepanel-is-always.aspx

The ASP.NET AJAX history state causes this kind of requests. When a client uses our web application, we track the partial page updates using EnableHistory="true" on the ScriptManager. If the client clicks the back button in her browser, the ScriptManager initiates an asynchronous request, using itself as EventTarget.

Related

Browser refresh in asp .net 3.5

I am new to asp .net web forms and I am having trouble in efficiently handling the broswer refersh. I have used the below link to do the same.
http://geekswithblogs.net/Vipin/archive/2011/06/08/detecting-browser-refresh-from-code-behind-in-.net.aspx
However , the problem with the above approach is that the button click is throwing the below error:
Invalid postback or callback argument. Event validation is enabled using in configuration or <%# Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.
Any help would be appreciated.
I suggest you to use Post/Redirect/Get pattern
If a web user attempts to refresh the page this pattern avoid the HTTP POST request to be resubmitted.
Just force a redirect at the end of the page that receive the posted data.
Moreover rely on session like example does is never a good practice

ASP.net dont fire unload on normal postback

If I leave my current page on a asp.net Web application I want that all sessions get destroyed. For that I am using Session.Abandon() on Page_Unload Event. But if I do a Postback with a normal Button_Click I don't want to fire this event.
It would be awesome if you could help me.
Lingo
First you need to understand how the web works. When you access stackoverflow.com for example your are actualy seeing the past. The page you accessed it's already destroyed on the web server.
Based on that principle when you use Page_Unload or Session.Abandon if you actually close your browser you don't send any request for the web server so the web server didn't know (and don't care even) if you close your page.
For doing like
The page unload description act like this it's after being rendered it's had nothing with the close of the page.
"The Unload event is raised after the page has been fully rendered, sent to the client, and is ready to be discarded.
At this point, page properties such as Response and Request are unloaded and cleanup is performed.'
Reference (https://msdn.microsoft.com/en-us/library/ms178472.aspx)
You need to use javascript for doing this behaviour or try the new websocket that will keep the connection alive and the server could check if the client has lost the link with the server.

Detecting page refresh without viewstate

Is there a way to detect page refresh without using viewstate in asp.net webforms ? I was wondering if it was at all even possible since asp.net mvc doesn't use viewstate for example.
I am trying to avoid using viewstate alltogether to keep my page size small.
IsPostBack is not going to work because its a F5 or Pager refresh case not the post back from any event.
You can detect a page refresh on the server-side using IsPostBack. In fact, any code you put in Page_Load in your code-behind will run each time the page is refreshed. Or have I misunderstood your question?
Why can't you just use IsPostback in the code behind with ViewState disabled in the page? This way you can do whatever you need to in the code behind and not worry about ViewState clogging up your page size.
You can disable ViewState in the page by adding the enableViewState flag to the #Page directive.
The only other thing I can think of is to have a piece of javascript code which catches the key press of F5, but obviously this will only capture an F5 refresh and not a Refresh-button refresh.
This site shows how to capture key presses with Javascript:
http://www.go4expert.com/forums/showthread.php?t=2733
The http protocal is stateless and after a response is send to the one how requestet it removes all trace of him except his session. The way webforms work is that it serialize your page state and send it to the client and then back to the server when you make a postback(A form post request). So you can save information about your client in his session about the page he been on before and then check in the session if he request the same page again.

What is the difference between ICallBackEventHandler and HTTPHandler?

When we write our own custom HTTPHandlers aren't they behave the same way as ICallBackEventHanlder does? we use both to make ajax calls from our web page, isn't this correct? or my understanding wrong, I wont doubt if it is :(
Obviously HTTPHandlers are more broader concept since a web page (.aspx) etc are also http handlers.
A ICallBackEventHandler is for integration with a page -- a handler is for anything. A callback handler is useful when you want to do an ajax request from the client-side of a page, and from that handler you still want access to all of the controls on the page, their re-saturated state that comes from ViewState, etc. An http handler has no access to the page or its state. A callback handler can also push some state changes back to the client. For example, a callback handler might render something which requires the __EVENTVALIDATION field on the client-side to be updated.

Http Exception generated while validating viewstate

I am getting the following error whenever I click on a postbacking control
HttpException (0x80004005): Validation
of viewstate MAC failed. If this
application is hosted by a Web Farm
or cluster, ensure that configuration
specifies the same validationKey and
validation algorithm. AutoGenerate
cannot be used in a cluster.
I am not using a Web Farm or cluster server. I have even tried setting the page property EnableViewStateMac to false but it changes the error message stating
The state information is invalid for
this page and might be corrupted.
What could possibly be wrong?
There is an article about this here: http://blogs.msdn.com/tom/archive/2008/03/14/validation-of-viewstate-mac-failed-error.aspx .
The basic problem is that Your page hasn't completed loading before You perform the postback.
A few different solutions are in the article listed above:
1. Set enableEventValidation to false and viewStateEncryptionMode to Never
2. Mark the form as disabled and then enable it in script once the load is complete.
3. override the Render Event of the page to place the hidden fields for Encrypted Viewstate and Event validation on the top of the form.
But the main problem is that the page load slow, which should be fixed (if possible ASAP). It can also be good to apply solution 2 above as well as there will always be trigger happy users that will click faster that the page loads no matter how fast it loads :-).
/Andreas
I have encountered the same problem with a custom build ASP.NET control which was dynamically reloaded and rebuild on every POST / GET request. Thus the page sending the POST request was not the same as the one recieving the response.
If you use any custom or databound controls look closly how they behave on a POST back.

Resources