0

I have a webmethod in asp.net project i wanna pass a listview in jquery ajax method but i dont now how can i detect elements of listview and using them.folowing code is my c# code.bt i need jquery code

if (ck != null)
{
      reqnum[0, 0] = "@RequestingBranchID";
      reqnum[0, 1] = ck["BranchID"];
      reqnum[1, 0] = "@ProviderBranchID";
      reqnum[1, 1] = customer.ToString();
      DataTable dt = SqlCommands.FillData(out OutStatus, out OutMessage, "BSD.SW_Boxes_StockOfProviderAndRequestingBranch", CommandType.StoredProcedure, reqnum);
      List<DataRow> rows = dt.Rows.Cast<DataRow>().ToList();
      int x=rows.Count;
      return rows;
}
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
heavy
  • 439
  • 2
  • 5
  • 13

2 Answers2

0

Ok let's say that you have a DataTable and you want to pass the results of this DataTable to some JavaScript to display the results using Ajax. So first step is that we need to convert these results into JSON format. you can the following method::

public string GetJson(DataTable dt)
{
    System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
    List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
    Dictionary<string, object> row = null;

    foreach (DataRow dr in dt.Rows) {
        row = new Dictionary<string, object>();
        foreach (DataColumn col in dt.Columns) {
            row.Add(col.ColumnName, dr[col]);
        }
        rows.Add(row);
    }
    return serializer.Serialize(rows);
}

the next step is to parse the result JSON string in JavaScript and i guess this the easy part. You can just check this question to find out how to parse JSON string into objects Safely turning a JSON string into an object .If u want know more tell me.

Community
  • 1
  • 1
ebram khalil
  • 8,252
  • 7
  • 42
  • 60
0

You can use JSON.NET to serialize your datatable to JSON. Then call the webmethod using ajax.

In server side,

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string GetList(int branchID, string customer)
{
    // do your code here
    reqnum[0, 0] = "@RequestingBranchID";
    reqnum[0, 1] = branchID;
    reqnum[1, 0] = "@ProviderBranchID";
    reqnum[1, 1] = customer;
    DataTable dt = SqlCommands.FillData(out OutStatus, out OutMessage, "BSD.SW_Boxes_StockOfProviderAndRequestingBranch", CommandType.StoredProcedure, reqnum);
    if(dt!=null)
    {
          return Newtonsoft.Json.JsonConvert.SerializeObject(dt);
    }
    else{
        //return "[]"; 
    }
}

Then in script call your method and pass the parameters for the method.

$(function(){});
    $('#myButton').click(function() {
        $.ajax({ 
            type: "POST",
            url: "YourPage.aspx/GetList",
            data: "{'branchID':" + branchID + ",'customer':" + customer + "}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                var jsonResult = $.parseJSON(data.d);
                // do your client side code here
            }
        });
    });
});

Note: If you are not using a webservice/writing the method in your code behind, use [ScriptService] decorator before your class and include the following namespaces.

using System.Web.Script.Services;
using System.Web.Services;
Sen Jacob
  • 3,384
  • 3
  • 35
  • 61