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", "");//((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);
}
ajax调用aspx.cs中的WebMethod的更多相关文章
- jQuery Ajax 方法调用 Asp.Net WebService 以及调用aspx.cs中方法的详细例子
一.jQuery Ajax 方法调用 Asp.Net WebService (引自Terry Feng) Html文件 <!DOCTYPE html PUBLIC "-//W3C//D ...
- 用juery的ajax方法调用aspx.cs页面中的webmethod方法示例
juery的ajax调用aspx.cs页面中的webmethod方法:首先在 aspx.cs文件里建一个公开的静态方法,然后加上WebMethod属性,具体实现如下,感兴趣的朋友可以参考下哈,希望对大 ...
- 仅用aspx文件实现Ajax调用后台cs程序。(实例)
仅用aspx文件实现Ajax调用后台cs无刷新程序.(实例) 两个文件:aaa.aspx 和aaa.aspx.cs 一.aaa.aspx <script type="text/java ...
- Jquery Ajax调用aspx页面方法
Jquery Ajax调用aspx页面方法 在asp.net webform开发中,用jQuery ajax传值一般有几种玩法 1)普通玩法:通过一般处理程序ashx进行处理: 2)高级玩法:通过as ...
- Jquery Ajax调用aspx页面方法 (转载)
在asp.net webform开发中,用jQuery ajax传值一般有几种玩法 1)普通玩法:通过一般处理程序ashx进行处理: 2)高级玩法:通过aspx.cs中的静态方法+WebMethod进 ...
- jquery.ajax请求aspx和ashx的异同 Jquery Ajax调用aspx页面方法
1.jquery.ajax请求aspx 请求aspx的静态方法要注意一下问题: (1)aspx的后台方法必须静态,而且添加webmethod特性 (2)在ajax方法中contentType必须是“a ...
- 用juery的ajax方法调用aspx.cs页面中的webmethod方法
首先在 aspx.cs文件里建一个公开的静态方法,然后加上WebMethod属性. 如: [WebMethod] public static string GetUserName() { //.... ...
- 项目中Ajax调用ashx页面中的Function的实战
前台页面: 使用几个display=none的空间存储DropdownList中的值,点击Search Button后刷新页面再次给DropdownList赋值使用 <%@ Page Langu ...
- asp.net Ajax调用Aspx后台方法
Ajax调用的前提(以aspx文件为例:) 1.首先需要在aspx文件后台中引用using System.Web.Services; 2.需要调用的方法必须是公共的(public).静态的(stati ...
随机推荐
- Python Web Crawler
Python版本:3.5.2 pycharm URL Parsing¶ https://docs.python.org/3.5/library/urllib.parse.html?highlight= ...
- java中如何将字符串数组转换成字符串(转)
如果是 “字符串数组” 转 “字符串”,只能通过循环,没有其它方法 String[] str = {"abc", "bcd", "def"} ...
- Java字节流:InputStream OutputStream
字节输入流:InputStream 类声明: public abstract class InputStream implements Closeable 位于java.io包下,是一个抽象类. 官方 ...
- CF459B Pashmak and Flowers (水
Pashmak and Flowers Codeforces Round #261 (Div. 2) B. Pashmak and Flowers time limit per test 1 seco ...
- Java实验2-数据库编程
目标:掌握Java数据库编程 内容: 学生选课系统包括如下数据库表 学生表:Student(Sno,Sname,Ssex,Sage,Sdept) 课程表:Course(Cno,Cname,Ccredi ...
- Mastering Web Application Development with AngularJS 读书笔记(一)
第一章笔记 (一) 一.PS:运行时配置IIS <html> <head> <script src="angular.js"></scri ...
- 将本地的新建的web Form页面放到服务器提示错误;
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs&q ...
- JDBC、JDBCTemplate、MyBatis、Hiberante 比较与分析
JDBC (Java Data Base Connection,java数据库连接) JDBC(Java Data Base Connection,java数据库连接)是一种用于执行SQL语句的Jav ...
- 认识ATL窗口
这是一个相当于“Hello world!”的任务,作为认识ATL,考查了其运作流程与机制. 环境:VS2008 创建:新建-项目-Win32项目-添加公用头文件用于(选择ATL). PS:注意新建项目 ...
- PHP简单封装MysqlHelper类
MysqlHelper.class.php 1: <?php 2: 3: /** 4: * Mysql数据帮助类 5: */ 6: class MysqlHelper 7: { 8: func ...