Use the UserControl to do the website navigation bar The idea analysis _ practical skill

Source: Internet
Author: User

URL is actually the mapping of directory files. So as long as you split the URL and loop according to the hierarchy of the URL, you get the directory of the current navigation file.

1, the Establishment of database table: Navigate

Column Name Data type Description
Id Int
ParentID Int Parent ID
Root Int Level, 0 represents the root, topmost
PathName varchar (50) (URL) path name
Navname varchar (50) (column) Navigation name


2, the establishment of user control Navigate.ascx
-----------------------------------------------------------
<%@ control language= "C #" autoeventwireup= "true" codefile= "Navigate.ascx.cs" inherits= "Uc_navigate"%>
<div style= "display:block;margin-top:5px;margin-left:3px;margin-bottom:5px;" >

Your current location: <a href= "/index.aspx" > Home </a>
>>
<asp:label id= "lblnavigate" runat= "Server" ></asp:Label>
<asp:label id= "Lbltitle" runat= "Server" ></asp:Label>

</div>


3, the user control's CS background file Navigate.ascx.cs
-----------------------------------------------------------
Using System;
Using System.Collections;
Using System.ComponentModel;
Using System.Data;
Using System.Data.SqlClient;
Using System.Configuration;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Web.UI.HtmlControls;

Using Bai51.data;

public partial class UC_Navigate:System.Web.UI.UserControl
{
private String Localurl= "http://localhost:8085";
private string title;
Private DataSet DS;
Private DataTable DT;

protected void Page_Load (object sender, EventArgs e)
{

#region--Get navigation information table contents (cache)--
Ds= (DataSet) cache["Dsnavigatecache"];
if (ds==null)
{
Ds=sqlcomd.createsqldataset ("SELECT * from Navigate", "dsnavigate");
Cache.Insert ("Dsnavigatecache", Ds,null,system.web.caching.cache.noabsoluteexpiration,timespan.fromhours (24));
}
Dt=ds. Tables[0];
#endregion

#region--Generate navigation information--
Split Current URL address
string[] Weburl = System.Web.HttpContext.Current.Request.path. Split (new char[] {'/'});
int len = weburl.length-1;

Hierarchy cycle
String strURL = Localurl + "/";
String strnavigate = "";
String Parenid = "0";

for (int i = 1; i < Len; i++)
{
String sql = "root=" + convert.tostring (i-1) + "and pathname= '" + weburl[i] + "' and parentid=" + Parenid;
datarow[] rows = dt. Select (SQL);

Circular Connection Address
strURL + + (Weburl[i] + "/");
Strnavigate + = "<a href= '" + strurl + "' >" + rows[0]["Navname"]. ToString () + "</a>" + ">>";

Save Parent ID
Parenid = rows[0]["Id"]. ToString ();
}
#endregion

Lblnavigate.text=strnavigate;
Lbltitle.text=title;
}

#region--Control properties--
public string Localurl
{
Get{return Localurl;}
set{Localurl=value;}
}
public string Title
{
get {return title;}
set {title = value;}
}
#endregion

}

4, the use of methods
-----------------------------------------------------------
Drag the Navigate.ascx user control into the page, named "NavBar".
The navigation name of the current file is written in the background code.
Navbar.title = "This page is xx column";

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.