zoukankan      html  css  js  c++  java
  • 省级三连动(二)

    前台:

    <div>
          <asp:DropDownList ID="DropDownList1" runat="server" Height="29px" 
                Width="150px" 
                onselectedindexchanged="DropDownList1_SelectedIndexChanged1" 
                AutoPostBack="True">
                <asp:ListItem Value="0">----请选择省份----</asp:ListItem>
            </asp:DropDownList>
    &nbsp;
            <asp:DropDownList ID="DropDownList2" runat="server" Height="29px" Width="135px" 
                AutoPostBack="True" onselectedindexchanged="DropDownList2_SelectedIndexChanged">
                <asp:ListItem Value="0">----请选择市----</asp:ListItem>
            </asp:DropDownList>
        &nbsp;&nbsp;
            <asp:DropDownList ID="DropDownList3" runat="server" Height="29px" Width="135px">
                <asp:ListItem Value="0">----请选县----</asp:ListItem>
            </asp:DropDownList>
        </div>
    后台:
    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;

    namespace 省级连动
    {
        public partial class WebForm1 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    FillDownList();
                }
            }
            private void FillDownList()
            {
                string connstr = "Data Source=PC-DLL; initial catalog=CityandContury;user id=sa;password=linlin";
                using (SqlConnection conn = new SqlConnection(connstr))
                {
                    conn.Open();
                    using (SqlCommand cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = "select * from S_Province";

                        using (SqlDataReader reader = cmd.ExecuteReader())
                        {
                            this.DropDownList1.DataSource = reader;
                            this.DropDownList1.DataTextField = "ProvinceName";
                            this.DropDownList1.DataValueField = "ProvinceID";
                            this.DropDownList1.DataBind();
                        }
                    }

                }
            }

            protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
            {
                string connstr = "Data Source=PC-DLL; initial catalog=CityandContury;user id=sa;password=linlin";
                using (SqlConnection conn = new SqlConnection(connstr))
                {
                    conn.Open();
                    using (SqlCommand cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = "select CityID,CityName from S_City where ProvinceID=@xxx";
                        SqlParameter paran = new SqlParameter("xxx", this.DropDownList1.SelectedValue);
                        cmd.Parameters.Add(paran);
                        using (SqlDataReader reader = cmd.ExecuteReader())
                        {
                            this.DropDownList2.DataSource = reader;
                            this.DropDownList2.DataTextField = "CityName";
                            this.DropDownList2.DataValueField = "CityID";
                            this.DropDownList2.DataBind();
                        }
                    }
                }
            }

            protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
            {
                string connstr = "Data Source=PC-DLL; initial catalog=CityandContury;user id=sa;password=linlin";
                using (SqlConnection conn = new SqlConnection(connstr))
                {
                    conn.Open();
                    using (SqlCommand cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = "select DistrictID,DistrictName from S_District where CityID=@xxx";
                        SqlParameter paran = new SqlParameter("xxx", this.DropDownList2.SelectedValue);
                        cmd.Parameters.Add(paran);
                        using (SqlDataReader reader = cmd.ExecuteReader())
                        {
                            this.DropDownList3.DataSource = reader;
                            this.DropDownList3.DataTextField = "DistrictName";
                            this.DropDownList3.DataValueField = "DistrictID";
                            this.DropDownList3.DataBind();
                        }
                    }
                }
            }
        }
    }

  • 相关阅读:
    转:5个AJAX Loading动画图标生成器
    小练习:图片轮播jQuery版
    找不同,在一定范围内找出不同数最小的数组。
    一个Hibernate的Hello World, 基于Hibernate 4.0
    队列的操作, 计算要出队某个数需要移动的距离.
    使用反射操作私有(Private)方法和属性
    求由色子组成的立方体的5个可见面(底部不算)中所有数字最小之和.
    动态代理的简单实例.
    设计模式:Java的代理模式.
    Java的反射 基础+简单复制对象实例
  • 原文地址:https://www.cnblogs.com/duanlinlin/p/2954621.html
Copyright © 2011-2022 走看看