Articles in this section
Category / Section

How to trigger server events by using PageMethods mechanism of the ASP.NET?

1 min read

In the ASP wrapper, though the Grid has in-built support for server-side events for certain grid actions, you may have to trigger a server-side event externally as per the requirement.

Solution

You can trigger a server-side event by using PageMethods mechanism of the ASP.NET application. The PageMethods acts similar to that of the AJAX call. Refer to the following link for more details on the PageMethods:

http://aspalliance.com/1922_PageMethods_In_ASPNET_AJAX.1

 

In the following example, the DataSource is bound to the Grid by using the PageMethods in Load client-side event of the Grid.

Render the Grid.

 

ASPX

<ej:Grid ID="OrdersGrid" runat="server" AllowPaging="True">        
        <ClientSideEvents Load="load" />            
        <Columns>
            <ej:Column Field="OrderID" HeaderText="Order ID" TextAlign="Right" Width="100"/>
            <ej:Column Field="CustomerID" HeaderText="Customer ID" Width="100"/>
            <ej:Column Field="EmployeeID" HeaderText="Employee ID" Width="100"/>
            <ej:Column Field="Freight" Format="{0:C2}" Width="100"/>
            <ej:Column Field="ShipCountry" HeaderText="Country" Width="100"/>
        </Columns>
    </ej:Grid>

 

JS

<script type="text/javascript">
        function load(args)
        {
            //PageMethods object to call the WebMethod of the page.
            PageMethods.GetPageMethod(onSucceed, onError);
            return false;
        }
        function onSucceed(result) {
            //Instance of the Grid.
            var gridObj = $('#<%= FlatGrid.ClientID %>').data("ejGrid");
            //Assigns DataSource to the Grid.
            gridObj.dataSource(result);        }
   </script>

 

The PageMethods mechanism is enabled as given in the following code example.

In Site.Master
<%--Enables PagerMethods--%>
<asp:ScriptManager runat="server" EnablePageMethods="true">
</asp:ScriptManager>

 

At the server side, the DataSource for the Grid is returned.

ASPX.CS

[System.Web.Services.WebMethod]
        public static IEnumerable GetPageMethod()        {           
            return OrderRepository.GetAllrecords().ToList();         }

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied