geminiCoder
geminiCoder

Reputation: 2906

body onLoad not calling dynamicly made javascript function

Thanks for all your help so far I have made the following changes

This is the code on run:

<body>
<form method="post" action="ResultsDetails.aspx" id="form1">
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE"    value="/wEPDwUJNzQ5NzY1NjU0ZGRuWExqnYyaWn0sRggTtOIdHlawc3aZvdNLKpOq0D+uMQ==" />
</div>

 <div id="placeholder" style="width:600px;height:300px;"></div>


<script type="text/javascript">
//<![CDATA[
var arrayOfResults = new Array(); 
 arrayOfResults[0] = 2; 
 arrayOfResults[1] = 4; 
arrayOfResults[2] = 5; 
arrayOfResults[3] = 1; 
arrayOfResults[4] = 4; 
 var numberArray = Array(1, 2, 3, 4, 5); 
  //]]>
</script>
</form>

<div id="codeGeneration">
  <script type="text/javascript">
   $(document).ready(function () {
      $.plot($('#placeholder'), [arrayOfResults, numberArray]); 
     });


</script>
</div>

</body>

The plot method is being run but i keep gettin the error saying that the arrays havent been initalised?

im using

 ClientScript.RegisterStartupScript(this.GetType(), "run", sb.ToString() ,true);

to write the array code.


Sloved I did the following

  private void generateJScriptArray(int[] array)
{
    StringBuilder sb = new StringBuilder();        
    sb.Append("var arrayOfResults = Array(); \n ");

    for (int i = 0; i < array.Length; i++)
    {
        sb.Append("arrayOfResults[" + i + "] = " + array[i] + "; \n ");
    }

    sb.Append("var numberArray = Array(1, 2, 3, 4, 5); \n");

    sb.Append("alert(numberArray[0]); \n");

    sb.Append("$(document).ready(function () { \n  $.plot($('#placeholder'), [arrayOfResults, numberArray]); \n });");

    ClientScript.RegisterStartupScript(this.GetType(), "run", sb.ToString() ,true);
}

Thanks to all who helped.

Upvotes: 1

Views: 1472

Answers (3)

gerardrubio
gerardrubio

Reputation: 11

You could try

<body onload="javascript:run()">

Or seeing that you're loading jQuery, why don't you try executing the run function with jQuery's document ready event, instead of the body onload.

$(function(){
    run();
});

Take into account when initializing the numberArray array, it should be

var numberArray = [1, 2, 3, 4, 5];
//instead of
int [] numberArray = {1, 2, 3, 4, 5}; //It feels like C#

Also, remove the following

(function () { 
    $.plot($('#placeholder'), [arrayOfResults, numberArray]);
} //missing );

from your run function and leave only the plot line

$.plot($('#placeholder'), [arrayOfResults, numberArray]);

as the function run will be called when the page and all external scripts are loaded (either using onload or jQuery document ready)

Hope it helps,

gR

Upvotes: 1

Mayank Pathak
Mayank Pathak

Reputation: 3681

you can also do it without using script manager..

string Code = "<script> alert('Foo Alert');</script>";
ClientScript.RegisterStartupScript(this.GetType(), "someKey", Code);

Upvotes: 1

Icarus
Icarus

Reputation: 63964

Use the ScriptManager to register the startup script. Something like this:

ScriptManager.RegisterStartupScript(this, this.GetType(),"somekey","your run script",true);

Upvotes: 5

Related Questions