C#写的多级关联菜单

这是一个使用C#编写的多级关联菜单示例,通过ASP.NET控件实现动态加载。当用户从省份下拉菜单中选择一项时,会自动加载对应城市的下拉菜单;同样,从城市下拉菜单中选择后,会加载相应的区/县下拉菜单。代码中使用了SqlConnection和SqlDataAdapter来连接和查询SQL Server数据库。

C#写的多级关联菜单

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!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:Label ID="lblProv" runat="server" Text="省份"></asp:Label>
<asp:DropDownList ID="ddProv" runat="server" DataTextField="dqmc" DataValueField="dqbh"  AutoPostBack="True" OnSelectedIndexChanged="ddProv_SelectedIndexChanged" >
</asp:DropDownList>
<asp:Label ID="lblCity" runat="server" Text="城市" Visible="False"></asp:Label><asp:DropDownList ID="ddCity" runat="server" Visible="false" DataTextField="dqmc" DataValueField="dqbh" AutoPostBack="True" OnSelectedIndexChanged="ddCity_SelectedIndexChanged">
</asp:DropDownList>
<asp:Label ID="lblQX" runat="server" Text="区/县" Visible="False"></asp:Label><asp:DropDownList ID="ddQX" runat="server" AutoPostBack="false" Visible="false" DataTextField="dqmc" DataValueField="dqbh">
</asp:DropDownList>&nbsp;&nbsp;

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

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;

public partial class Default2 : System.Web.UI.Page
{
    public string connstr = ConfigurationManager.ConnectionStrings[0].ToString();
   
    SqlConnection conn = new SqlConnection("server=AYKJ-CLCHUN;user id=sa;pwd=anyuan;database=changsha");
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write(connstr);
        if (!Page.IsPostBack)
        {
            try
            {
                conn.Open();
                SqlDataAdapter sda = new SqlDataAdapter("select dqbh,dqmc from T_diqu where dqdj=1", conn);
                DataSet ds = new DataSet();
                sda.Fill(ds, "T_diqu");
                ddProv.DataSource = ds.Tables[0].DefaultView;
                ddProv.DataBind();
            }
            catch (Exception se)
            {
                Response.Write(se.ToString());

            }
            finally { conn.Close(); }
        }

    }
    protected void ddProv_SelectedIndexChanged(object sender, EventArgs e)
    {
        lblCity.Visible = true;
        ddCity.Visible = true;
        try
        {
            conn.Open();
            SqlDataAdapter sda = new SqlDataAdapter("select dqbh,dqmc from T_diqu where sjdqbh='" + ddProv.SelectedItem.Value.ToString() + "'", conn);
            DataSet ds = new DataSet();
            sda.Fill(ds, "T_diqu");
            ddCity.DataSource = ds.Tables[0].DefaultView;
            ddCity.DataBind();
        }
        catch (Exception se)
        {
            Response.Write(se.ToString());

        }
        finally { conn.Close(); }
    }
    protected void ddCity_SelectedIndexChanged(object sender, EventArgs e)
    {

        ddCity.SelectedItem.Selected = true;
        lblQX.Visible = true;
        ddQX.Visible = true;
        try
        {
            conn.Open();
            SqlDataAdapter sda = new SqlDataAdapter("select dqbh,dqmc from T_diqu where sjdqbh='" + ddCity.SelectedItem.Value.ToString() + "'", conn);
            DataSet ds = new DataSet();
            sda.Fill(ds, "T_diqu");
            ddQX.DataSource = ds.Tables[0].DefaultView;
            ddQX.DataBind();
        }
        catch (Exception se)
        {
            Response.Write(se.ToString());

        }
        finally
        {
            conn.Close();
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值