• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

asp.net中使用下拉菜单的级联问题

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

页面文件

View Code
<asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="True">

<asp:ListItem>城市1</asp:ListItem>

<asp:ListItem>城市2</asp:ListItem>

</asp:DropDownList>

<asp:DropDownList ID="DropDownList2" runat="server">

</asp:DropDownList>

程序文件

View Code
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)

{

if (DropDownList1.SelectedItem.Text == "城市1")

{

DropDownList2.Items.Clear(); DropDownList2.Items.Add("区1"); DropDownList2.Items.Add("区2");

}

else if (DropDownList1.SelectedItem.Text == "城市2")

{

DropDownList2.Items.Clear(); DropDownList2.Items.Add("区3"); DropDownList2.Items.Add("区4");

}

}

 注意第一个dropdownlist的AutoPostBack="True"一定要设置 其实如果这种省市区联动的一般不用数据库,直接在js里面写,或者找现成的有很多,
放数据库里浪费服务器资源,而且刷新效果不好。


提供一个比较好的省市三级联动的DropDownList+Ajax的三种框架(aspnet/Jquery/ExtJs)
示例:http://www.cnblogs.com/downmoon/archive/2010/06/15/1758675.html

 

附加一个使用数据库数据的例子:

前台控件代码:

<asp:PlaceHolder ID="PlaceHolder1" runat="server">
<asp:Label ID="Label1" runat="server" Text="模块:"></asp:Label>
<asp:DropDownList ID="DropDownList1" runat="server"
onselectedindexchanged="DropDownList1_SelectedIndexChanged"
AutoPostBack="True">
</asp:DropDownList>
&nbsp;&nbsp;&nbsp;&nbsp;<asp:Label ID="Label2" runat="server" Text="分类:" Visible="false"></asp:Label>
<asp:DropDownList ID="DropDownList2" Visible="false" runat="server">
</asp:DropDownList>
</asp:PlaceHolder>

后台代码段:

    protected string SqlModuleList = "";
protected string SqlTypeList = "";
SqlHelper sqlhelper = new SqlHelper();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ModuleList(); //首次加载模块的下拉数据
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedItem.Text != "")
{
SqlTypeList = "select TypeID,TypeName from KY_SendMessage_Type as T,KY_SendMessage_Module as M
where T.ModuleID=M.ModuleID and T.ModuleID=
" + DropDownList1.SelectedValue + "";
DataSet ds = sqlhelper.readDataSet(SqlTypeList);
DataTable dt = ds.Tables[0];
if (dt.Rows.Count != 0)//判断模块下面是否有子分类
{
Label2.Visible = true;
DropDownList2.Visible = true;
DropDownList2.Items.Clear();
}
else
{
Label2.Visible = false;
DropDownList2.Visible = false;
}
DropDownList2.DataSource = dt;
DropDownList2.DataValueField = "TypeID";
DropDownList2.DataTextField = "TypeName";
DropDownList2.DataBind();
}
}
protected void ModuleList()
{
SqlModuleList = "select * from KY_SendMessage_Module as M";
DataSet ds = sqlhelper.readDataSet(SqlModuleList);
DataTable dt = ds.Tables[0];
DropDownList1.DataSource = dt;
DropDownList1.DataValueField = "ModuleID";
DropDownList1.DataTextField = "ModuleName";
DropDownList1.DataBind();
}

部分效果图:

                         初始加载图

       点击选项加载子下拉图


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap