PageClose Event in asp.net - c#

when user close the web site that time i need to call some function here, what was the event for user close the page or browser in asp.net(C#).

You cannot do this with asp.net. The server does not know when the user closes the window. However you can use javascript, and attach an beforeunload event to the body tag.
window.onbeforeunload = function (e) {
//javascript code here
}
Here is a good SO thread on how this can be achieved.

There is no event in asp.net that could catch the event of user closing the window, because that happens on client-side. Your C# code runs on the server without any knowledge of the client's software.
There are some mechanisms using javascript and sending some data using ajax, but no one should rely on that.

Related

PopUp Message while navigating from one aspx page to another

Can anybody help me out with the popup message related concept in ASP.NET.
I have been working in ASP.NET MVC, and I am getting no popup message with the below code while navigating from one aspx page to another.
Response.Write("<script>alert('Data is Updated')</script>");
Response.Redirect("WebForm2.aspx");
The pop-up is not occurring because of the redirect. Both the alert and the redirect are being done in server-side code. There is no way to wait for user interaction on the client side because the server-side code has already executed by the time the user sees it.
You could move the alert and redirect to client side JavaScript. The redirect would not fire until the alert is closed.
alert("Successful");
window.location = "WebForm2.aspx";
Or, you could send the message as an url parameter in your server-side redirect, and handle the message display on WebForm2.aspx.
Response.Redirect("WebForm2.aspx?status=successful");

Is it possible to init a window on the client, server side (ASP.net)

In my C# code behind file I want to init a window on the client?
Ie. Window.open();
This should be fed with DialogTitle and DialogText and sent to the client.
I can set the window in the HTML and hide it until .open(); is called?
Is this task even possible Server side?
You can't call anything on the client from the server. However, you can send code to do so, for example JavaScript code or just plain HTML. If you are using ASP.NET Web Forms, the Page.RegisterClientScriptBlock method can be useful.
When you render the page, or a AJAX callback, put some <script> element in there with the code to open the window. Then you are all set. If it is an AJAX call, you have to execute the script, which is a little harder than just rendering the script element. See here how to do that.

Get close window button in asp.net C#(server side)

I want to get the [browser] close button event in my web app.
I know how to get it client side by javascript but there are some problems within.
Is it possible to get this event server side in my web app?
Thanks
Can't be done through server code, but you can add some code on your server-side event which adds <script type="text\javascript">window.close();</script> when it finishes.
Have a look at ClientScriptManager.RegisterClientScriptBlock.

Invoke code-behind method when browser is closing

I need to find a way to intercept browser closing and invoke a metod to update a record with several information about logged user in DB. It's very important this record is updated when the user is logging-out or when he close the browser. Obviously when the user clicks 'Logout' I handle the update in the server-side event, but what if the user simply exit from browser?
Someone suggest to use the window.onbeforeunload event and make an asynchronous call to some WS or WebMethod to execute the code, but this doesn't convince me at all: the problem with onbeforeunload is that it shows a confirm prompt. I need to avoid this message and simply invoke the method.
So I'm wondering if there is a 'server-side' solution without using ajax or javascript.
For example... a way to trigger some event on session abandon or session clear, or some other way to solve this problem just working on code-behind...
There is no way you could have a server-side solution to know something that happens in the client browser.
I do not believe there is any way to do what you need server-side only. Client side is the only way. Server has no way of knowing when browser window was closed, this is limitation of HTTP protocol.
Yes, you can put an event in the Global.AsaX which will fire when the session ends. Now if you need data from the client to update the db etc., you'll need a way of getting it there, but if not, then the Session_End will do the trick.
Note: Session end is slightly different than the browser closing, so it this will depend on what you want the event firing to do.
How to handle session end in global.asax?
I'd like to find a 'server-side' solution without using ajax or
javascript.
I suspect that it's impossible with that requirement.
Maybe you could do something like:
Have a hidden IFRAME on the page
Set the Refresh header on this IFRAME (or use a META element) to contact the server every couple of seconds
If you do not hear from the client for some period of time, assume the browser has been closed.
However, I imagine that this solution will not scale well.
Have you considered something like signalr? I use it to detect when someone has a record open.
public class ChatHub : Hub
{
public override Task OnDisconnected()
{
Broadcaster.Disconnected(Context.ConnectionId);
return base.OnDisconnected();
}
}
For the moment I changed radically the approach to my problem.
To update pending rows I implemented a timed job using Quartz.NET framework, that runs every night.

Performing click of a link button from a webservice in c#

I have a webservice and a default.aspx page in c#.
I want to perform a click of the link button on default page from the webservice.
How can i call the linkbutton of the default.aspx from the webservice?
Please suggest a genuine way for doing this.
Web services cannot "click" buttons. They can return data or perform certain process on their end; you in turn can hook into any callback function after the web service finishes processing to perform the click on your end.
Example:
$.ajax({
url: "URLTOWEBService.here",
success: function(){
//webservice returned successfully
performClick();
}
});
What does the button do? Instead of trying to click it remotely you should change architecture of your system to invoke logic under button via web service.
the closest you can get is if you return a javascript that "clicks" the button and is evaluated by the browser client.
The webservice itself can't do jack since its running on the server.

Categories

Resources