2. November 2009 14:03
/
0
/
Comments (2)
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: http://forums.asp.net/t/1484503.aspx.
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!
45f46650-65a2-4493-bfbd-c3fe74019897|0|.0|27604f05-86ad-47ef-9e05-950bb762570c