Jonas Stawski

Everything .NET and More

JavaScript not working within UpdatePanel

The other day I was having a problem with JavaScript functions not being found. The simplified setup was similar to this:

- UpdatePanel
   - UserControl (Visible = true)
   - UserControl (Visible = false)
      - Embedded JavaScript and Controls

There was some controls on the page that triggered an asynchronous postback which changed the visibility of the UserControls and when the Embedded JavaScript was called I was getting JS errors. Here’s the question I asked on the forums:

According to InfinitiesLoop:

The over simplified way of explaining how UpdatePanel does its work on the client is through the innerHTML DOM property. When a delta is retrieved from the server, it finds itself in the existing DOM, disposes of the contents, and then assigns the new content via innerHTML. Thanks to a basically consistent behavior across the major browsers, the new content appears just as if it were there to begin with.

But inline script doesn't work this way. Setting the innerHTML of a DOM element to HTML which contains a script block does not cause that script to execute. The only way to execute arbitrary script dynamically is to eval() it directly, or dynamically create a script element with document.createElement("script") and inject it into the DOM.

So to solve my problem I dynamically created the embedded JS through the use of the ScriptManager on the Page Load:

   1: ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "SomeUniqueID", "function HelloWorld() { alert('Hello World'); }", true);

Happy Programming!

Comments (2) -

in update panel javascript is not working can anyone help me out asap...



not work java script and jquary


Add comment