ajax调用aspx.cs中的WebMethod
前台:
<script language="javascript" src="../js/jquery-1.8.2.js"></script> <script language="javascript" src="../js/locationFilter.js"></script> <tr> <td class="fieldtitle" width="20%"> Test Filter: </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> </td> </tr>
Js文件: locationFilter.js
$(document).ready(function () { alert(1); 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: "MallListCAM.aspx/initData", url: "../Mall/MallListCAM.aspx/initData", 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) { var result = JSON.parse(data.d); $("#ddlRegion").append($("<option value='ALL'>ALL</option>")); $("#ddlSubRegion").append($("<option value='ALL'>ALL</option>")); $("#ddlGrid").append($("<option value='ALL'>ALL</option>")); $("#ddlPref").append($("<option value='ALL'>ALL</option>")); $("#ddlCity").append($("<option value='ALL'>ALL</option>")); $("#ddlCounty").append($("<option value='ALL'>ALL</option>")); $("#ddlTown").append($("<option value='ALL'>ALL</option>")); $("#ddlVillage").append($("<option value='ALL'>ALL</option>")); //clearData("region"); $.each(result, function (i, item) { $("#ddlRegion").append($("<option value='" + item.rgn_cd + "'>" + item.rgn_nm + "</option>")); }) }, error: function (err) { alert("err:" + err); } }); }) 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: "../Mall/MallListCAM.aspx/initData", 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) { var result = JSON.parse(data.d); if (type == "subregion") { $.each(result, function (i, item) { $("#ddlSubRegion").append($("<option value='" + item.subrgn_cd + "'>" + item.subrgn_nm + "</option>")); }) } else if (type == "grid") { $.each(result, function (i, item) { $("#ddlGrid").append($("<option value='" + item.grid_cd + "'>" + item.grid_nm + "</option>")); }) } else if (type == "pref") { $.each(result, function (i, item) { $("#ddlPref").append($("<option value='" + item.pref_cd + "'>" + item.pref_nm + "</option>")); }) } else if (type == "city") { $.each(result, function (i, item) { $("#ddlCity").append($("<option value='" + item.city_id + "'>" + item.name + "</option>")); }) } else if (type == "county") { $.each(result, function (i, item) { $("#ddlCounty").append($("<option value='" + item.county_city_id + "'>" + item.county_name + "</option>")); }) } else if (type == "town") { $.each(result, function (i, item) { $("#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(result, function (i, item) { $("#ddlVillage").append($("<option value='" + item.village_city_id + "'>" + item.village_city_id + "</option>")); }) } }, error: function (err) { alert("err:" + err); } }); } 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>")); } }
后台代码:
[System.Web.Services.WebMethod] public static string initData(string rgn_cd, string subrgn_cd, string grid_cd, string pref_cd, string city_id, string county_id, string town_id, string village_id, string type, string WWID) { //string rgn_cd,string subrgn_cd,string grid_cd,string pref_cd,string city_id,string county_id,string town_id,string village_id, 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");//((AccessControler)this.Session["_accessctrl"]).GetWWID()); SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = cmd; DataTable dt = new DataTable(); conn.Open(); da.Fill(dt); //this.HiddenRegion.Text = JsonConvert.SerializeObject(dt); conn.Close(); da.Dispose(); cmd.Dispose(); return JsonConvert.SerializeObject(dt); }