Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use ClientScript.RegisterStartUpScript to inject javascript into the page on Page_Load.</p> <p>Here's a link to MSDN reference: <a href="http://msdn.microsoft.com/en-us/library/asz8zsxy.aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/asz8zsxy.aspx</a></p> <p>Here's the code in Page_Load:</p> <pre><code> List&lt;string&gt; tempString = new List&lt;string&gt;(); tempString.Add("Hello"); tempString.Add("World"); StringBuilder sb = new StringBuilder(); sb.Append("&lt;script&gt;"); sb.Append("var testArray = new Array;"); foreach(string str in tempString) { sb.Append("testArray.push('" + str + "');"); } sb.Append("&lt;/script&gt;"); ClientScript.RegisterStartupScript(this.GetType(), "TestArrayScript", sb.ToString()); </code></pre> <p>Notes: Use StringBuilder to build the script string as it will probably be long.</p> <p>And here's the Javascript that checks for the injected array "testArray" before you can work with it:</p> <pre><code>if (testArray) { // do something with testArray } </code></pre> <p>There's 2 problems here:</p> <ol> <li><p>Some consider this intrusive for C# to inject Javascript</p></li> <li><p>We'll have to declare the array at a global context</p></li> </ol> <p>If you can't live with that, another way would be to have the C# code save the Array into View State, then have the JavaScript use PageMethods (or web services) to call back to the server to get that View State object as an array. But I think that may be overkill for something like this.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload