This is a migrated thread and some comments may be shown as answers.

Populating RadGrid through a Webservice

1 Answer 285 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Devon
Top achievements
Rank 1
Devon asked on 18 Jan 2011, 08:00 PM
Hello,

I'm trying to populate a RadGrid with data from a WebMethod call, and I'm having no luck.  The primary example I'm trying to use (Here) doesn't tell me what type of data to return in the GetData method, and doesn't show example code for the Web Service.  Below is my example code, what is wrong with it?

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeFile="Default.aspx.cs" Inherits="_Default" %>
 
<%@ Register Assembly="Telerik.Web.UI" TagPrefix="tel" Namespace="Telerik.Web.UI" %>
 
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent" >
    <asp:Label ID="Label1" runat="server" Text="" />
    <tel:RadScriptManager runat="server"></tel:RadScriptManager>
    <tel:RadGrid ID="RadGrid1" runat="server">
        <MasterTableView>
            <Columns>
                <tel:GridBoundColumn DataField="ID" HeaderText="ID" DataType="System.Int32" />
            </Columns>
        </MasterTableView>
        <ClientSettings>
            <DataBinding Location="Services/WerbService.asmx" SelectMethod="HelloWorld2" />
        </ClientSettings>
    </tel:RadGrid>
</asp:Content>

WerbService.asmx Codebehind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using Telerik.Web.UI;
 
/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "WerbServices")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 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 WerbService : System.Web.Services.WebService {
 
    public WerbService () {
 
        //Uncomment the following line if using designed components
        //InitializeComponent();
    }
 
    [WebMethod]
    public static object HelloWorld2(int startRowIndex, int maximumRows, List<GridSortExpression> sortExpression, List<GridFilterExpression> filterExpression)
    {
        return new[] {
            new{ID = "Hello World"}
        };
    }
     
}

1 Answer, 1 is accepted

Sort by
0
Marin
Telerik team
answered on 19 Jan 2011, 04:59 PM
Hi Devon,

 To make grid bind correctly to the service you also need the total count of the items that are returned by the HelloWorld2 method. You can have a look at this demo showing also service code. Basically there are two ways to expose the correct number of items. One is by specifying a method in the SelectCountMethod property of the DataBinding that should return the number of items.

<ClientSettings>
            <DataBinding Location="WebService.asmx" SelectCountMethod="GetCount" SelectMethod="HelloWorld2" />
        </ClientSettings>

[WebMethod(EnableSession = true)]
   public List<MyBusinessObject> HelloWorld2(int startRowIndex, int maximumRows, List<GridSortExpression> sortExpression, List<GridFilterExpression> filterExpression)
   {
       return new List<MyBusinessObject>(){
           new MyBusinessObject(){ID = "Hello World"}
       };
   }
 
   public class MyBusinessObject
   {
       public string ID { get; set; }
   }
 
   [WebMethod(EnableSession = true)]
   public int GetCount()
   {
       return 1;
   }

Another way is to do it in one method returning a dictionary with keys Data and Count which hold the relevant values for the collection and its size. In this case you do not need an additional method for the SelectCountMethod property:

[WebMethod(EnableSession = true)]
    public Dictionary<string, object> HelloWorld2(int startRowIndex, int maximumRows, List<GridSortExpression> sortExpression, List<GridFilterExpression> filterExpression)
    {
        Dictionary<string, object> dictionary = new Dictionary<string, object>();
        dictionary.Add("Data", new[]{ new{ ID = 1234 }});
        dictionary.Add("Count", 1);
        return dictionary;
    
    }

Another thing that I noticed in the code is that you have commented the ScriptService attribute of the service class. This attribute is needed so the web service can be invoked through script if you are using client-side databinding as in the demo.

Full code for the samples is available for download from the site so you can have a look at the full working version of the code.

Greetings,
Marin
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
Tags
Grid
Asked by
Devon
Top achievements
Rank 1
Answers by
Marin
Telerik team
Share this question
or