jquery1.4+VS2010+linq+ajax 品牌和型号下拉列表二级联动

一、页面

 

<%@ 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;

        }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值