动态下拉菜单的简单实现

最近想要在C#下做一个下拉菜单,找了好久也没有找到一个简单的实现方法,自己想了一个;介绍一下:首先在aspx文件中加入如下代码:

 1<script>   
 2function openMenu(cur)//显示菜单   
 3{   
 4eval("menu"+cur+".style.visibility='visible'")   
 5//alert(menu1)   
 6}   
 7function closeMenu(cur)//隐藏菜单   
 8{   
 9eval("menu"+cur+".style.visibility='hidden'")   
10}   
11</script>
 1<td class="lcolhead" onmouseout='closeMenu("1")' onmouseover='openMenu("1")' style="PADDING-RIGHT: 8px; PADDING-LEFT: 8px">   
 2//添加一个列   
 3<a href="#" onmouseout='closeMenu("1")' onmouseover='openMenu("1")'>   
 4大生产作品</a>
 5
 6//定义一个单独的层   
 7<div class="unnamed1" id="Layer1" style="Z-INDEX: 1; LEFT: 0px; WIDTH: 0px; POSITION: relative; TOP: -2px; HEIGHT: 0px">
 8
 9//定义一个菜单   
10<div id="menu1" onmouseout='closeMenu("1")' onmouseover='openMenu("1")' style="Z-INDEX: 2; LEFT: 0px; VISIBILITY: hidden; POSITION: absolute; TOP: 0px">   
11//添加一个表,容纳菜单   
12<table align="center" border="0" cellpadding="1" cellspacing="1" id="Table1" width="120">
13<tr>
14<td align="center">
15
16//加入DataGrid控件,并添加一列做菜单,将数据库中数据读出与此列邦定写形成菜单   
17<asp:datagrid autogeneratecolumns="False" borderwidth="0px" id="DataGrid1" runat="server" showheader="False">
18<itemstyle bordercolor="Transparent">
19</itemstyle>   
20  
21//加入链接列   
22<columns>
23<asp:hyperlinkcolumn datanavigateurlfield="ID" datanavigateurlformatstring="http://localhost/Taile/Creativity/WebForm1.aspx?id={0}" datatextfield="Name" 显示数据库中的name字段="">
24<headerstyle width="120px">
25</headerstyle>
26<itemstyle font-italic="True">
27</itemstyle>
28</asp:hyperlinkcolumn>
29</columns>
30</asp:datagrid>
31</td>
32</tr>
33</table>
34</div>
35</div>
36</td>

以上是aspx文件中要添加的代码

然后在aspx.cs文件中将数据读出邦定到datagrid1上即可用数据库中的Name字段来形成下菜单
如:
protected System.Web.UI.WebControls.DataGrid DataGrid1;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here

DataView UserProductDataView=new DataView();

//获得数据库视图
UserProductDataView=(new UserProductBF()).GetUserProductType();

DataGrid1.DataSource=UserProductDataView;
DataGrid1.DataBind();
}
运行即可,

哈哈,比较简单,我目前是这样做的,不知道这样生成菜单好不好,其出来供大家参考,还望大家不要见笑才好。

Published At
Categories with Web编程
Tagged with
comments powered by Disqus