We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

XML binding

Hello,

How to bind the Grid with a XML returned by a PageMethod? The binding must be by javascript code and the PageMethod has parameter.

Thanks!

3 Replies

JK Jayaprakash Kamaraj Syncfusion Team November 13, 2015 12:24 PM UTC

Hi Jorge,
We have created a sample for binding datasource for the Grid from server using “PageMethods”. Please refer to the code example.

Code Example:


Site.Master


<%--Enable PagerMethods--%>

<asp:ScriptManager runat="server" EnablePageMethods="true">

</asp:ScriptManager>


Default.aspx

   <ej:Button Type="Button" runat="server" ClientSideOnClick="btnClick" Text="button"></ej:Button>

<ej:Grid ID="FlatGrid" runat="server" AllowSorting="True" AllowPaging="True">
. . . . 
</ej:Grid>

<script type="text/javascript">


    function btnClick(args) {

       

       PageMethods.GetCityNameArray(keyvalue, OnSuccessGetCityNameArray);

    }

    function OnSuccessGetCityNameArray(response) {

        // here we get xml data

        var jsondata = …;// here We want to change xmltojson 

        var gridObj = $('#<%= OrdersGrid.ClientID %>').data("ejGrid");

        gridObj.dataSource(jsondata);

    }


</script>

Default.aspx.cs

[System.Web.Services.WebMethod]

  public static object GetCityNameArray(string[] cities)

        {

            var data = new NorthwndDataContext().Orders.Take(10).ToList();

            var count = new NorthwndDataContext().Orders.Count();

            var xmlString = GetXMLString(data, count);

            return xmlString;

        }

        public static string GetXMLString(IEnumerable<Order> list, int count)

        {


            var xml = new XElement("Orders", from ord in list

                                             select new XElement("Order",

                                            new XElement("OrderID", ord.OrderID),

                                            new XElement("EmployeeID", ord.EmployeeID),

                                            new XElement("CustomerID", ord.CustomerID),

                                            new XElement("Freight", ord.Freight),

                                                            new XElement("ShipName", ord.ShipName)

                                                        ), new XElement("Count", count));

            return xml.ToString();

        }
    }


Note: We need to convert xml data to proper json format to bind with ejGrid.

Regards,

Jayaprakash K.


JP Jorge Pampin November 13, 2015 02:41 PM UTC

I'ts perfect! Thanks!


PK Prasanna Kumar Viswanathan Syncfusion Team November 16, 2015 12:43 PM UTC

Hi Jorge,

We are happy that the provided solution working fine at your end.

Regards,
Prasanna Kumar N.S.V


Loader.
Up arrow icon