<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CasMenu.aspx.cs" Inherits="_Default" %>
<!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>Porschev--前台JS(Jquery)调用后台方法 级联菜单</title>
<script src="http://www.cnblogs.com/Javascript/jquery-1.4.1.js" type="text/javascript"></script>
<script type="text/javascript">
$(function() {
$("#selectShop").change(function() { //省份下拉菜单的change事件
var params = '{str:"' + $(this).val() + '"}'; //此处参数名要注意和后台方法参数名要一致
$.ajax({
type: "POST", //提交方式
url: "List.aspx/ShowShopPro", //提交的页面/方法名
data: params, //参数(如果没有参数:null)
dataType: "text", //类型
contentType: "application/json; charset=utf-8",
beforeSend: function(XMLHttpRequest) {
$('#tipsDiv').text("正在查询...");
},
success: function(msg) {
$('#tipsDiv').text("查询成功!");
$("#selectShopPro option").each(function() {
$(this).remove(); //移除原有项
});
$("<option value='0'>===请选择===</option>").appendTo("#selectShopPro"); //添加一个默认项
$(eval("(" + msg + ")").d).appendTo("#selectShopPro"); //将返回来的项添加到下拉菜单中
},
error: function(xhr, msg, e) {
alert("error");
}
});
});
});
var i = 1;
//克隆添加
function adds() {
$("#selectShop").clone().prependTo("#tipsDiv");
$("#tipsDiv select").attr("id","select"+i+"");
alert($("#tipsDiv").html());
i++;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
Porschev--前台JS(Jquery)调用后台方法 级联菜单<br />
<br />
<select id="selectShop">
<%=strShop%>
</select>
<select id="selectShopPro" name="selCity">
<option value="0">===请选择===</option>
</select>
<div id="tipsDiv"></div>
<a href="javascript:adds()">添加</a>
</div>
</form>
</body>
</html>
CasMenu.aspx.cs 文件中
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CasMenuModels;
using CasMenuBLL;
using System.Text;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ShowPro ();
}
}
public static string strShop = string.Empty;
public static string strShopPro = string.Empty;
public string ShowPro()
{
StringBuilder str = new StringBuilder();
List<ZGWed.Model.Shop> list = new ShopBLL().GetALLItems();
//添加一个初始项
str.Append("<option value=\"");
str.Append("0");
str.Append("\">");
str.Append("===请选择===");
str.Append("</option>");
//循环追加省份下拉项
foreach (ZGWed.Model.Shop s in list)
{
str.Append("<option value=\"");
str.Append(s.ID);
str.Append("\">");
str.Append(s.ShopName);
str.Append("</option>");
}
return strShop = str.ToString();
}
[System.Web.Services.WebMethod()]
public static string ShowShopPro(string str)
{
StringBuilder strCi = new StringBuilder();
if (str == "0") //为初始项
{
strCi.Append("<option value=\"");
strCi.Append("请选择");
strCi.Append("\">");
strCi.Append("请选择");
strCi.Append("</option>");
}
else
{
List<string> scon = new List<string>();
scon.Add("shopid,"+str+",=");
List<ZGWed.Model.ShopProductInfo> list = new ShopProductInfoBLL().GetItemsByCondition(scon) ;
foreach (ZGWed.Model.ShopProductInfo c in list)
{
strCi.Append("<option value=\"");
strCi.Append(c.ID);
strCi.Append("\">");
strCi.Append(c.ShopID);
strCi.Append("</option>");
}
}
return strShopPro = strCi.ToString();
}
#endregion
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/porschev/archive/2010/10/15/5943579.aspx