zoukankan      html  css  js  c++  java
  • DropDownList

    use master
    go

    if exists(select * from sys.databases where name='MyData')
    drop database MyData
    go

    create database MyData
    go

    use MyData
    go

    if exists(select * from sys.objects where name='Provice')
    drop table provice
    go

    create table Provice
    (
     proid int primary key identity(1,1),
     proname varchar(100),
     upproid int
    )
    go

    --select * from Provice
    --go

    insert into Provice(proname,upproid) values('河北',0)
    insert into Provice(proname,upproid) values('保定',1)
    insert into Provice(proname,upproid) values('张家界',1)

    insert into Provice(proname,upproid) values('湖北',0)
    insert into Provice(proname,upproid) values('荆州',4)
    insert into Provice(proname,upproid) values('仙桃',4)

    insert into Provice(proname,upproid) values('湖南',0)
    insert into Provice(proname,upproid) values('长沙',7)
    insert into Provice(proname,upproid) values('岳阳',7)
    go

    s

    前台代码

    <asp:DropDownList ID="ddlOne" runat="server" ontextchanged="ddlOne_TextChanged" onselectedindexchanged="Page_Load" AutoPostBack="true"></asp:DropDownList>            
    <asp:DropDownList ID="ddlTwo" runat="server" onselectedindexchanged="Page_Load"></asp:DropDownList>

    后台代码

     protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    Share();
                    CommondOne();
                }
            }

            public string str = "";
            public SqlConnection con;
            protected void Share()
            {
                str = ConfigurationManager.ConnectionStrings["ConStr"].ToString();
                con = new SqlConnection(str);
                con.Open();
            }

            protected void CommondOne()
            {
                SqlCommand cmd = new SqlCommand("select * from Provice where  upproid=0",con);
                SqlDataReader dr = cmd.ExecuteReader();

                ddlOne.Items.Clear();
                ddlOne.Items.Add(new ListItem("请选择省", ""));
                ddlTwo.Items.Add(new ListItem("请选择城市", ""));

                if (!dr.HasRows)
                {
                    return;
                }

                while (dr.Read())
                {
                    ddlOne.Items.Add(new ListItem(dr["proname"].ToString(), dr["proid"].ToString()));
                }
                dr.Close();
                con.Close();
            }

            protected void ddlOne_TextChanged(object sender, EventArgs e)
            {
                Share();
                CommondTwo();
            }

            protected void CommondTwo()
            {
                SqlCommand cmd = new SqlCommand("select * from Provice where upproid =" + Convert.ToInt32(ddlOne.SelectedValue), con);
                SqlDataReader dr = cmd.ExecuteReader();
                ddlTwo.Items.Clear();

                ddlTwo.Items.Add(new ListItem("请选择城市", ""));
                if (!dr.HasRows)
                {
                    return;
                }

                while (dr.Read())
                {
                    ddlTwo.Items.Add(new ListItem(dr["proname"].ToString(), dr["proid"].ToString()));
                }
                dr.Close();
                con.Close();
            }      

  • 相关阅读:
    Jedis、RedisTemplate、StringRedisTemplate之间的比较
    阿里云服务器搭建DiscuzQ
    阿里云Docker加速镜像配置
    IDEA不卡配置
    Tomcat中出现"RFC 7230 and RFC 3986"错误的解决方法
    Eclipse设置保存时自动格式化代码
    VUE中v-for和v-if不能同时使用的问题
    JS中设置cookie,读取cookie,删除cookie
    JS中数组的拷贝方法
    在VSCode中开启ESLint风格审查
  • 原文地址:https://www.cnblogs.com/meroselove/p/1870258.html
Copyright © 2011-2022 走看看