avoid execution of javascript on postback with updatepanel and timer

Posted on 2009-12-21
Last Modified: 2012-05-08
I have a masterpage with an updatepanel and a timer in it. Another page shows some data in a gridview.
Within the gridview in edititemtemplate there are two dropdownlists c1 and c2 .
I add a javascript function on Gridview_RowDataBound when it's in editmode with Sys.Application.add_load and ScriptManager.RegisterStartupScript. The javascript toggles the display style (block or none) of c2 depending on the value of c1 and calls the javascript function scrollintoview on c2. I'm sure by reading you know what's happening: Each time the timer fires, my c2 is scrolled into view. So the question is:

How do I avoid multiple calls to a javascript function registered as startupscript when the timer fires?

Question by:Stephan_Schrandt
    LVL 51

    Expert Comment

    can you please post some code?
    maybe you can try something like this

    var fired = false;

    function timerEvent(){
      if fired return;
      .... do whatever you want here ...
      fired = true;
    LVL 9

    Author Comment

    Yes of course I can, but please notice that I'm looking for a solution that works with all javascript, not only for this function. I know I can avoid it with flags, but that will not be helpful in the future with other javascript functions.

    I've shortened it a bit, this is the core:

    Protected Sub gvIncident_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvIncident.RowDataBound

          s = "Sys.Application.add_load(InitToggleAE" & divAE.ClientID & ");function InitToggleAE" & divAE.ClientID & "() {toggleDisplay('" & divAE.ClientID & "'," & Convert.ToInt32(ddlAE.SelectedValue) & ");};"
          ScriptManager.RegisterStartupScript(Me.Page, Page.GetType, "InitToggleAE" & divAE.ClientID, s, True)

    End Sub

    The Javascript function:
    function toggleDisplay(ctrlID,value)
          var toggle = document.getElementById(ctrlID);
          if (value == 1)
    LVL 51

    Accepted Solution

    cant you add the flag to script like this

    ;) I hope you get the idea below (it may contain syntax errors)
    s = "Sys.Application.add_load(var "& divAE.ClientID &"_fired = false; InitToggleAE" & divAE.ClientID & ");function InitToggleAE" & divAE.ClientID & "() {if (!"& divAE.ClientID &"_fired) toggleDisplay('" & divAE.ClientID & "'," & Convert.ToInt32(ddlAE.SelectedValue) & ");"& divAE.ClientID &"_fired = true};"

    Open in new window

    LVL 9

    Author Comment

    You are right, it works for this function. And because that's ecactly what I used to work around this behaviour I accept your answer as solution. But for your interest the right way is to extend the function with two arguments that are supplied by default (I did some research on my own the last hours :-))

    s = "Sys.Application.add_load(InitToggleAE" & divAE.ClientID & ");function InitToggleAE" & divAE.ClientID & "(sender, args) {if (args.get_isPartialLoad()) return;toggleDisplay('" & divAE.ClientID & "'," & Convert.ToInt32(ddlAE.SelectedValue) & ",0);ValidatorEnable2('" & calArbeitEingestelltDatum.ValidatorClientID & "'," & Convert.ToInt32(ddlAE.SelectedValue) & ");};"
    LVL 9

    Author Closing Comment

    Thank you

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
    This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now