This is a migrated thread and some comments may be shown as answers.

How to clear server variables on unload via ajaxRequest

3 Answers 174 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
Josh Turpen
Top achievements
Rank 1
Josh Turpen asked on 24 Jun 2011, 10:11 PM

I've been having some trouble clearing server variables during unload events of pages using RadAjaxManager.

If I click on a link which navigates away from the page, I need to clear a session variable before continuing.  

TestPage.aspx page:
       <telerik:RadAjaxManager runat="server" ID="RadAjaxManager1" OnAjaxRequest="OnAjaxRequest"  />
        ....
        <a href="google.com">google!</a>
        ...
        <telerik:RadScriptBlock runat="server">
            <script type="text/javascript">
                function onunload(sender, args) {
                    var ajaxManager = $find("<%= RadAjaxManager.GetCurrent(Page).ClientID %>");
                    ajaxManager.ajaxRequest("pageUnload");                    
                }
                Sys.Application.add_unload(onunload);
            </script>
        </telerik:RadScriptBlock>


TestPage.aspx.cs code:

        protected void OnAjaxRequest(object sender, AjaxRequestEventArgs e)
        {
            if (e.Argument == "pageUnload")
            {
                Session["someSessionVar"] = null;
            }
      }

Unfortunately this does not work.  After the OnAjaxRequest event, the browser remains on the TestPage.aspx.  It does not continue navigating to the original link clicked (google.com).  

How can I set a server variable when a page is navigated away from? 

Thanks,
Josh

3 Answers, 1 is accepted

Sort by
0
Genti
Telerik team
answered on 27 Jun 2011, 03:46 PM
Hello Josh Turpen,

Thank you for contacting us.

I do not know what your scenario is, however I would suggest you handle this situation in the following way:
1) Replace the anchor element with a server control:
<asp:LinkButton Text="linkbutton" runat="server" OnClick="goAway" />
2) Then handle the click in code behind. There you can clear the session and redirect the user to some other page:

protected void goAway(object sender, EventArgs e)
{
    Session["someSessionVar"] = null;
    Response.Redirect("http://www.google.com", false);
}

Hope this helps.

Greetings,
Genti
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

0
Josh Turpen
Top achievements
Rank 1
answered on 27 Jun 2011, 07:18 PM
Handling the issue with a server link button doesn't work if the user navigates away from the page via the browser's URL or back button.  This is why I'm trying to handle the issue using the clientside event onunload and sending an ajax request to clear the variable.

Thanks,
Josh
0
Genti
Telerik team
answered on 28 Jun 2011, 11:40 AM
Hi Josh Turpen,

Thank you for the clarification.

In such case, I am following your implementation and providing a sample application that illustrates how the session is cleared.

Hope this helps.

Best wishes,
Genti
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

Tags
Ajax
Asked by
Josh Turpen
Top achievements
Rank 1
Answers by
Genti
Telerik team
Josh Turpen
Top achievements
Rank 1
Share this question
or