1

I am trying to load my dynamically generated Datatable during runtime into JS array in javascript. But cannot pass it through webservice as only static methods can be called through javascript.How to return the datatable into javascript.

aspx.cs file :

protected void btnRead_Click(object sender, EventArgs e)
{
//on button click it reads file path of excel and stores it in a string path 
}

private DataTable ReadExcelWithStream(string path)

{
//method reads the excel file and stores it in a dt
ForJs(dt);
     return dt;
 }
[ScriptMethod, WebMethod]
 public static DataTable ForJs(DataTable dt)
 {

     return dt;
 }

aspx file :

 <script type="text/javascript">
        function InsertLabelData() {
           PageMethods.ForJs(onSuccess, onFailure);

        }

             function onSuccess(dt) {

    //attach the table to dhtmlx grid
    }

But the datatable is not passed to the javascript. How to pass the datatable to javascript from c# ? or any other methods to pass the datatable from c# to JS

Please Help.

Thanks!

2
  • 1
    A brief code sample might help, at the web service layer. Are you using WebAPI?
    – Jake
    Commented May 23, 2014 at 15:14
  • @Jake : I am trying it in webservices. Commented May 23, 2014 at 15:30

1 Answer 1

0

duplicate?/related question: Dynamically pass datatable from C# to Javascript

You can not simply pass an(y) object to JavaScript. It has to be in a usable format. Like an array or serialized to XML or JSON. Also "DataTables contain lots of additional information which JSON cannot store".

Take a look at how to pass c# datatable to a javascript function or What's the best way to jSON serialize a .NET DataTable in WCF? for more information on this subject.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.