Question

After searching online with multiple different keywords, I haven't been able to find the solution to my problem.

I started by looking for a way to popup a message asking for a user to renew their session a minute before it cancels. The solution I decided to use was the run a javascript timer in the masterpage and have it be called on a Masterpage.Page_Load(). In this way, whenever my user navigates from page to page, they reset the timer. However, because many of the pages the user will be using have updatepanels and update asynchronously, the function I am using is being reached, but being skipped over (according to the debugger).

The function I am using is:

ScriptManager.RegisterStartupScript(main, this.GetType(), "callTimer", "callTimer();", true);

This function runs fine on a regular postbacks, or asynchronous postbacks on the masterpage (I update the timer by doing a postback on a updatepanel in the masterpage), however, will be skipped on asynchronous postbacks.

I have tried a few things, making the control of the function Page or the UpdatePanel in the masterpage or the updatepanel in the childpages. I have tried putting this code in the childpages as well, but while these change do reset the timer, it causes the javascriptcode to run multiple times and they fight over the label displaying the countdown.

I'm really at a loss here. I think a possible solution would be to change the startupscript's control and terminate all javascripts running before that code again. However searching for a way to terminate, cancel or stop javascript functions programatically yields no useful results as well.

Another solution I would be okay with is reseting the timer on mouse movement, but since I'm designing for IE8, this will not work unless the window is out of focus.

Was it helpful?

Solution 2

I found a solution to the problem: I was able to solve this by sending the current date.time as a number to the javascript function. from there I set the value of the countdown label to the date.time. there, during my timer if I found that the date.time of the label didn't match the one that the function started with, it would return and end the function. This meant that only the latest instance of the function would remain running at any given time.

OTHER TIPS

You need to inject the script through the ScriptManager in the case of a partial postback or it will not be re-executed. Here's a utility function (vb.net) to handle the injection logic for you:

Public Shared Sub InjectScript(ByVal containingPage As Page, ByVal scriptKey As String, ByVal scriptText As String)
    Dim sManager = ScriptManager.GetCurrent(containingPage)

    If sManager IsNot Nothing AndAlso sManager.IsInAsyncPostBack Then
        //the page is being partially-posted back by an Ajax control (e.g. UpdatePanel)
         ScriptManager.RegisterStartupScript(containingPage, GetType(Page), scriptKey, scriptText, True)
    Else
        //standard postback
        containingPage.ClientScript.RegisterStartupScript(GetType(Page), scriptKey, scriptText, True)
    End If
End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top