views:

1817

answers:

2

I'm using ASP.NET Ajax. How can I tell the browser to close the current window after the server call finishes (server-side code)?

I managed to do this using the ASP.NET Ajax's ScriptManager method "RegisterDataItem" in the server method (inside a button click handler):

sm.RegisterDataItem(ActionLabel, "action:closewindow")

and a hidden Label and handling it this way on the client:

function PageLoadingHandler(sender, args)
{
    var dataItems = args.get_dataItems();
    if (dataItems['ActionLabel'] == 'action:closewindow') {
        window.close()
    }
}
Sys.WebForms.PageRequestManager.getInstance().add_pageLoading(PageLoadingHandler);

But it feels dirty. There must be a more elegant way to do this.

Thanks for any advice.

+1  A: 

If I understood you right, registering window.close() with ScriptManagers RegisterStartupScript method on event handler should work. For details on using ScriptManager.RegisterStartupScript see MSDN

PiRX
Aargh! Yes, that worked. Many thanks! I was sure I've tried this before.
splattne
A: 

How to force logoff in ASP.Net when browser closes..