views:

27

answers:

4

Hi, I have an .aspx page. I populate a public variable in the code behind (.cs) page and then access that variable in JS on client side. I have the script declared after the FORM tag as below :-

<body>


<form>
...
</form>
    <script language="javascript" type="text/javascript">
        debugger;
        var data = "<%=cSharpData%>";
    </script>
 </body>

After postback this script does not get executed first time, but when I click on any other server button on the page, then it gets executed. Any idea why?

A: 

have you tried to execute it with jquery $(document).ready()? something like:

<script language="javascript" type="text/javascript">
$(document).ready(function(){....});
</scrip>

updated without jquery

/* registr event on document load */
 if ( document.addEventListener ) {
 document.addEventListener( "DOMContentLoaded", log4link, false );
 } else if ( document ) {
 document.attachEvent("onreadystatechange",function(){
         if ( document.readyState === "complete" ) {log4link();}
 });}
jebberwocky
I cannot use jQuery.
Angshuman
added code for without jQuery.
jebberwocky
I am not a JS guru. But, why do I want to add so much code? I just want to populate a variable in JS after page_load has finished processing on server side. Isn't there any simple way out ?
Angshuman
due to different browsers, ie, FF and so on...you could try window.onload.
jebberwocky
Angshuman
window.onload refer to http://javascript.about.com/library/blonload.htm
jebberwocky
A: 

Is your postback done with ajax?

If so then only part of the page is refreshed and the script is not executed again.

You can wrap it in a function and add that function to the postback callback handler.

This is how I did it on one site:

Sys.WebForms.PageRequestManager.getInstance().add_endRequest(function () { edit.update(); });
David Mårtensson
There is no AJAX. The page is made up of standard ASP.Net Server Controls with plain JS.
Angshuman
A: 

Putting some Javascript after a <FORM> element does not mean it will get processed after the form has been submitted. It gets processed as the HTML is rendered, which is what you are seeing.

If you want to run some Javascript after POSTing a form then you need to use a onClick handler, something like this:

<FORM NAME="myform" ACTION="" METHOD="GET">
<INPUT TYPE="text" NAME="inputbox" VALUE=""><P>
<INPUT TYPE="button" NAME="button" Value="Click" onClick="dosomejavascriptstuff()">
</FORM>
Steve Claridge
But, that is not my requirement. I want the JavaScript variable to be populated after the page_load on server side has finished processing.
Angshuman
A: 

Hi All, Thanks for your replies. I got closer to the issue but I do not know the solution :( The C# variable contains a path info as "D:\" and when I set this on a JS variable I get the error as "Unterminated String Constant". How do I overcome this.

Actually this was the problem, why the script was not getting executed. As soon as I removed the "\", it worked !

Angshuman
\ needs to be escaped in js strings as \\ otherwise it escapes the ending ". "D:\" should be "D:\\"
David Mårtensson