zoukankan      html  css  js  c++  java
  • Ajax 三级联动

    主页面

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <script src="js/jquery-1.7.2.min.js"></script>
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
    
                <asp:DropDownList ID="DropDownList1" runat="server">
                    <asp:ListItem Value="0">加载中...</asp:ListItem>
                </asp:DropDownList>
                <asp:DropDownList ID="DropDownList2" runat="server">
                    <asp:ListItem Value="0">加载中...</asp:ListItem>
                </asp:DropDownList>
                <asp:DropDownList ID="DropDownList3" runat="server">
                    <asp:ListItem Value="0">加载中...</asp:ListItem>
                </asp:DropDownList>
    
    
            </div>
        </form>
    </body>
    </html>
    <script type="text/javascript">
    
        DataBind(1);
    
        function DataBind(number) {
            var pcode = "";
            var drop;
            if (number == 1) {
                pcode = "0001";
                drop = document.getElementById("DropDownList1");
            }
            else if (number == 2) {
                pcode = document.getElementById("DropDownList1").value;
                drop = document.getElementById("DropDownList2");
                document.getElementById("DropDownList2").innerHTML = "<option value="0">加载中...</option>";
                document.getElementById("DropDownList3").innerHTML = "<option value="0">加载中...</option>";
            }
            else if (number == 3) {
                pcode = document.getElementById("DropDownList2").value;
                drop = document.getElementById("DropDownList3");
                document.getElementById("DropDownList3").innerHTML = "<option value="0">加载中...</option>";
            }
            else {
                return;
            }
    
            $.ajax({
                url: "ajax/China.ashx",
                data: { "pc": pcode },
                type: "post",
                dataType: "json",
                success: function (data) {
                    drop.innerHTML = "";
                    for (i in data) {
                        var op = document.createElement("option");
                        op.value = data[i].acode;
                        op.innerHTML = data[i].aname;
    
                        drop.appendChild(op);
                    }
                    number++;
                    DataBind(number);
                }
            });
        }
    
        document.getElementById("DropDownList1").onchange = function () {
            DataBind(2);
        };
        document.getElementById("DropDownList2").onchange = function () {
            DataBind(3);
        };
    
    
    </script>
    <%@ WebHandler Language="C#" Class="China" %>
    
    using System;
    using System.Web;
    using System.Collections.Generic;
    using System.Text;
    
    public class China : IHttpHandler
    {
    
        public void ProcessRequest(HttpContext context)
        {
            
            //string end = "";
            StringBuilder end = new StringBuilder();
    
            // end += "[";
            end.Append("[");
    
            string pcode = context.Request["pc"];
    
            List<ChinaStates> clist = new ChinaData().Select(pcode);
    
            int count = 0;
            foreach (ChinaStates c in clist)
            {
                if (count > 0)
                {
                    end.Append(",");
                }
    
                end.Append("{"acode":"" + c.AreaCode + "","aname":"" + c.AreaName + "","pcode":"" + c.ParentAreaCode + ""}");
                count++;
            }
    
    
            end.Append("]");
    
            context.Response.Write(end);
            context.Response.End();
        }
    
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Data.SqlClient;
    
    /// <summary>
    /// ChinaData 的摘要说明
    /// </summary>
    public class ChinaData
    {
        SqlConnection conn = null;
        SqlCommand cmd = null;
    
        public ChinaData()
        {
            conn = new SqlConnection("server=.;database=mydb;user=sa;pwd=123;");
            cmd = conn.CreateCommand();
        }
    
        public List<ChinaStates> Select(string pcode)
        {
            List<ChinaStates> clist = new List<ChinaStates>();
    
            cmd.CommandText = "select *from ChinaStates where ParentAreaCode=@a";
            cmd.Parameters.Clear();
            cmd.Parameters.Add("@a", pcode);
    
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                ChinaStates c = new ChinaStates();
                c.AreaCode = dr[0].ToString();
                c.AreaName = dr[1].ToString();
                c.ParentAreaCode = dr[2].ToString();
    
                clist.Add(c);
            }
            conn.Close();
    
            return clist;
        }
    
        public List<ChinaStates> SelectChina(string areaName)
        {
            List<ChinaStates> clist = new List<ChinaStates>();
    
            cmd.CommandText = "select top 10 *from ChinaStates where AreaName like @a";
            cmd.Parameters.Clear();
            cmd.Parameters.Add("@a", areaName + "%");
    
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                ChinaStates c = new ChinaStates();
                c.AreaCode = dr[0].ToString();
                c.AreaName = dr[1].ToString();
                c.ParentAreaCode = dr[2].ToString();
    
                clist.Add(c);
            }
            conn.Close();
    
            return clist;
        }
    
    
    }
  • 相关阅读:
    常见英语单词后缀
    vim手册
    笔记《鸟哥的Linux私房菜》9 档案与文件系统的压缩与打包
    笔记《鸟哥的Linux私房菜》8 Linux 磁盘与文件系统管理
    Centos 搭建 NFS
    Python Unicode编码方式
    Centos 安装 OpenCV
    Centos 安装 Python Image PIL
    Linux 文件打乱顺序
    CentOS 安装ffmpeg
  • 原文地址:https://www.cnblogs.com/shadow-wolf/p/6290010.html
Copyright © 2011-2022 走看看