• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
༺҉戰҉༻
博客园    首页    新随笔    联系   管理    订阅  订阅

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;
    }


}

 

posted @ 2017-01-16 16:03  孤丷狼  阅读(185)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3