Monday, February 13, 2012

Jquery Web page method




<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.5.js" type="text/javascript"></script>
<script type="text/javascript" src="https://github.com/malsup/blockui/raw/master/jquery.blockUI.js?v2.34"></script>
<script type="text/javascript">
    $(document).ready(function() {
        $.ajax({
            type: "POST",
            contentType: "application/json",
            data: "{}",
            url: "JQuery AJAX call to page method not working.aspx/GetCountry",
            dataType: "json",
            success: function(data) {
                $(data.d).each(function(index, item) {
                    $("#country").append($("<option>").val(item.CountryID).html(item.CountryName));
                });
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                debugger;
            }
        });
        $("#country").change(function() {
            $.blockUI({ message: '<img src="http://www.clickbd.com/global/img/loading.gif" alt="loading..."/> <br /> Just a moment......' });
            $.ajax({
                type: "POST",
                contentType: "application/json",
                data: "{CountryID:" + $(this).val() + "}",
                url: "JQuery AJAX call to page method not working.aspx/GetState",
                dataType: "json",
                success: function(data) {
                    $("#State").empty();
                    $(data.d).each(function(index, item) {
                        $("#State").append($("<option>").val(item.StateID).html(item.StateName));
                    });
                    $.unblockUI();
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    debugger;
                }
            });
        });
    });
</script>
</head>
<body>
    <form id="form1" runat="server">
  <select id="country" >
  </select>
  <select id="State" ></select>
    </form>
</body>
</html>
public partial class JQuery_AJAX_call_to_page_method_not_working : System.Web.UI.Page
{
    [System.Web.Services.WebMethod]
    public static object GetCountry()
    {
        var obj = new { CountryID = 0, CountryName = "Country Name 0" };
        var objList = (new[] { obj }).ToList();
        for (int i = 1; i < 10; i++)
            objList.Add(new { CountryID = i, CountryName = "Country Name "+i.ToString() });
        return objList;
    }
    [System.Web.Services.WebMethod]
    public static object GetState(string CountryID)
    {
        var obj = new { StateID = 0, StateName = "Country "+CountryID+ " state Name 0" };
        var objList = (new[] { obj }).ToList();
        for (int i = 1; i < 10; i++)
            objList.Add(new { StateID = i, StateName = "Country " + CountryID + " state Name "+i.ToString() });
        System.Threading.Thread.Sleep(4000);//Delete this line
        return objList;
    }
}

1 comment:

  1. This comment has been removed by a blog administrator.

    ReplyDelete