Jquery. TreeView combines ASP. Net and database to generate menu navigation bar

Source: Internet
Author: User

The following example uses Jquery. TreeView based on the actual application of the project. Of course, when using the control tree, the corresponding js file is required.
Next we will show you the generated TreeView, hoping to help you! Before use, you need to download the js file and Css style of the Control tree.

Introduction to table structure

M_ID M_Name M_ParentID M_URL M_Sort

Create a new website, add the CSS folder and js folder under the newly created website to store the Css style and JS, and add an image folder to store the TreeView image.
Then we began to implement our functions!
Front-end codeCopy codeThe Code is as follows: <% @ 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>

Background code:Copy codeThe Code is as follows: 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 a subnode exists
HtmlGenericControl serverLi = new HtmlGenericControl ("li"); // generates a Li label as the parent node.
If (childern. Length! = 0 | parentId = "") // It 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 tag
HtmlAnchor NewAnchorControl = new HtmlAnchor ();
// Set the attributes of tag
NewAnchorControl. Name = "NewAnchorControl ";
NewAnchorControl. InnerHtml = t ["M_Name"]. ToString ();
NewAnchorControl. HRef = t ["M_URL"]. ToString ();
NewAnchorControl. Target = "_ black"; // set the display position. Change it here.
ServerLi. Controls. Add (NewAnchorControl );
UL. Controls. Add (serverLi );
Createmenu (ds, t ["M_ID"]. ToString (), UL );
}
}
}
}

Related Article

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.