Click here to Skip to main content
15,908,776 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends,
How to bind the gridview through clientscript through webservices

XML
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %>

<%@ Register Assembly="AjaxDataControls" Namespace="AjaxDataControls" TagPrefix="AjaxData" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
       
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    <Services>
    <asp:ServiceReference Path="~/WebService/PlanInfo.asmx" />
    </Services>
    </asp:ScriptManager>
    <div>
        <AjaxData:GridView ID="gvPlanSheet" runat="server" DataKeyName="Name">
        <EmptyDataTemplate>
       Now Rows Available
        </EmptyDataTemplate>
        <Columns>
        <AjaxData:GridViewBoundColumn DataField="Name" HeaderText="NAME" />
        <AjaxData:GridViewBoundColumn DataField="Age" HeaderText="AGE" />
        </Columns>
        </AjaxData:GridView>
        <input id="btnRebind" type="button" value="Reset" onclick="Page_Load();" />
    </div>
    </form>
    <script type="text/javascript">
        Sys.Application.add_load(Page_Load);
        function Page_Load(sender, e) {
            PlanInfo.PlanSheetInfo(onSuccess, onFailed);
    }
    //Bind GridView OnSuccess
     function onSuccess(Result) {
         var GvPlan = $find('gvPlanSheet');
         GvPlan.set_dataSource = Result;
        GvPlan.dataBind();
    }
    //OnFailed Alert
    function onFailed(Result) {
        alert(Result);
    }
    </script>
</body>
</html>




and the WebServices is

XML
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
/// <summary>
/// Summary description for PlanInfo
/// </summary>
[ScriptService]
[WebService(Namespace = "http://tempuri.org/")]
[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 PlanInfo : System.Web.Services.WebService {

    [ScriptMethod]
    [WebMethod]
    public List<PlanSheet> PlanSheetInfo()
    {
        List<PlanSheet> PlnLst = new List<PlanSheet>();
        try
        {
            PlnLst = new List<PlanSheet>(){
            new PlanSheet(){Name="Sample1",Age="23"},
            new PlanSheet(){Name="Sample2",Age="25"},
            new PlanSheet(){Name="Sample3",Age="21"},
            new PlanSheet(){Name="Sample4",Age="24"},
            new PlanSheet(){Name="Sample5",Age="22"},
            };
        }
        catch (Exception ex) { }
        return PlnLst;
    }
}
public class PlanSheet
{
    public string Name { get; set; }
    public string Age { get; set; }
}



I'm getting the Result in
C#
var GvPlan = $find('gvPlanSheet');
        GvPlan.set_dataSource = Result;
       GvPlan.dataBind();


But the values are not displaying in the AjaxGridView Control.

Please help me to solve this problem.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900