两种方式实现省市区联动

一、aspx方式实现

1、前端

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="省市区联动.aspx.cs" Inherits="参数传递demo.省市区联动" %>






    


    
省: 市: 区:

2、后台

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
namespace 参数传递demo
{
    public partial class 省市区联动 : System.Web.UI.Page
    {
        protected StringBuilder s1 = new StringBuilder();
        protected StringBuilder s2 = new StringBuilder();
        protected void Page_Load(object sender, EventArgs e)
        {
            GetSHENG();
        }
        void GetSHENG()
        {
            string sql1 = "select provinceID,province from dbo.hat_province";
            DataTable dtsheng = DbHelperSQL.GetDataTable(sql1);
            foreach (DataRow row in dtsheng.Rows)
            {
                ListItem list = new ListItem(row["province"].ToString(), row["provinceID"].ToString());
                ddp.Items.Add(list);
            }
        }
        protected void GetShi()
        {
            string sql2 = "select cityID,city from dbo.hat_city where  father='" + ddp.Text + "'";
            DataTable dtshi = DbHelperSQL.GetDataTable(sql2);
            foreach (DataRow row in dtshi.Rows)
            {
                ListItem list = new ListItem(row["city"].ToString(), row["cityID"].ToString());
                ddp2.Items.Add(list);
            }
        }
        void GetQu()
        {
            string sql3 = "select areaID,area from dbo.hat_area where father='" + ddp2.Text + "'";
            DataTable dtqu = DbHelperSQL.GetDataTable(sql3);
            foreach (DataRow row in dtqu.Rows)
            {
                ListItem list = new ListItem(row["area"].ToString(), row["areaID"].ToString());
                ddp3.Items.Add(list);
            }
        }
        protected void ddp_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddp2.Items.Clear();
            ddp3.Items.Clear();
            GetShi();
        }
        protected void ddp2_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddp3.Items.Clear();
            GetQu();
        }
    }
}


二、html+ashx+ajax方式实现

1、前端





    
    
    


    省:
    市:
     区:



二、ashx端

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Text;
namespace 参数传递demo
{
    /// 
    /// 省市区联动AJAX 的摘要说明
    /// 
    public class 省市区联动AJAX : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            if (context.Request["state"].ToString() == "1")
            {
                StringBuilder s = new StringBuilder(1000);
                string sql1 = "select provinceID,province from dbo.hat_province";
                DataTable dtsheng = DbHelperSQL.GetDataTable(sql1);
                foreach (DataRow row in dtsheng.Rows)
                {
                    s.AppendLine("");
                }
                context.Response.Write(s);
            }
            if (context.Request["state"].ToString() == "2")
            {
                StringBuilder s = new StringBuilder(1000);
                string sql2 = "select cityID,city from dbo.hat_city where  father='" + context.Request["shiid"].ToString() + "'";
                DataTable dtshi = DbHelperSQL.GetDataTable(sql2);
                foreach (DataRow row in dtshi.Rows)
                {
                    s.AppendLine("");
                }
                context.Response.Write(s);
            }
            if (context.Request["state"].ToString() == "3")
            {
                StringBuilder s = new StringBuilder(1000);
                string sql3 = "select areaID,area from dbo.hat_area where father='" + context.Request["shiid"].ToString() + "'";
                DataTable dtqu = DbHelperSQL.GetDataTable(sql3);
                foreach (DataRow row in dtqu.Rows)
                {
                    s.AppendLine("");
                }
                context.Response.Write(s);
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}


你可能感兴趣的:(web前端)