数据的三级联动

<body>
    <form id="form1" runat="server">
    <div>
        <div style="text-align: center">
            <table style="width: 458px; height: 24px">
                <tr>
                    <td style="width: 100px">
                        <asp:DropDownList ID="danwei" runat="server" AutoPostBack="True" OnSelectedIndexChanged="danwei_SelectedIndexChanged"
                            Width="160px">
                        </asp:DropDownList></td>
                    <td style="width: 100px">
                        <asp:DropDownList ID="roadname" runat="server" AutoPostBack="True" Width="142px" OnSelectedIndexChanged="roadname_SelectedIndexChanged">
                        </asp:DropDownList></td>
                    <td style="width: 100px">
                        </td>
                </tr>
            </table>
            <div style="text-align: center">
                <table style="width: 318px; height: 22px">
                    <tr>
                        <td style="width: 100px">
                            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="按路名显示图片" /></td>
                        <td style="width: 100px">
                            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></td>
                    </tr>
                </table>
            </div>
            <br />
            <div style="text-align: center">
                 
                <br />
                <div style="text-align: center">
                    <table style="width: 356px; height: 83px">
                        <tr>
                            <td style="width: 100px">
                                <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="按路段显示图片" /></td>
                            <td style="width: 100px">
                        <asp:DropDownList ID="roadnum" runat="server" Width="128px" OnSelectedIndexChanged="roadnum_SelectedIndexChanged" >
                        </asp:DropDownList></td>
                        </tr>
                        <tr>
                            <td style="width: 100px">
                            </td>
                            <td style="width: 100px">
                                <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label></td>
                        </tr>
                    </table>
                </div>
            </div>
        </div>
    
    </div>
    </form>
</body>

 

后台页

 

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            filldanwei();
           
        }
        
    }

    public void filldanwei()
    {

        SqlConnection con = new SqlConnection("server=localhost;database=DQMQI;uid=sa;pwd=1234");
        con.Open();
        string sql = "select DepartmentName from TDQDepartment";
        SqlCommand com = new SqlCommand(sql, con);

        SqlDataReader dr = com.ExecuteReader();


        while (dr.Read())
        {
            danwei.Items.Add(dr["DepartmentName"].ToString());


        }

        dr.Close();
        con.Close();
        luname();
        lunum();
    }


    public void luname()
    {

        SqlConnection con = new SqlConnection("server=localhost;database=DQMQI;uid=sa;pwd=1234");
        con.Open();
        string sql = "select  TDQRoadInfo.Department,TDQRoadInfo.RoadName from TDQRoadInfo,TDQDepartment  where TDQDepartment.Department=TDQRoadInfo.Department  and TDQDepartment.DepartmentName='" + danwei.SelectedItem.Text  + "' ";
        SqlCommand com = new SqlCommand(sql, con);

        SqlDataReader dr = com.ExecuteReader();


        while (dr.Read())
        {
            roadname.Items.Add(dr["RoadName"].ToString());


        }

        dr.Close();
        con.Close();

    }




    protected void danwei_SelectedIndexChanged(object sender, EventArgs e)
    {

        roadname.Items.Clear();
        roadnum.Items.Clear();

        luname();
        lunum();
    }


    public void lunum()
    {

        SqlConnection con = new SqlConnection("server=localhost;database=DQMQI;uid=sa;pwd=1234");
        con.Open();
        string sql = "select  RoadSectionNum from RoadSectionInfo  where  RoadName='" + roadname.SelectedItem.Text + "' ";
        SqlCommand com = new SqlCommand(sql, con);

        SqlDataReader dr = com.ExecuteReader();


        while (dr.Read())
        {
            roadnum.Items.Add(dr["RoadSectionNum"].ToString());

            //Session["RoadSectionNum"] = roadnum.SelectedItem.Text;
            Label1.Text = roadnum.SelectedItem.Text;
            Session["RoadSectionNum"] = Label1.Text;
        }

        dr.Close();
        con.Close();

    }


    protected void roadname_SelectedIndexChanged(object sender, EventArgs e)
    {
        roadnum.Items.Clear();

        lunum();
       
    }


 

其中  roadnum.Items.Clear();   相当有用

他可以防止  数据的重复出现  即 把以前的内容  再在  dropdownlist 出现

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值