• 省市选择期三级联动


    前台:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="省市县.aspx.cs" Inherits="省级连动.省市县" %>



    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:DropDownList ID="ddlProvince" runat="server" AutoPostBack="True" 
                onselectedindexchanged="ddlProvince_SelectedIndexChanged">
            <asp:ListItem Value="0">--省份--</asp:ListItem>
            </asp:DropDownList>
            <asp:DropDownList ID="ddlCity" runat="server" AutoPostBack="True" 
                onselectedindexchanged="ddlCity_SelectedIndexChanged">
            <asp:ListItem Value="0">--城市--</asp:ListItem>
            </asp:DropDownList>
            <asp:DropDownList ID="ddlDistrict" runat="server" AutoPostBack="True" 
                onselectedindexchanged="ddlDistrict_SelectedIndexChanged">
            <asp:ListItem Value="0">--县区--</asp:ListItem>
            </asp:DropDownList>


        </div>
        </form>
    </body>

    </html>

    后台:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data.SqlClient;
    using System.Data;
    using System.Configuration;


    namespace 省级连动
    {
        public partial class 省市县 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!this.Page.IsPostBack)
                {
                    getddlProvinceDataBind();       //页面首次加载执行省份绑定
                }
            }


            public void getddlProvinceDataBind()
            {
                string sqlProvince = "select * from S_Province";
                ddlProvince.DataSource = getDataSet(sqlProvince);
                ddlProvince.DataTextField = "ProvinceName";
                ddlProvince.DataValueField = "ProvinceID";
                ddlProvince.DataBind();
                ddlProvince.Items.Insert(0, new ListItem("--省份--", "0"));
            }


            protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e)
            {
                int ProvinceID = Convert.ToInt32(ddlProvince.SelectedValue);
                if (ProvinceID > 0)
                {
                    string sqlCity = "select * from S_City WHERE ProvinceID=" + ProvinceID + "";       //根据省份ID找城市
                    ddlCity.DataSource = getDataSet(sqlCity);
                    ddlCity.DataTextField = "CityName";
                    ddlCity.DataValueField = "CityID";
                    ddlCity.DataBind();
                    ddlCity.Items.Insert(0, new ListItem("--请选择城市--", "0"));
                }
                else
                {
                    ddlCity.Items.Clear();
                    ddlCity.Items.Insert(0, new ListItem("--请选择城市--", "0"));
                    ddlDistrict.Items.Clear();
                    ddlDistrict.Items.Insert(0, new ListItem("--请选择县区--", "0"));
                }
            }
        
            


            protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
            {
                int CityID = Convert.ToInt32(ddlCity.SelectedValue);
                if (CityID > 0)
                {
                    string sqlDistrict = "SELECT * FROM S_District WHERE CityID=" + CityID + "";       //根据城市ID找县区
                    ddlDistrict.DataSource = getDataSet(sqlDistrict);
                    ddlDistrict.DataTextField = "DistrictName";
                    ddlDistrict.DataValueField = "DistrictID";
                    ddlDistrict.DataBind();
                    ddlDistrict.Items.Insert(0, new ListItem("--请选择县区--", "0"));
                }
                else
                {
                    ddlDistrict.Items.Clear();
                    ddlDistrict.Items.Insert(0, new ListItem("--请选择县区--", "0"));
                }
            }


            protected void ddlDistrict_SelectedIndexChanged(object sender, EventArgs e)
            {
                int ProvinceID = Convert.ToInt32(ddlProvince.SelectedValue);
                int CityID = Convert.ToInt32(ddlCity.SelectedValue);
                int DistrictID = Convert.ToInt32(ddlDistrict.SelectedValue);
            }
            public DataSet getDataSet(string sql)       
            {
                string conStr = "Data Source=PC-DLL; initial catalog=CityandContury;user id=sa;password=linlin";
                SqlConnection conn = new SqlConnection(conStr);
                SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
                DataSet ds = new DataSet();
                sda.Fill(ds);
                return ds;
            }
        }
    }

  • 相关阅读:
    c# 重绘窗体 控件为不规则形状
    c# 调用C库函数
    《Unix/Linux系统编程》第1,2章学习笔记 20201209戴骏
    API微信小程序开发(六)
    项目创建及基本组成结构微信小程序开发(二)
    注册微信小程序及开发准备微信小程序开发(一)
    常见组件的使用微信小程序开发(四)
    WXML 模板语法③条件渲染微信小程序开发(九)
    宿主环境微信小程序开发(五)
    页面组成部分微信小程序开发(三)
  • 原文地址:https://www.cnblogs.com/duanlinlin/p/2954558.html
Copyright © 2020-2023  润新知