Monday, March 26, 2012

How to use WCF to enable Telerik Grid Client side binding

I spend almost two days, finally i can use the WCF to retrieve the data for the rad grid control from Telerik


You must remember that Rad Grid client side databinding did not support WCF hosting on the cross domain.The WCF must running from the same domain as the applicaiton that using the rad grid.



The Html Code Snippet in the ASPX page


 <telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" GridLines="None" 
AllowSorting="True" AutoGenerateColumns="false" PageSize="10">
               <MasterTableView DataKeyNames="MyID" ClientDataKeyNames="MyID" PageSize="5" AllowPaging="true" >
                <PagerStyle Mode="NextPrevAndNumeric" />
                <Columns>
                <telerik:GridBoundColumn DataField="MyID" HeaderText="MyID" DataType="System.Int32">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="CustomerName" HeaderText="CustomerName"
                        DataType="System.String">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="Addressline1" HeaderText="Addressline1"
                        DataType="System.String">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="City" HeaderText="City"
                        DataType="System.String">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="Prov" HeaderText="Prov"
                        DataType="System.String">
                    </telerik:GridBoundColumn>
                </Columns>
                <PagerStyle Mode="NumericPages" />
            </MasterTableView>
            <ClientSettings>
                <DataBinding  SelectMethod="GetDataUsingDataContract"
            Location="~/WCF/TCCIService.svc"  SortParameterType="Linq" FilterParameterType="Linq"></DataBinding>
            </ClientSettings>
                </telerik:RadGrid>




WCF Service Code Snippet


  [ServiceKnownType(typeof(MyObject))]
    [ServiceContract]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class MyService
    {
        [OperationContract]
        public ResultData GetDataUsingDataContract(int startRowIndex, int maximumRows,  
string sortExpression, string filterExpression)
        {
 
            MyDB LeaseRetensionDBAccess = new MyDB();
            List<MyObject> tObjectList = new List<MyObject>();
            using (SqlDataReader tempDataReader = LeaseRetensionDBAccess.LoadDataTable("CRM_Portfolio_Sel"))
            {
                if (tempDataReader != null && tempDataReader.HasRows)
                {
                    while (tempDataReader.Read())
                    {
                        MyObject tObject = new MyObject();
                        for (int i = 0; i < tempDataReader.FieldCount; i++)
                        {
                            if (tempDataReader.GetValue(i) != DBNull.Value)
                            {
                                SciUtil.DataMapper.SetPropertyValue(tObject, tempDataReader.GetName(i), 
tempDataReader.GetValue(i));
                            }
                        }
                        tObjectList.Add(tObject);
                    }
                    tempDataReader.Close();
                }
            }
            ResultData result = new ResultData();
            result.Data = tObjectList.Skip<MyObject>(startRowIndex)
.Take<MyObject>(maximumRows).ToList<MyObject>();
            result.Count = tObjectList.Count;
            return result;
        }
    }
}
 
here i use linq to filter the record for the paging. 
 
tObjectList.Skip<MyObject>(startRowIndex)
.Take<MyObject>(maximumRows).ToList<MyObject>();
 
the Configuration Setting in the Web.Config file
 
 <services>
      <!-- Note: the service name must match the configuration name for the service implementation. -->
      <service name="TabWebApp.WCF.MyService" behaviorConfiguration="MyServiceTypeBehaviors" >
        <!-- Add the following endpoint.  -->
        <!-- Note: your service must have an http base address to add this endpoint. -->
        <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
        <endpoint contract="TabWebApp.WCF.MyService" binding="webHttpBinding" 
 address="" behaviorConfiguration="JsonBehavior"></endpoint>
      </service>
    </services>
   
    <behaviors>
      <endpointBehaviors>
        <behavior name="JsonBehavior">
          <enableWebScript/>
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="MyServiceTypeBehaviors" >
          <!-- Add the following element to your service behavior configuration. -->
          <serviceMetadata httpGetEnabled="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors> 
 
 
the following configuration will enable the WCF to serialize the data as JSON format.
tthis setting will fix the data serialization issue.  
       <behavior name="JsonBehavior">
          <enableWebScript/>
        </behavior> 

No comments:

Post a Comment