Jquery.TreeView结合ASP.Net和数据库生成菜单导航条
后台代码
using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.ui;
using system.web.ui.webcontrols;
using system.data;
using system.data.sqlclient;
using system.web.ui.htmlcontrols;
public partial class _default : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{
if (!ispostback)
{
dataset ds = getdate();
createmenu(ds, null, tree);
}
}
public dataset getdate()
{
dataset ds = new dataset();
string config = system.configuration.configurationmanager.connectionstrings["liveoffice"].tostring();
sqlconnection conn = new sqlconnection(config);
sqldataadapter da = new sqldataadapter("select * from systemmenu order by m_sort", conn);
da.fill(ds);
return ds;
}
private void createmenu(dataset ds, string parentid, htmlgenericcontrol ul)
{
datarow[] rows;
if (string.isnullorempty(parentid))
rows = ds.tables[0].select("m_parentid is null");//过滤
else
rows = ds.tables[0].select("m_parentid='" + parentid + "'");//过滤
foreach (datarow t in rows)
{
datarow[] childern = ds.tables[0].select("m_parentid =" + t["m_id"].tostring());//用于判断是否有子节点
htmlgenericcontrol serverli = new htmlgenericcontrol("li");//生成li标签,作为父节点
if (childern.length != 0 || parentid == "")//是父节点
{
serverli.innertext = t["m_name"].tostring();
htmlgenericcontrol serverul = new htmlgenericcontrol("ul");
serverli.controls.add(serverul);
ul.controls.add(serverli);
createmenu(ds, t["m_id"].tostring(), serverul);
}
else
{
//生成标签a
htmlanchor newanchorcontrol = new htmlanchor();
// 设置标签a的属性
newanchorcontrol.name = "newanchorcontrol";
newanchorcontrol.innerhtml = t["m_name"].tostring();
newanchorcontrol.href = t["m_url"].tostring();
newanchorcontrol.target = "_black";//设置显示的位置,这里改一下
serverli.controls.add(newanchorcontrol);
ul.controls.add(serverli);
createmenu(ds, t["m_id"].tostring(), ul);
}
}
}
}
前台代码
<%@ page language="c#" autoeventwireup="true" codefile="default.asp教程x.cs" inherits="_default" %>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.zzzyk.com/1999/xhtml">
<head runat="server">
<title></title>
<link rel="stylesheet" href="css教程/screen.css" />
<link rel="stylesheet" href="css/jquery.treeview.css" />
<script src="js/jquery-1.4.2.js" type="text/网页特效"></script>
<script src="js/jquery.treeview.js" type="text/javascript"></script>
<script type="text/javascript">
$(function() {
$("#tree").treeview();
})
</script>
</head>
<body>
<div id="main">
<a>main demo</a>
<div id="sidetree">
<ul id="tree" runat="server">
</ul>
</div>
</div>
</body>
</html>
补充:asp.net教程,.Net开发