一、页面
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="PhoneInAdd.aspx.cs" Inherits="Web.PhoneIn.PhoneInAdd" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head id="Head1" runat="server">
<title>无</title>
<script src="../scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script type="text/javascript">
function checkInfo() {
var msg = "";
var BIndex = $('#ddlBrand option:selected').attr('index');
var MIndex = $('#ddlModel option:selected').attr('index');
if (BIndex == 0)
msg = msg + "请选择品牌/n";
if (MIndex == 0)
msg = msg + "请选择型号/n";
if (msg != "") { alert(msg); return false; }
else return true;
}
$(document).ready(function () {
$("#ddlBrand").change(function () {
AjaxFn('ddlBrand', 'ddlModel');
})
});
function AjaxFn(Pid, Cid) {
$.ajax({
type: 'post',
dataType: 'json',
url: 'PhoneInAdd.aspx',
data: 'Pid=' + $('#' + Pid).val() + '&&type=1',
beforeSend: function (data) { },
success: function (data) {Format(data,Cid);},
error: function (data, status) { if (status == 'error') { alert("系统发生错误"); } }
});
}
function Format(data, Cid) {
var value = '';
var text = '';
if (data != null) {
$('#' + Cid).empty();
$.each(data, function (i) {
if (data != null && data != "undefined" && data[i] != null && data[i] != "undefined") {
value = data[i].id;
text = data[i].name;
$('#' + Cid).append('<option value="' + value + '">' + text + '</option>');
}
});
}
else {
$('#' + Cid).empty();
$('#' + Cid).append('<option value="-1">选择型号</option>');
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
选择品牌:
<asp:DropDownList ID="ddlBrand" runat="server">
</asp:DropDownList>
选择型号:
<asp:DropDownList ID="ddlModel" runat="server">
<asp:ListItem Value="-1">选择型号</asp:ListItem>
</asp:DropDownList>
<asp:ImageButton ID="ibtnAdd" OnClientClick="return checkInfo();"
CssClass="ibutton" runat="server" ImageUrl="/images/add.png" />
</form>
</body>
</html>
二、代码
DataClassesDataContext dc = new DataClassesDataContext(SqlHelper.ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindDll_Brand();
if (Request["type"] != null && Request["type"] == "1" && Request["Pid"]!=null)
{
Response.Write(GetModelListJson(Convert.ToInt32(Request["Pid"])));
Response.End();
}
}
}
//绑定品牌
protected void BindDll_Brand()
{
this.ddlBrand.DataSource = from c in dc.BrandInfos select new { c.BrandId, c.BrandName };
this.ddlBrand.DataTextField = "BrandName";
this.ddlBrand.DataValueField = "BrandId";
this.ddlBrand.DataBind();
this.ddlBrand.Items.Insert(0, new ListItem("选择品牌", "-1"));
}
//跟据品牌获取型号列表 pid(品牌id)
protected string GetModelListJson(int pid)
{
StringBuilder json = new StringBuilder();
var list = from c in dc.ModelInfos where c.BrandId == pid select new { c.ModelId,c.ModelName };
if (list.Count() > 0)
{
int i = 0;
json.Append("[");
json.AppendFormat("{{/"id/":/"-1/",/"name/":/"选择型号/"}},");
foreach (var p in list)
{
json.AppendFormat("{{/"id/":/"{0}/",/"name/":/"{1}/"}}",p.ModelId.ToString(),p.ModelName);
if (i++ < list.Count() - 1)
json.AppendFormat(",");
}
json.Append("]");
return json.ToString();
}
else
return string.Empty;
}