码迷,mamicode.com
首页 > Web开发 > 详细

项目中Ajax调用ashx页面中的Function的实战

时间:2015-11-24 14:51:28      阅读:379      评论:0      收藏:0      [点我收藏+]

标签:

前台页面:

使用几个display=none的空间存储DropdownList中的值,点击Search Button后刷新页面再次给DropdownList赋值使用

<%@ Page Language="c#" CodeBehind="MallListCAM.aspx.cs" AutoEventWireup="True" Inherits="PRCSales_internal.Mall.MallListCAM"
    EnableEventValidation="false" %>

<%@ Implements Interface="Idunno.AntiCsrf.ISuppressCsrfCheck" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html>
<head>
    <title>MallList</title>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" content="C#">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    <link href="../css/Styles_1.css" type="text/css" rel="stylesheet">
    <script language="javascript" src="../js/jquery-1.8.2.js"></script>
    <script language="javascript" src="../js/locationFilter.js"></script>
    <script language="javascript">
        function Popup(url, width, height) {
            var left = (window.screen.width - width) / 2;
            var top = (window.screen.height - height) / 2;
            window.open(url, _blank, top= + top + ,left= + left + ,resizable=no,toolbar=no,width= + width + ,height= + height);
            //window.open(url,‘_blank‘,‘top=190,left=170,resizable=no,toolbar=no,width=‘+width+‘,height=‘+height);
            //window.showModalDialog(url,window,‘status=0;dialogWidth=‘+width+‘px;dialogHeight=‘+height+‘px;‘);
        }

        function Refresh() {
            document.Form1.btnSubmit.click();
        }

        function Remove() {

            if (confirm("Are you sure to remove selected Mall?")) {
                return true;
            }
            else {
                return false;
            }

        }

    </script>
</head>
<body ms_positioning="GridLayout">
    <form id="Form1" method="post" runat="server">
    <table cellspacing="0" cellpadding="0" width="100%">
        <tr>
            <td class="pagetitle">
                <asp:Label ID="lblTitle" Style="z-index: 101; position: relative" runat="server"></asp:Label>
            </td>
        </tr>
        <tr>
            <td>
                <table width="100%">
                    <tr>
                        <td class="fieldtitle" width="50%" colspan="2">
                            Mall information
                        </td>
                        <td class="fieldtitle" width="50%" colspan="2">
                            <font color="#993333">
                                <asp:Label ID="lblComplete" runat="server"></asp:Label></font>
                        </td>
                    </tr>
                    <tr style="height: 1px; background-color: #3366ff">
                        <td colspan="4">
                        </td>
                    </tr>
                    <tr>
                        <td class="fieldtitle" width="50%" colspan="2">
                            Mall search
                        </td>
                        <td align="right" width="50%" colspan="2">
                            <asp:Button ID="btnSubmit" runat="server" Text="Search" CssClass="fieldButton" OnClick="btnSubmit_Click">
                            </asp:Button>
                        </td>
                    </tr>
                    <tr>
                        <td class="fieldtitle" width="20%">
                            Region/SubRegion/Grid/Prefecture/City/County/Town/Village:
                        </td>
                        <td class="fieldtitle" colspan="3">
                            <asp:DropDownList ID="ddlRegion" runat="server" CssClass="fieldList" onchange="initData(‘subregion‘)">
                            </asp:DropDownList>
                            <asp:DropDownList ID="ddlSubRegion" runat="server" CssClass="fieldList" onchange="initData(‘grid‘)">
                            </asp:DropDownList>
                            <asp:DropDownList ID="ddlGrid" runat="server" CssClass="fieldList" onchange="initData(‘pref‘)">
                            </asp:DropDownList>
                            <asp:DropDownList ID="ddlPref" runat="server" CssClass="fieldList" onchange="initData(‘city‘)">
                            </asp:DropDownList>
                            <asp:DropDownList ID="ddlCity" runat="server" CssClass="fieldList" onchange="initData(‘county‘)">
                            </asp:DropDownList>
                            <asp:DropDownList ID="ddlCounty" runat="server" CssClass="fieldList" onchange="initData(‘town‘)">
                            </asp:DropDownList>
                            <asp:DropDownList ID="ddlTown" runat="server" CssClass="fieldList" onchange="initData(‘village‘)">
                            </asp:DropDownList>
                            <asp:DropDownList ID="ddlVillage" runat="server" CssClass="fieldList">
                            </asp:DropDownList>
                        </td>
                        <td>
                            <asp:TextBox ID="txtWWID" runat="server" Style="display: none"></asp:TextBox>
                            <asp:TextBox ID="txtRegion" runat="server" Style="display: none"></asp:TextBox>
                            <asp:TextBox ID="txtSubRegion" runat="server" Style="display: none"></asp:TextBox>
                            <asp:TextBox ID="txtGrid" runat="server" Style="display: none"></asp:TextBox>
                            <asp:TextBox ID="txtPref" runat="server" Style="display: none"></asp:TextBox>
                            <asp:TextBox ID="txtCity" runat="server" Style="display: none"></asp:TextBox>
                            <asp:TextBox ID="txtCounty" runat="server" Style="display: none"></asp:TextBox>
                            <asp:TextBox ID="txtTown" runat="server" Style="display: none"></asp:TextBox>
                            <asp:TextBox ID="txtVillage" runat="server" Style="display: none"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td class="fieldtitle" width="25%">
                            Type:
                        </td>
                        <td class="fieldtitle" width="25%">
                            <asp:DropDownList ID="dplType" runat="server" CssClass="fieldList" AutoPostBack="True"
                                OnSelectedIndexChanged="dplType_SelectedIndexChanged">
                            </asp:DropDownList>
                        </td>
                        <td class="fieldtitle" width="25%">
                            Branding:
                        </td>
                        <td class="fieldtitle" width="25%">
                            <asp:DropDownList ID="dplBrd" runat="server" CssClass="fieldList">
                            </asp:DropDownList>
                        </td>
                    </tr>
                    <tr style="height: 1px; background-color: #3366ff">
                        <td colspan="4">
                        </td>
                    </tr>
                    <tr>
                        <td class="fieldtitle" width="25%">
                            Mall list&nbsp;&nbsp;&nbsp;&nbsp;<asp:Label ID="lblCount" runat="server" CssClass="fieldList"></asp:Label>
                        </td>
                        <td align="right" colspan="3">
                            <asp:Button ID="btnAdd" runat="server" CssClass="fieldButton" Text="Add" Width="60px">
                            </asp:Button>&nbsp;&nbsp;&nbsp;&nbsp;<asp:Button ID="btnExport" runat="server" Visible="False"
                                Text="Export" CssClass="fieldButton" Width="60px" OnClick="btnExport_Click">
                            </asp:Button>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="4">
                            <asp:DataGrid ID="dg" Style="z-index: 101; position: relative" runat="server" Width="96%"
                                AutoGenerateColumns="False" PageSize="60" AllowPaging="True" AllowSorting="True">
                                <AlternatingItemStyle CssClass="datagridAlternating"></AlternatingItemStyle>
                                <HeaderStyle CssClass="tableHead"></HeaderStyle>
                                <Columns>
                                    <asp:BoundColumn Visible="False" DataField="mall_id"></asp:BoundColumn>
                                    <asp:BoundColumn DataField="grid_cd" HeaderText="Grid" SortExpression="grid_cd">
                                        <ItemStyle HorizontalAlign="Center" Width="5%"></ItemStyle>
                                    </asp:BoundColumn>
                                    <asp:BoundColumn DataField="pref_nm" HeaderText="Prefecture" SortExpression="pref_nm">
                                        <ItemStyle HorizontalAlign="Center" Width="7%"></ItemStyle>
                                    </asp:BoundColumn>
                                    <asp:BoundColumn DataField="city_std_loc_nm" HeaderText="City" SortExpression="city_std_loc_nm">
                                        <ItemStyle HorizontalAlign="Center" Width="7%"></ItemStyle>
                                    </asp:BoundColumn>
                                    <asp:BoundColumn DataField="county_nm" HeaderText="County Name" SortExpression="county_nm">
                                        <ItemStyle HorizontalAlign="Center" Width="7%"></ItemStyle>
                                    </asp:BoundColumn>
                                    <asp:BoundColumn DataField="town_nm" HeaderText="Town Name" SortExpression="town_nm">
                                        <ItemStyle HorizontalAlign="Center" Width="7%"></ItemStyle>
                                    </asp:BoundColumn>
                                    <asp:BoundColumn DataField="village_nm" HeaderText="Village Name" SortExpression="village_nm">
                                        <ItemStyle HorizontalAlign="Center" Width="7%"></ItemStyle>
                                    </asp:BoundColumn>
                                    <asp:BoundColumn DataField="mall_type_nm" HeaderText="Mall Type" SortExpression="mall_type_nm">
                                        <ItemStyle HorizontalAlign="Center" Width="5%"></ItemStyle>
                                    </asp:BoundColumn>
                                    <asp:BoundColumn DataField="mall_brnd_nm" HeaderText="Mall Branding" SortExpression="mall_brnd_nm">
                                        <ItemStyle HorizontalAlign="Center" Width="5%"></ItemStyle>
                                    </asp:BoundColumn>
                                    <asp:BoundColumn DataField="mall_nm" HeaderText="Mall Name" SortExpression="mall_nm">
                                        <ItemStyle HorizontalAlign="Left" Width="23%"></ItemStyle>
                                    </asp:BoundColumn>
                                    <asp:BoundColumn DataField="mall_addr" HeaderText="Address" SortExpression="mall_addr">
                                        <ItemStyle HorizontalAlign="Left" Width="23%"></ItemStyle>
                                    </asp:BoundColumn>
                                    <asp:BoundColumn DataField="last_upd_dtm" HeaderText="Last Updated at" DataFormatString="{0:M/d/yyyy}"
                                        SortExpression="last_upd_dtm">
                                        <ItemStyle HorizontalAlign="Center" Width="10%"></ItemStyle>
                                    </asp:BoundColumn>
                                    <asp:BoundColumn DataField="last_upd_usr_nm" HeaderText="Last Updated by" SortExpression="last_upd_usr_nm">
                                        <ItemStyle HorizontalAlign="Center" Width="5%"></ItemStyle>
                                    </asp:BoundColumn>
                                    <asp:TemplateColumn HeaderText="Action">
                                        <ItemStyle HorizontalAlign="Center" Width="5%"></ItemStyle>
                                        <ItemTemplate>
                                            <a href="javascript:Popup(‘MallProfile.aspx?action=edit&mall_id=<%# DataBinder.Eval(Container, "DataItem.mall_id") %>‘,‘850‘,‘600‘);void(0);">
                                                Edit</a>
                                        </ItemTemplate>
                                    </asp:TemplateColumn>
                                    <asp:TemplateColumn HeaderText="">
                                        <ItemStyle HorizontalAlign="Center" Width="5%"></ItemStyle>
                                        <ItemTemplate>
                                            <asp:LinkButton ID="btn_Delete" CssClass="Command" Text=‘Delete‘ CommandName="delete"
                                                CommandArgument=‘<%# DataBinder.Eval(Container.DataItem, "mall_id") %>‘ runat="server">
                                            </asp:LinkButton>
                                        </ItemTemplate>
                                    </asp:TemplateColumn>
                                </Columns>
                                <PagerStyle Position="TopAndBottom" Mode="NumericPages"></PagerStyle>
                            </asp:DataGrid>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
    </form>
</body>
</html>

JS代码:

注意在Ajax中调用ashx页面中的function的方式。

$(document).ready(function () {
    var Rgn_cd = "";
    var Subrgn_cd = "";
    var Grid_cd = "";
    var Pref_cd = "";
    var City_id = "";
    var County_id = "";
    var Town_id = "";
    var Village_id = "";
    var Type = "region";
    var WWID = $("#txtWWID").val();

    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        url: "../LocationFilter.ashx",
        data: "{rgn_cd:‘" + Rgn_cd + "‘, subrgn_cd:‘" + Subrgn_cd + "‘,grid_cd:‘" + Grid_cd + "‘,pref_cd:‘" + Pref_cd + "‘,city_id:‘" + City_id + "‘,county_id:‘" + County_id + "‘,town_id:‘" + Town_id + "‘,village_id:‘" + Village_id + "‘,type:‘" + Type + "‘,WWID:‘" + WWID + "‘}",
        success: function (data) {
            $("#ddlRegion").append($("<option value=‘ALL‘>ALL</option>"));
            $.each(data, function (i, item) {
                $("#ddlRegion").append($("<option value=‘" + item.rgn_cd + "‘>" + item.rgn_nm + "</option>"));
            })
            //alert($("#txtRegion").val());
            $("#ddlRegion option[value=‘" + $("#txtRegion").val() + "‘]").attr("selected", true);
        },
        error: function (err) {
            //alert("请等待Filter数据加载完毕再进行查询!");
        }
    });

    updateData("subregion");
    updateData("grid");
    updateData("pref");
    updateData("city");
    updateData("county");
})



function initData(type) {
    if (type == "subregion") {
        clearData("subregion");
    }
    else if (type == "grid") {
        clearData("grid");
    }
    else if (type == "pref") {
        clearData("pref");
    }
    else if (type == "city") {
        clearData("city");
    }
    else if (type == "county") {
        clearData("county");
    }
    else if (type == "town") {
        clearData("town");
    }

    var Rgn_cd = $("#ddlRegion").val();
    var Subrgn_cd = $("#ddlSubRegion").val();
    var Grid_cd = $("#ddlGrid").val();
    var Pref_cd = $("#ddlPref").val();
    var City_id = $("#ddlCity").val();
    var County_id = $("#ddlCounty").val();
    var Town_id = $("#ddlTown").val();
    var Village_id = $("#ddlVillage").val();
    var Type = type;
    var WWID = $("#txtWWID").val();

    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        url: "../LocationFilter.ashx",
        data: "{rgn_cd:‘" + Rgn_cd + "‘, subrgn_cd:‘" + Subrgn_cd + "‘,grid_cd:‘" + Grid_cd + "‘,pref_cd:‘" + Pref_cd + "‘,city_id:‘" + City_id + "‘,county_id:‘" + County_id + "‘,town_id:‘" + Town_id + "‘,village_id:‘" + Village_id + "‘,type:‘" + Type + "‘,WWID:‘" + WWID + "‘}",
        success: function (data) {
            if (type == "subregion") {
                $("#txtRegion").val(Rgn_cd);
                updateData("grid");
                updateData("pref");
                updateData("city");
                updateData("county");
                $.each(data, function (i, item) {
                    $("#ddlSubRegion").append($("<option value=‘" + item.subrgn_cd + "‘>" + item.subrgn_nm + "</option>"));
                })
            }
            else if (type == "grid") {
                $("#txtSubRegion").val(Subrgn_cd);
                updateData("pref");
                updateData("city");
                updateData("county");
                $.each(data, function (i, item) {
                    $("#ddlGrid").append($("<option value=‘" + item.grid_cd + "‘>" + item.grid_nm + "</option>"));
                })
            }
            else if (type == "pref") {
                $("#txtGrid").val(Grid_cd);
                updateData("city");
                updateData("county");
                $.each(data, function (i, item) {
                    $("#ddlPref").append($("<option value=‘" + item.pref_cd + "‘>" + item.pref_nm + "</option>"));
                })
            }
            else if (type == "city") {
                $("#txtPref").val(Pref_cd);
                updateData("county");
                $.each(data, function (i, item) {
                    $("#ddlCity").append($("<option value=‘" + item.city_id + "‘>" + item.name + "</option>"));
                })
            }
            else if (type == "county") {
                $("#txtCity").val(City_id);
                updateData("town");
                updateData("village");
                $.each(data, function (i, item) {
                    $("#ddlCounty").append($("<option value=‘" + item.county_city_id + "‘>" + item.county_name + "</option>"));
                })
            }
            else if (type == "town") {
                $("#txtCounty").val(County_id);
                $.each(data, function (i, item) {
                    $("#ddlTown").append($("<option value=‘" + item.town_city_id + "‘>" + item.town_name + "</option>"));
                })
            }
            else if (type == "village") {
                $("#txtTown").val(Town_id);
                updateData("village");
                $("#ddlVillage").empty();
                $("#ddlVillage").append($("<option value=‘ALL‘>ALL</option>"));
                $.each(data, function (i, item) {
                    $("#ddlVillage").append($("<option value=‘" + item.village_city_id + "‘>" + item.village_city_id + "</option>"));
                })
            }
        },
        error: function (err) {
            //alert("err:" + err);
            //alert("请等待Filter数据加载完毕再进行查询!");
        }
    });
}

function updateData(type) {
    if (type == "subregion") {
        clearData("subregion");
    }
    else if (type == "grid") {
        clearData("grid");
    }
    else if (type == "pref") {
        clearData("pref");
    }
    else if (type == "city") {
        clearData("city");
    }
    else if (type == "county") {
        clearData("county");
    }
    else if (type == "town") {
        clearData("town");
    }


    var Rgn_cd = $("#ddlRegion").val();
    var Subrgn_cd = $("#ddlSubRegion").val();
    var Grid_cd = $("#ddlGrid").val();
    var Pref_cd = $("#ddlPref").val();
    var City_id = $("#ddlCity").val();
    var County_id = $("#ddlCounty").val();
    var Town_id = $("#ddlTown").val();
    var Village_id = $("#ddlVillage").val();
    var Type = type;
    var WWID = $("#txtWWID").val();


    if (type == "subregion" && $("#txtRegion").val() != "") {
        Rgn_cd = $("#txtRegion").val();
    }
    if (type == "grid" && $("#txtSubRegion").val() != "") {
        Subrgn_cd = $("#txtSubRegion").val();
    }
    if (type == "pref" && $("#txtGrid").val() != "") {
        Grid_cd = $("#txtGrid").val();
    }
    if (type == "city" && $("#txtPref").val() != "") {
        Pref_cd = $("#txtPref").val();
    }
    if (type == "county" && $("#txtCity").val() != "") {
        City_id = $("#txtCity").val();
    }  

    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        url: "../LocationFilter.ashx",
        data: "{rgn_cd:‘" + Rgn_cd + "‘, subrgn_cd:‘" + Subrgn_cd + "‘,grid_cd:‘" + Grid_cd + "‘,pref_cd:‘" + Pref_cd + "‘,city_id:‘" + City_id + "‘,county_id:‘" + County_id + "‘,town_id:‘" + Town_id + "‘,village_id:‘" + Village_id + "‘,type:‘" + Type + "‘,WWID:‘" + WWID + "‘}",
        success: function (data) {
            if (type == "subregion") {
                $.each(data, function (i, item) {
                    if (item.subrgn_cd == $("#txtSubRegion").val()) {
                        $("#ddlSubRegion").append($("<option value=‘" + item.subrgn_cd + "‘ selected = ‘selected‘>" + item.subrgn_nm + "</option>"));
                    }
                    else {
                        $("#ddlSubRegion").append($("<option value=‘" + item.subrgn_cd + "‘>" + item.subrgn_nm + "</option>"));
                    }
                    //$("#ddlSubRegion").append($("<option value=‘" + item.subrgn_cd + "‘>" + item.subrgn_nm + "</option>"));
                })
            }
            else if (type == "grid") {
                $.each(data, function (i, item) {
                    if (item.grid_cd == $("#txtGrid").val()) {
                        $("#ddlGrid").append($("<option value=‘" + item.grid_cd + "‘ selected = ‘selected‘>" + item.grid_nm + "</option>"));
                    }
                    else {
                        $("#ddlGrid").append($("<option value=‘" + item.grid_cd + "‘>" + item.grid_nm + "</option>"));
                    }
                    //$("#ddlGrid").append($("<option value=‘" + item.grid_cd + "‘>" + item.grid_nm + "</option>"));
                })
            }
            else if (type == "pref") {
                $.each(data, function (i, item) {
                    if (item.pref_cd == $("#txtPref").val()) {
                        $("#ddlPref").append($("<option value=‘" + item.pref_cd + "‘ selected = ‘selected‘>" + item.pref_nm + "</option>"));
                    }
                    else {
                        $("#ddlPref").append($("<option value=‘" + item.pref_cd + "‘>" + item.pref_nm + "</option>"));
                    }
                    //$("#ddlPref").append($("<option value=‘" + item.pref_cd + "‘>" + item.pref_nm + "</option>"));
                })
            }
            else if (type == "city") {
                $.each(data, function (i, item) {
                    if (item.city_id == $("#txtCity").val()) {
                        $("#ddlCity").append($("<option value=‘" + item.city_id + "‘ selected = ‘selected‘>" + item.name + "</option>"));
                    }
                    else {
                        $("#ddlCity").append($("<option value=‘" + item.city_id + "‘>" + item.name + "</option>"));
                    }
                    //$("#ddlCity").append($("<option value=‘" + item.city_id + "‘>" + item.name + "</option>"));
                })
            }
            else if (type == "county") {
                $.each(data, function (i, item) {
                    if (item.county_city_id == $("#txtCounty").val()) {
                        $("#ddlCounty").append($("<option value=‘" + item.county_city_id + "‘ selected = ‘selected‘>" + item.county_name + "</option>"));
                    }
                    else {
                        $("#ddlCounty").append($("<option value=‘" + item.county_city_id + "‘>" + item.county_name + "</option>"));
                    }
                    //$("#ddlCounty").append($("<option value=‘" + item.county_city_id + "‘>" + item.county_name + "</option>"));
                })
            }
            else if (type == "town") {
                $.each(data, function (i, item) {
                    if (item.town_city_id == $("#txtTown").val()) {
                        $("#ddlTown").append($("<option value=‘" + item.town_city_id + "‘ selected = ‘selected‘>" + item.town_name + "</option>"));
                    }
                    else {
                        $("#ddlTown").append($("<option value=‘" + item.town_city_id + "‘>" + item.town_name + "</option>"));
                    }
                    //$("#ddlTown").append($("<option value=‘" + item.town_city_id + "‘>" + item.town_name + "</option>"));
                })
            }
            else if (type == "village") {
                $("#ddlVillage").empty();
                $("#ddlVillage").append($("<option value=‘ALL‘>ALL</option>"));
                $.each(data, function (i, item) {
                    if (item.village_city_id == $("#txtVillage").val()) {
                        $("#ddlVillage").append($("<option value=‘" + item.village_city_id + "‘ selected = ‘selected‘>" + item.village_city_id + "</option>"));
                    }
                    else {
                        $("#ddlVillage").append($("<option value=‘" + item.village_city_id + "‘>" + item.village_city_id + "</option>"));
                    }
                    //$("#ddlVillage").append($("<option value=‘" + item.village_city_id + "‘>" + item.village_city_id + "</option>"));
                })
            }
        },
        error: function (err) {
            //alert("err:" + err);
            //alert("请等待Filter数据加载完毕再进行查询!");
        }
    });
}



function clearData(type) {
    if (type == "subregion") {
        $("#ddlSubRegion").empty();
        $("#ddlSubRegion").append($("<option value=‘ALL‘>ALL</option>"));
        $("#ddlGrid").empty();
        $("#ddlGrid").append($("<option value=‘ALL‘>ALL</option>"));
        $("#ddlPref").empty();
        $("#ddlPref").append($("<option value=‘ALL‘>ALL</option>"));
        $("#ddlCity").empty();
        $("#ddlCity").append($("<option value=‘ALL‘>ALL</option>"));
        $("#ddlCounty").empty();
        $("#ddlCounty").append($("<option value=‘ALL‘>ALL</option>"));
        $("#ddlTown").empty();
        $("#ddlTown").append($("<option value=‘ALL‘>ALL</option>"));
        $("#ddlVillage").empty();
        $("#ddlVillage").append($("<option value=‘ALL‘>ALL</option>"));
    }
    else if (type == "grid") {
        $("#ddlGrid").empty();
        $("#ddlGrid").append($("<option value=‘ALL‘>ALL</option>"));
        $("#ddlPref").empty();
        $("#ddlPref").append($("<option value=‘ALL‘>ALL</option>"));
        $("#ddlCity").empty();
        $("#ddlCity").append($("<option value=‘ALL‘>ALL</option>"));
        $("#ddlCounty").empty();
        $("#ddlCounty").append($("<option value=‘ALL‘>ALL</option>"));
        $("#ddlTown").empty();
        $("#ddlTown").append($("<option value=‘ALL‘>ALL</option>"));
        $("#ddlVillage").empty();
        $("#ddlVillage").append($("<option value=‘ALL‘>ALL</option>"));
    }
    else if (type == "pref") {
        $("#ddlPref").empty();
        $("#ddlPref").append($("<option value=‘ALL‘>ALL</option>"));
        $("#ddlCity").empty();
        $("#ddlCity").append($("<option value=‘ALL‘>ALL</option>"));
        $("#ddlCounty").empty();
        $("#ddlCounty").append($("<option value=‘ALL‘>ALL</option>"));
        $("#ddlTown").empty();
        $("#ddlTown").append($("<option value=‘ALL‘>ALL</option>"));
        $("#ddlVillage").empty();
        $("#ddlVillage").append($("<option value=‘ALL‘>ALL</option>"));
    }
    else if (type == "city") {
        $("#ddlCity").empty();
        $("#ddlCity").append($("<option value=‘ALL‘>ALL</option>"));
        $("#ddlCounty").empty();
        $("#ddlCounty").append($("<option value=‘ALL‘>ALL</option>"));
        $("#ddlTown").empty();
        $("#ddlTown").append($("<option value=‘ALL‘>ALL</option>"));
        $("#ddlVillage").empty();
        $("#ddlVillage").append($("<option value=‘ALL‘>ALL</option>"));
    }
    else if (type == "county") {
        $("#ddlCounty").empty();
        $("#ddlCounty").append($("<option value=‘ALL‘>ALL</option>"));
        $("#ddlTown").empty();
        $("#ddlTown").append($("<option value=‘ALL‘>ALL</option>"));
        $("#ddlVillage").empty();
        $("#ddlVillage").append($("<option value=‘ALL‘>ALL</option>"));
    }
    else if (type == "town") {
        $("#ddlTown").empty();
        $("#ddlTown").append($("<option value=‘ALL‘>ALL</option>"));
        $("#ddlVillage").empty();
        $("#ddlVillage").append($("<option value=‘ALL‘>ALL</option>"));
    }
} 

ashx页面代码:

由于前面JS中Get的为Json数据,在ProcessRequest中设置:context.Response.ContentType = "text/json";

using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using Intel.PRCSMS.Common;

using Newtonsoft.Json;
using System.IO;
using System.Web.Script.Serialization;
using System.Collections.Generic;


namespace PRCSales_internal
{
    /// <summary>
    /// Summary description for LocationFilter
    /// </summary>
    public class LocationFilter : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {

            HttpResponse res = context.Response;
            res.ContentType = "text/json";

            Dictionary<String, Object> dicParameter = GetParameter(context);
            string rgn_cd = dicParameter["rgn_cd"].ToString();
            string subrgn_cd = dicParameter["subrgn_cd"].ToString();
            string grid_cd = dicParameter["grid_cd"].ToString();
            string pref_cd = dicParameter["pref_cd"].ToString();
            string city_id = dicParameter["city_id"].ToString();
            string county_id = dicParameter["county_id"].ToString();
            string town_id = dicParameter["town_id"].ToString();
            string village_id = dicParameter["village_id"].ToString();
            string type = dicParameter["type"].ToString();
            string WWID = dicParameter["WWID"].ToString();

            SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["DSN"].DecodeBase64());

            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "region_subregion_grid_pref_city_county_town_village_by_wwid";
            cmd.Connection = conn;

            if (string.IsNullOrEmpty(rgn_cd) || rgn_cd == "null" || rgn_cd == "ALL")
            {
                cmd.Parameters.AddWithValue("@rgn_cd", DBNull.Value);
            }
            else
            {
                cmd.Parameters.AddWithValue("@rgn_cd", rgn_cd);
            }

            if (string.IsNullOrEmpty(subrgn_cd) || subrgn_cd == "null" || subrgn_cd == "ALL")
            {
                cmd.Parameters.AddWithValue("@subrgn_cd", DBNull.Value);
            }
            else
            {
                cmd.Parameters.AddWithValue("@subrgn_cd", subrgn_cd);
            }

            if (string.IsNullOrEmpty(grid_cd) || grid_cd == "null" || grid_cd == "ALL")
            {
                cmd.Parameters.AddWithValue("@grid_cd", DBNull.Value);
            }
            else
            {
                cmd.Parameters.AddWithValue("@grid_cd", grid_cd);
            }

            if (string.IsNullOrEmpty(pref_cd) || pref_cd == "null" || pref_cd == "ALL")
            {
                cmd.Parameters.AddWithValue("@pref_cd", DBNull.Value);
            }
            else
            {
                cmd.Parameters.AddWithValue("@pref_cd", pref_cd);
            }

            if (string.IsNullOrEmpty(city_id) || city_id == "null" || city_id == "ALL")
            {
                cmd.Parameters.AddWithValue("@city_id", DBNull.Value);
            }
            else
            {
                cmd.Parameters.AddWithValue("@city_id", city_id);
            }

            if (string.IsNullOrEmpty(county_id) || county_id == "null" || county_id == "ALL")
            {
                cmd.Parameters.AddWithValue("@county_id", DBNull.Value);
            }
            else
            {
                cmd.Parameters.AddWithValue("@county_id", city_id);
            }

            if (string.IsNullOrEmpty(town_id) || town_id == "null" || town_id == "ALL")
            {
                cmd.Parameters.AddWithValue("@town_id", DBNull.Value);
            }
            else
            {
                cmd.Parameters.AddWithValue("@town_id", town_id);
            }

            if (string.IsNullOrEmpty(village_id) || village_id == "null" || village_id == "ALL")
            {
                cmd.Parameters.AddWithValue("@village_id", DBNull.Value);
            }
            else
            {
                cmd.Parameters.AddWithValue("@village_id", village_id);
            }
            cmd.Parameters.AddWithValue("@type", type);
            cmd.Parameters.AddWithValue("@wwid", "10651923");// WWID 

            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = cmd;

            DataTable dt = new DataTable();

            conn.Open();
            da.Fill(dt); 

            conn.Close();
            da.Dispose();
            cmd.Dispose();
              
            res.Write(JsonConvert.SerializeObject(dt).ToString());            
            res.End(); 
            
        }

        private Dictionary<String, Object> GetParameter(HttpContext context)
        {
            StreamReader reader = new StreamReader(context.Request.InputStream); 
            String strJson = HttpUtility.UrlDecode(reader.ReadToEnd());
            JavaScriptSerializer jss = new JavaScriptSerializer(); 
            Dictionary<String, Object> dicParameter = jss.Deserialize<Dictionary<String, Object>>(strJson);
            return dicParameter;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

 

项目中Ajax调用ashx页面中的Function的实战

标签:

原文地址:http://www.cnblogs.com/eric-qin/p/4991455.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!