UpdatePanel preventing JavaScript from runninghttp://forums.asp.net/t/1526438.aspx/1?UpdatePanel+preventing+JavaScript+from+runningSat, 27 Feb 2010 02:32:03 -050015264383683262http://forums.asp.net/p/1526438/3683262.aspx/1?UpdatePanel+preventing+JavaScript+from+runningUpdatePanel preventing JavaScript from running <p>Hi,</p> <p>Here's what I'm doing: on my page I have a GridView that displays a bunch of people from different parts of the world. I display their names, email addresses, etc. I also wanted to display the local time of their location but I didn't want this to be a snap shot of the time when the page gets rendered so I decided to use JavaScript to display the time and have the clock keep ticking.</p> <p>I found a nice jQuery plug-in called jClock which is very simple to use and implement. I simply place a &lt;span class=&quot;timeZoneID&quot;&gt;&lt;/span&gt; and it works like a clock -- except if I place my GridView in an UpdatePanel.</p> <p>It wasn't working and I kept troubleshooting to no avail. I thought it was the GridView's TemplateField somehow throwing some junk in there. Then I tried removing UpdatePanel and everything started working fine.</p> <p>Why is UpdatePanel preventing this JavaScript from functioning?</p> 2010-02-16T20:39:15-05:003683666http://forums.asp.net/p/1526438/3683666.aspx/1?Re+UpdatePanel+preventing+JavaScript+from+runningRe: UpdatePanel preventing JavaScript from running <p>What's happening is your UpdatePanel refreshes are reverting the DOM back to how it was before jQuery wired up those handlers.&nbsp; The quick and dirty fix is to move that jQuery code into pageLoad() instead of &#36;(document).ready(), since <a href="http://encosia.com/2007/08/01/simplify-aspnet-ajax-client-side-page-initialization/"> pageLoad fires after every partial postback</a>.</p> 2010-02-17T02:20:48-05:003683758http://forums.asp.net/p/1526438/3683758.aspx/1?Re+UpdatePanel+preventing+JavaScript+from+runningRe: UpdatePanel preventing JavaScript from running <p>Friend This is very genuine problem&nbsp;occur&nbsp;with developers</p> <p>You have to add script from the code Behind</p> <p>Use ScriptManager Class to do so.</p> <p>This is same as ClientScript Property of Page. This class expose all shared function as ClientScript has.</p> <p>For Example The function in Javascript</p> <p></p> <pre class="prettyprint">function xyz(){ alert('Hello'); }</pre><p><br>Use it from Code Behind</p><p></p><pre class="prettyprint">Dim script As String = "" script += "function xyz(){" script += " alert('Hello');" script += "}" ScriptManager.RegisterClientScriptBlock(Me, Me.GetType, "AsyncScript", script, True)</pre> <p><br> </p> <p><br> </p> <p>Use it inside any&nbsp;relevant&nbsp;event handler</p> <p>Your Javascript will work properly inside update panel</p> <p></p> <p></p> 2010-02-17T03:22:46-05:003685930http://forums.asp.net/p/1526438/3685930.aspx/1?Re+UpdatePanel+preventing+JavaScript+from+runningRe: UpdatePanel preventing JavaScript from running <p>Thank you both very much for your help.</p> <p>One follow up question:</p> <p>I put the jQuery code in an external file. How would I call/load this script from code behind in the Page_Load() event? This would really help me keep my code behind nice and neat.</p> 2010-02-17T23:07:46-05:003686715http://forums.asp.net/p/1526438/3686715.aspx/1?Re+UpdatePanel+preventing+JavaScript+from+runningRe: UpdatePanel preventing JavaScript from running <p>Good You can do it. We have two situation</p> <p>If the code has to invoke from within the update panel you have to use ScriptManager Class</p> <p>Else use ClientScript Property</p> <p></p> <pre class="prettyprint">Dim Url as string=&quot;JSFilePath&quot; 'In Case of Update Panel ScriptManager.RegisterClientScriptInclude(Me, Me.GetType, &quot;Key&quot;, Url) 'In normal case ClientScript.RegisterClientScriptInclude(&quot;Key&quot;, Url)</pre> <p><br> </p> <p><br> </p> <p><b><span style="color:rgb(255,102,0)">One More Important thing if Post is helpful please mark it as Answer.</span></b></p> <p><b><span style="color:rgb(255,102,0)">It encourage us to answer</span></b>&nbsp;</p> <p></p> 2010-02-18T07:45:22-05:003686716http://forums.asp.net/p/1526438/3686716.aspx/1?Re+UpdatePanel+preventing+JavaScript+from+runningRe: UpdatePanel preventing JavaScript from running <p>Good You can do it. We have two situation</p> <p>If the code has to invoke from within the update panel you have to use ScriptManager Class</p> <p>Else use ClientScript Property</p> <p><br> </p> <pre class="prettyprint">Dim Url as string=&quot;JSFilePath&quot; 'In Case of Update Panel ScriptManager.RegisterClientScriptInclude(Me, Me.GetType, &quot;Key&quot;, Url) 'In normal case ClientScript.RegisterClientScriptInclude(&quot;Key&quot;, Url)</pre> <p><br> </p> <p><br> </p> <p><span class="Apple-style-span" style="font-weight:bold"><span style="color:rgb(255,102,0)">One More Important thing if Post is helpful please mark it as Answer.</span></span></p> <p><span class="Apple-style-span" style="font-weight:bold"><span style="color:rgb(255,102,0)">It encourage us to answer</span></span>&nbsp;</p> <p><br> </p> 2010-02-18T07:45:51-05:003688310http://forums.asp.net/p/1526438/3688310.aspx/1?Re+UpdatePanel+preventing+JavaScript+from+runningRe: UpdatePanel preventing JavaScript from running <p>Please&nbsp;keep the javascript code stay outside of updatepanel. And please make&nbsp;sure the id is correct&nbsp;in JQuery reference.</p> <p>BTW, did you&nbsp;get any client error?&nbsp;</p> 2010-02-19T02:30:49-05:003689370http://forums.asp.net/p/1526438/3689370.aspx/1?Re+UpdatePanel+preventing+JavaScript+from+runningRe: UpdatePanel preventing JavaScript from running <p>The jQuery/JavaScript reference was in the HEAD section of the page. I wasn't getting any errors. Script just didn't work.</p> <p>Not sure how to keep the script outside of UPDATEPANEL. THe script reference was in the HEAD section of the page but what the script will effect has to be inside the UPDATEPANEL because it displays local times for people displayed in GridView rows which by definition has to be inside the panel. So not sure what you're suggesting. Would you please clarify further?</p> 2010-02-19T14:20:20-05:003703838http://forums.asp.net/p/1526438/3703838.aspx/1?Re+UpdatePanel+preventing+JavaScript+from+runningRe: UpdatePanel preventing JavaScript from running <p><br> </p> 2010-02-27T02:32:03-05:00