Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You have some options for this. First of all, I assume that what you need is the actual data and not any functionality given by the DataTable object, so you better create an entity (could be a <code>struct</code>) to store the table's data —it must be marked with <code>Serializable</code> attribute.</p> <p>One of your options is to encapsulate retrieving it in a <em>Page Method</em>. These methods are exposed to JavaScript transparently (i.e., they have the same syntax as in the code behind). These methods need to be <code>static</code>, and be marked with the <code>PageMethod</code> attribute.</p> <p>Another option would be to "write" the content of your table in the HTML in JSON format (a notation to represent objects in JavaScript).</p> <p><strong>Edit 2</strong>: The way to serialize something into JSON is by using the class <code>System.Web.Script.Serialization.JavaScriptSerializer</code>, creating an instance and calling <code>.Serialize(&lt;your-serializable-object&gt;)</code>.</p> <p><strong>Edit</strong>: Added the following (working) example...</p> <p><em>ASPX</em> page:</p> <pre><code>&lt;script type="text/javascript"&gt;// &lt;![CDATA[ function doTest( ) { try { PageMethods.test(theForm.qty.value, function( result ) { var str = ''; for ( var key in result ) { str += '\t{ '+ key +': '+ result[ key ] +' }\n'; } alert('Page Method returned the following result:\n'+ (str || "{ }")); }, function( ex ) { alert('Page Method returned the following "'+ ex.get_exceptionType() +'" exception:\n'+ ex.get_message()); }); } catch( err ) { alert('Failed to execute Page Method:\n'+ err); } } // void doTest() // ]]&gt; &lt;/script&gt; ··· &lt;form runat="server" id="form"&gt; &lt;asp:scriptmanager runat="server" enablepagemethods="true" /&gt; &lt;fieldset id="forma"&gt; &lt;legend&gt;WebMethod Test&lt;/legend&gt; &lt;label for="qty"&gt; Number of elements (&gt;= 0): &lt;input type="text" name="qty" id="qty" value="5" /&gt; &lt;/label&gt; &lt;input type="button" value="Push the button!" onclick="doTest(this)" /&gt; &lt;/fieldset&gt; &lt;/form&gt; </code></pre> <p>And, on your <em>code behind</em>:</p> <pre><code>using System; using System.Collections.Generic; using System.Web.Services; namespace Tests { public partial class Test : System.Web.UI.Page { [WebMethod] static public IDictionary&lt;string,string&gt; test( int length ) { if ( 0 &gt; length ) { throw new ArgumentException( "Length cannot be less than zero", "length" ); } var result = new Dictionary&lt;string,string&gt;( length ); for ( int i = 0; i &lt; length; i ++ ) { result.Add(i.ToString(), Char.ConvertFromUtf32(65 + i) +"|"+ Char.ConvertFromUtf32(97 + i)); } return result; } // IDictionary&lt;string,string&gt; test( int ) } } </code></pre>
    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