Link to home
Start Free TrialLog in
Avatar of Stephan_Schrandt
Stephan_SchrandtFlag for Germany

asked on

avoid execution of javascript on postback with updatepanel and timer

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?



Avatar of HainKurt
HainKurt
Flag of Canada image

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

<script>
var fired = false;

function timerEvent(){
  if fired return;
  .... do whatever you want here ...
  fired = true;
}
</script>
Avatar of Stephan_Schrandt

ASKER

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)
            {
                  toggle.style.display="block";
                  toggle.scrollIntoView();
            }
      else
            {
                  toggle.style.display="none";
            }
};
ASKER CERTIFIED SOLUTION
Avatar of HainKurt
HainKurt
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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) & ");};"
Thank you