下拉选项 2级 联动-- 服务器端(刷新)和ajax(不刷新)

来源:互联网 发布:淘宝异地客服怎么找 编辑:程序博客网 时间:2024/06/02 07:21

1. 刷新的  (这个大家都会了,我也就放个代码做个标记把,以后回头看看)

 

(涉及到dhtml,用随时翻手册)

 简介:

2个dropdownlist ,1个省份,1个城市

当省份的dropdownlist 改变时候,将它的  AutoPostBack="True" 就可以动态刷新第二个了

(如果ajax get 方法传参是中文参数的话,可以加上  escape( a))

aspx:

 

<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server"
            onselectedindexchanged="DropDownList1_SelectedIndexChanged" AutoPostBack="True">
      
        </asp:DropDownList>
        <asp:DropDownList ID="DropDownList2" runat="server"  >
     
        </asp:DropDownList>
    </div>
    </form>
</body>

 

CS页面:

 

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string a = "select * from dbo.p_te order by id asc ";
            DataSet ds = DB.getDataSet(a);
            this.DropDownList1.DataSource = ds;
            this.DropDownList1.DataTextField = "p_name";
            this.DropDownList1.DataValueField = "p_name";
            this.DropDownList1.DataBind();

            string b = "select * from dbo.s_te  where parentid =(select top 1 id from dbo.p_te order by id asc) ";
            DataSet ds2 = DB.getDataSet(b);
            this.DropDownList2.DataSource = ds2;
            this.DropDownList2.DataTextField = "value";
            this.DropDownList2.DataValueField = "value";
            this.DropDownList2.DataBind();

        }
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string a = this.DropDownList1.SelectedValue.ToString().Trim();
        //Response.Write("<script>alert('" + a.Length+ "');</script>");
        string b = "select * from dbo.s_te  where parentid =(select top 1 id from dbo.p_te where p_name=N'"+a+"') ";
        DataSet ds2 = DB.getDataSet(b);
        this.DropDownList2.DataSource = ds2;
        this.DropDownList2.DataTextField = "value";
        this.DropDownList2.DataValueField = "value";
        this.DropDownList2.DataBind();
    }

 


 

 

2. js+ajax  实现的无刷新联动  (等有空了用jquery做,那样的话更简洁)

 

js页面:

 

           <script type="text/javascript"  language="javascript"  >

        function createxmlrequest() {
            try {
                // Firefox, Opera 8.0+, Safari
                xmlrequest = new XMLHttpRequest();

            }
            catch (e) {

                // Internet Explorer
                try {
                    xmlrequest = new ActiveXObject("Msxml2.XMLHTTP");

                }
                catch (e) {

                    try {
                        xmlrequest = new ActiveXObject("Microsoft.XMLHTTP");

                    }
                    catch (e) {
                        alert("您的浏览器不支持AJAX,请换个浏览器!");

                    }
                }

            }
            return xmlrequest;
        }

        function getdata() {

            if (xmlrequest.readyState == 4 && xmlrequest.status == 200) {


                var strtext = xmlrequest.responseText;    //获取的数据

                var abc = strtext.split(",");        //数组依次保存islocked,inventory,singinventory
                var coll = document.getElementById('DropDownList2');
                coll.length = 0;
                for (var i = 0; i < abc.length - 1; i++) {
                    coll.options.add(new Option(abc[i], abc[i]));

                }
                document.getElementById('Label1').style.display = "none";
            }
            else {
                document.getElementById('Label1').style.display = "block";
            }
          
        }
        function check() {
            var thisDate = new Date();        // ajax请求有缓存,加上时间
            xmlrequest = createxmlrequest();
            var a = document.getElementById('DropDownList1').value;
            xmlrequest.onreadystatechange = getdata;
            var strurl = "ajax_liandong.aspx?a=" +escape( a);   // 传中文参数
//            alert(strurl);
            xmlrequest.open("GET", strurl, true);
            xmlrequest.send(null);
        }


</script>

 

 

ajax_liandong.aspx 接受请求页面,返回数据:

 

protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["a"] != null && Request.QueryString["a"].ToString() != "")
        {
          
            string b = "select * from dbo.s_te  where parentid =(select top 1 id from dbo.p_te where p_name=N'" + Request.QueryString["a"] + "') ";
            DataSet ds2 = DB.getDataSet(b);
            if (ds2.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds2.Tables[0].Rows.Count; i++)
                {
                    string str = ds2.Tables[0].Rows[i]["value"].ToString() + ",";
                    Response.Write(""+str+"");
                }
            }
            else
            {
                Response.Write("'" + Request.QueryString["a"] + "'");
            }

        }
        else
        {
            Response.Write("无");
        }
       
    }

 

 

原创粉丝点击