


Jquery.TreeView combines ASP.Net and database to generate menu navigation bar_jquery
May 16, 2016 pm 06:20 PMThe following example will use Jquery.TreeView based on the actual application of the project. Of course, using the control tree requires the corresponding js file
Now I will show you the TreeView I generated. I hope it will be helpful to everyone! Before using it, you need to download the js file and Css style of the control tree
Introduce the table structure
M_ID M_Name M_ParentID M_URL M_Sort
Then create a new website and create a new one Add a CSS folder and a js folder to the website to store Css styles and JS respectively, and add an image folder to store the TreeView pictures
Then we start to implement our functions!
Front-end code
<%@ Page Language= "C#" AutoEventWireup="true" CodeFile="Default.aspx.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.w3.org/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/javascript"> </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>
Backend 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() );//Used to determine whether there are child nodes
HtmlGenericControl serverLi = new HtmlGenericControl("li");//Generate Li tag as the 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
[ "M_Name"].ToString();
NewAnchorControl.HRef = t["M_URL"].ToString();
NewAnchorControl.Target = "_black";//Set the displayed position, change it here
serverLi.Controls.Add(NewAnchorControl);
UL.Controls.Add(serverLi);
createmenu(ds, t["M_ID"].ToString(), UL);
}
}
}
}

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Detailed explanation of jQuery reference methods: Quick start guide

How to use PUT request method in jQuery?

How to remove the height attribute of an element with jQuery?

jQuery Tips: Quickly modify the text of all a tags on the page

Use jQuery to modify the text content of all a tags

In-depth analysis: jQuery's advantages and disadvantages

Understand the role and application scenarios of eq in jQuery

How to tell if a jQuery element has a specific attribute?
