Jquery.treeview combines asp.net and database to generate menu navigation bars

Source: Internet
Author: User

Background code

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");/filter
Else
rows = Ds.tables[0].select ("m_parentid= '" + ParentID + "");/filter
foreach (DataRow t in rows)
{
datarow[] Childern = Ds.tables[0].select ("M_parentid =" + t["m_id"].tostring ());//To determine whether there are child nodes
HtmlGenericControl Serverli = new HtmlGenericControl ("Li");//Generate Li Tag as parent node
if (childern.length!= 0 | | | parentid = = "")/Is the parent node
{
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
{
Generate label A
HtmlAnchor Newanchorcontrol = new HtmlAnchor ();
Set the properties of label a
Newanchorcontrol.name = "Newanchorcontrol";
newanchorcontrol.innerhtml = t["M_name"].tostring ();
Newanchorcontrol.href = t["M_url"].tostring ();
Newanchorcontrol.target = "_black";//Set the location of the display, change it here.
Serverli.controls.add (Newanchorcontrol);
Ul.controls.add (Serverli);
CreateMenu (ds, t["m_id"].tostring (), UL);
}
}
}
}

Front Code

<%@ page language= "C #" autoeventwireup= "true" codefile= "Default.asp tutorial X.cs" inherits= "_default"%>
<!doctype HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/ Xhtml1-transitional.dtd ">
<title></title>
<link rel= "stylesheet" href= "CSS tutorial/screen.css"/>
<link rel= "stylesheet" href= "Css/jquery.treeview.css"/>
<script src= "Js/jquery-1.4.2.js" type= "text/Web Effects" ></script>
<script src= "Js/jquery.treeview.js" type= "Text/javascript" ></script>
<script type= "Text/javascript" >
$ (function () {
$ ("#tree"). TreeView ();
})
</script>

<body>
<div id= "Main" >
<a>main demo</a>
<div id= "Sidetree" >
<ul id= "tree" runat= "Server" >
</ul>
</div>
</div>
</body>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.