Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 472
  • Last Modified:

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?



0
Stephan_Schrandt
Asked:
Stephan_Schrandt
  • 3
  • 2
1 Solution
 
HainKurtSr. System AnalystCommented:
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>
0
 
Stephan_SchrandtAuthor Commented:
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";
            }
};
0
 
HainKurtSr. System AnalystCommented:
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

0
 
Stephan_SchrandtAuthor Commented:
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) & ");};"
0
 
Stephan_SchrandtAuthor Commented:
Thank you
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now