How i get server side jquery datatable length ,start,draw value in asp.net aspx page .

How i get server side jquery datatable length ,start,draw value in asp.net aspx page .

hinalhinal Posts: 2Questions: 1Answers: 0
edited May 2017 in Free community support

How i get server side jquery datatable length ,start,draw valuein asp.net aspx page .

Answers

  • bindridbindrid Posts: 730Questions: 0Answers: 119
    edited May 2017

    You would be better servered but having said that, google how to set up a webmethod (one word) on an aspx page. Once you get that working, you can search StackOverflow and this form on how to connect ajax to that web method. I put several examples out there. There are also some baseline classes you can deserialize the ajax request

  • hinalhinal Posts: 2Questions: 1Answers: 0

    i have already used web method and its working but i want the value of length ,start and draw in my web method .and i have used
    int record = Convert.ToInt32(HttpContext.Current.Request.Params["length"]); in my web method but i am not getting the value.so how to fetch the value of length ,start and draw in web method.

  • bindridbindrid Posts: 730Questions: 0Answers: 119

    Here is how I do it.
    Server Side:

    // class that the client side parameters are deserialized into including draw, start, length so on. It matches what DataTables sends out.

           public class DataTableParameter
            {
                public int draw { get; set; }
                public int length { get; set; }
                public int start { get; set; }
                public List<columm> columns { get; set; }
            }
            public struct columm
            {
                public string data;
                public string name;
                public Boolean searchable;
                public Boolean orderable;
                public searchValue Search;
            }
            public struct searchValue
            {
                public string value;
                public Boolean regex;
            }
    

    This is the class that sends the data back to the client (scripting services serializes it.

            public struct DataTableResponse
            {
                public int draw;
                public int recordsTotal;
                public int recordsFiltered;
                public List<dtData> data;
            }
            // dtData matches the data structure found on most the DataTable web site.
            public class dtData
            {
                public string name;
                public string position;
                public Int32 salary;
                public string start_date;
                public string office;
                public string extn;
            }
    

    My asmx page web method

             [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        [System.ComponentModel.ToolboxItem(false)]
        // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
        [System.Web.Script.Services.ScriptService]
       
        public class wsService : System.Web.Services.WebService
        {
            [WebMethod]
            public DataTableResponse GetDTDataUnserializedObject(DataTableParameter dtParameters)
            {
                DataTableResponse res = getTheData(dtParameters);
                res.draw = dtParameters.draw;
                return res;
            }
    }
    

    And finally my client side

                // I declare my columns separate because sometimes they are used in 
                // multiple tables or for other stuff
                var columns = [
                    { "data": "name", "name": "name", orderable: true, sel: [] },
                      { "data": "position", "name": "position", orderable: true, sel: [] },
                    { "data": "office", "name": "office", orderable: true, sel: [] },
                    { "data": "extn", "name": "phone", orderable: true, sel: [] },
                    { "data": "start_date", "name": "startdata", orderable: true,  sel:[]},
                    { "data": "salary", "name": "salary", orderable: true, sel:[] , searchable:false}
                ];
               
    
                var table = $('#example').DataTable({
                    "processing": true,
                    "serverSide": true,
                    "columns":columns,
                    "lengthMenu": [[10, 15, 25, 50, -1], [10, 15, 25, 50, "All"]],
                    "pageLength": 15,
                    "ajax": {
                        contentType: "application/json; charset=utf-8",
                        url: "wsService.asmx/GetDTDataUnserializedObject",
                        type: "Post",
                        data: function (dtParms) {
                            // notice dtParameters exactly matches the web service
                            return JSON.stringify({ dtParameters: dtParms });
                        },
                        // Data filter is a part of ajax and lets you look at the raw 
                        dataFilter: function (res) {
                            // You probably know by know that WebServices 
                            // puts your data in an object call d. these two lines puts it 
                            // in the form that DataTable expects
                            var parsed = JSON.parse(res);
                            return  JSON.stringify(parsed.d);
                        },
                        error: function (x, y) {
                            console.log(x);
                        }
                    },
                    order: [[0, 'asc']]
                });
    
This discussion has been closed.