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>
</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();
}
}
}

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

被折叠的 条评论
为什么被折叠?



