onClose Event in ASP.NET - asp.net

Is there an event like onLoad?
just at closing the Site (onClose)?

Not in the sense that I suspect you're thinking. All your code in ASP.net runs on the server which doesn't inherently "know" when the site is closed by the user. To achieve what you're after, you'd need to use Javascript to handle the window.onunload / window.onbeforeunload events and trigger some form of request to the server.
Take a look at http://developer.mozilla.org/en/DOM/window.onunload for more information on the window.onunload event.
Update: If you're using/able to use ASP.net AJAX, there's some quite tidy wrappers there that provide you with a similar set of events/beahviours to the server-side page lifecycle. There's a pretty decent tutorial from Stephen Walther on his blog.

I would suggest using asp.net ajax for this. See this link of page lfecycle for this, which shows the application.unload event (triggers when the page is being closed, navigated away from).
This fits well with asp.net for obvious reasons, better than using onunload event directly, unless of course you do everything without asp.net ajax. The only thing it cant do is notify you in the case the browser crashes.

You could use with limitations Session_End method in ASP.NET application file (Global.asax).

"Closing the side" means that the user closes the browser window on his client, right?
In JavaScript you have the OnUnload-event. If you need an event on the server, then you might be able (I never tried) to push the event to the server by hand using a custom client script. But you do not have it out-of-the box, because it's a client event.
Hope this helps.

The closest you can get it UnLoad.
http://msdn.microsoft.com/en-us/library/ms178472.aspx

Related

HTTPModule as ajax handler (IIS7+)

i have an argument at work about the best way to implement ajax handling for forms. we build all our forms under asp.net platform (sharepoint to be exact) but since we talk about public sites we though to cut the time-costing postbacks and make all postback in ajax request.
in order to best implement the server side i took a bit deeper look into handlers and modules and i then thought that i can cut all that by implementing a module that in his beginRequest identifies if its an ajax request (say by a special element in the querystring), and if so call CompleteRequest() and then in this module's endRequest if it was our ajax (say i flagged it in beginRequest) handle it.
so i would like to hear your thoughts about the pro's and con's about it, and also how "dangerously" it can impact the entire website ect.
the other option is, o.c., a generic handler (ajaxHandler.ashx).
i am again stating we are talking about IIS7+ where they integrated the pipelines.
p.s.
another thought is to build a WCF that will have to "open" (create instances) of anything that i would already have in my handler/module (in sharepoint for example the SPContext)
regards
Bresleveloper

asp.net page wait others server side / asynchrone page

I created an asp.net page for waiting ajax. I have one page creating something that takes 30 seconds. On every step I change a session value.
I have another page for ajax, returning the session value for showing the percentage of creation. But, I dont know why, my ajax page awaits the end of the creation of my first page. So I only get the 100% at the end.
Maybe it's because I use VS development server and not IIS server. If this is the problem, can I change settings of the development server for asynchrone execution?
Or is it something else?
WebForms are not ideal for asynchronous operations.
Add SignalR to your project and use a Hub to push status data back to your page to update the current state of the process you are running Asynchronously.
An example of a technique to perform this type of asynchronous notification is covered in my blog post titled "A Guide to using ASP.Net SignalR with RadNotifications"
Don't use ASP.Net session state to do that. It has an implicit reader/writer lock around it, meaning your other call is probably blocking until your process finishes. You can try storing your status in a database or the cache, but it would probably be better to redesign the interaction.

Webmethod authentication not passed

I'm new to this AJAX approach when you're not supposed to use UpdatePanel but rather go with WebMethods and WebServices. My main problem is authentication.
When page is entered or postback request is created, everything works fine. Authentication is passed correctly and I can access Thread.CurrentPrincipal.Identity and get my user identity object from there.
However this changes when I try to call WebMethod on my page. Call is passed correctly to server and everything seems to work just fine until i try to get user identity from thread. Then I get just Anonymous user instead of real one. Enabling session on webmethod didn't seem to help much.
Any ideas what might cause this problem and how to solve it? Someone mentioned that authentication cookie needs to be passed along with the request, but how am I supposed to do it?
Any help will be appreciated.
Edit:
Some clarification and code:
My application is written in standard asp.net. After some deeper research in legacy code I've found out, that all authentications are done in some base class from wchich all other pages inherit. Each time page is loaded, user principal are obtained from HttpContext.Current.Session("..."). I think this is far from good solution, but I'll need to stick with it right now. Problem was, WebMethod is not firing whole page lifecycle since it's static. I've fixed it right now by calling method that obtains user data from session.
I would like to get some ideas how this could be created correctly and what problems might be result of session based authentication.
PageMethods.SomeMethod(parameter, SuccessDelegate, FailureDelegate);
This is how I'm calling WebMethods right now. I assume it's passing all required cookies, am I right?
It depends on how you're calling the method and in what manner?
Jquery for instance with its Post method should push all cookies (including your FormsAuth / Session cookie) up with the request that should still authenticate as appropriate. Bare metal techniques might be making lightweight calls that simply do not push the cookie up...One way to monitor this is by using Fiddler to observe the request and a browser based development plugin like Firebug and see what is occuring and amend your JS code as appropriate.
Personally, if you are starting a brand new project and there is no pressing need to expose your services beyond your web application then I would suggest looking at ASP.NET MVC where you can make Jquery / client-side up to the controller and get your authentication wrapped up for free. I've recently created something simliar using WCF JSON endpoints and some inevitable pain, I then saw MVC and kinda kicked myself...
As noted in comment above, the issue lies in legacy code that handles users. It is needed to make call to special function that assigns appropriate user data to handling thread. Not a best solution, but that's how it sometimes is with legacy code. What you gonna do?

ASP.net server event handled client-side

I was wondering if anyone had an easy way to wire up javascript event handlers to events happening on the server-side. I have a long running process that includes a lot of steps, and would like the client to be continually updated with new information as the steps transition. Will this involve some sort of polling mechanism?
Send an AJAX or JSON request from the client every so often asking for status.xml. Then, on the server, when something changes, just quickly write a new line to status.xml the same way you would to the console. You can use setInterval( function, timeBetweenRuns ) in Javascript to do this regularly.
would it be possible to use an update panel to do this with maybe a timer? it's sort of how I would do it.
Course, this depends on what you're doing already. I'm assuming you're using Forms...
Ajax calling a Web Service is how I would do it.

ASP.NET AJAX and keeping the Session alive - What is the standard way to do it?

Long time ASP.NET Webforms developer, new to Ajax development (mostly via the UpdatePanel control !).
Since no postbacks are happening the server does not reset the user’s session timeout counter, even though a user is interacting with a page and refreshing parts of it.
I would like to know what is the simplest and standard way of keeping the ASP.NET session alive, when one is developing Ajax with the UpdatePanel;
Please provide code and/or links in your answer; The stuff i searched talks about frameworks, JQuery, JSON, and whatever is the flavour of the month acronym, i don't need anything fancy, just the plain standard way of doing it, i don't care if it's not optimized, etc :-|
As GenericTypeTea commented, there are server side postbacks when using .NET AJAX with UpdatePanel. The only difference is that the entire page is not loaded. All the server side code is triggered as normal, but the information is sent between the server and the page in a different way using the AJAX technology.
You can even call server side methods from client side javascript using a mechanism that .NET calls PageMethods. This is a more "manual" way of using AJAX in .NET than the traditional UpdatePanel technique.
I would say using a hidden iframe for communication would be the easiest wa ywith least hassle.
http://weblogs.asp.net/stephensonger/archive/2009/04/22/keeping-asp-net-sessions-alive.aspx
has a good example that you can expand on.

Resources