asp.net 2.0 's navigation control instance of TreeView and Menu

Source: Internet
Author: User
asp.net|treeview| Navigation | control

ASP.NET2.0 provides users with the TreeView and menu navigation controls, which are easy to use and greatly improve the speed and efficiency of user development projects.

In particular, XML files such as the TreeView and menu bindings Web.sitemap are convenient to form hierarchical data.

But I in the project development process, many places need to bind the relational database, after all, the relational database is most commonly used, the operation most convenient storage form.

For Treebview and relational database bindings can be dynamically bound, that is, first-level nodes are added first in the Page_Load event, and then the next level node is added in the Selectednodechanged event of the TreeView. Because of the use of Ajax technology, the TreeView control is placed inside the UpdatePanel control, and the user's page experience works pretty well.

For the menu control, however, this is a problem, because I found that the menu has problems with the UpdatePanel control: it's that the original item is left on the screen after the mouse is moved, until you click on the New menu item (I don't know if it's a bug or where I'm having problems). Maybe some people feel that menu do not dynamically bind, directly binding XML to make static how good! But when the project needs different users to appear after the menu effect is different, and in Web.sitemap roles role control is not very convenient, so use the relational database directly to the menu menu item enabled to assign value, control more flexible.

Because there are bugs, the menu control can only be placed outside the UpdatePanel control, and the binding to the menu is bound to be one-time in the Page_Load event, and if you use the Menuitemclick event binding of the menu control, then the user can't stand the experience!

The effect of the implementation is as follows:

The specific implementation code is as follows:

Default.aspx

1<%@ Page language= "VB" autoeventwireup= "false" codefile= "Default4.aspx.vb" inherits= "DEFAULT4"%>
2<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
345 <title> navigation control test </title>
67<body>
8 <form id= "Form1" runat= "Server" >
9
10
One <asp:menu id= "Menu1"
runat= "Server"
Disappearafter= "2000"
Staticsubmenuindent= "10px"
orientation= "Horizontal"
Staticenabledefaultpopoutimage= "False"
Backcolor= "#E3EAEB"
Dynamichorizontaloffset= "2"
Font-names= "Song Body"
Font-size= "14px"
Forecolor= "#666666" >
<staticmenuitemstyle horizontalpadding= "5px" verticalpadding= "2px"/>
<dynamichoverstyle backcolor= "#666666" forecolor= "white"/>
<dynamicmenustyle backcolor= "#E3EAEB"/>
<staticselectedstyle backcolor= "#E3EAEB"/>
<dynamicselectedstyle backcolor= "#E3EAEB"/>
<dynamicmenuitemstyle horizontalpadding= "5px" verticalpadding= "2px"/>
<statichoverstyle backcolor= "#666666" forecolor= "white"/>
</asp:Menu>
30
</form>
32</body>
33

Default.aspx.vb

1Imports System
2Imports System.Data
3Imports System.Data.SqlClient
4Partial Class Default4class DEFAULT4
5 Inherits System.Web.UI.Page
6 Public strconn, strSQL as String
7 Dim MyConn as SqlConnection
8 Dim MYDV as DataView
9 Dim I as Integer
Ten Dim Menunode as MenuItem
One Protected Sub Page_Load () Sub Page_Load (ByVal sender as Object, ByVal e as System.EventArgs) Handles Me.load
strconn = System.Configuration.ConfigurationManager.AppSettings ("connstring")
myconn = New SqlConnection (strconn)
14
If not Page.IsPostBack Then
strSQL = "Select * FROM Menu"
MYDV = Getdataview (strSQL, "menu")
Mydv. RowFilter = "Parentid=0"
For i = 0 to MYDV. Count-1
Menunode = New MenuItem
Menunode. Text = Mydv. Item (i). Item ("text")
Menunode. Value = Mydv. Item (i). Item ("id")
Menunode. Enabled = Mydv. Item (i). Item ("Enabled")
Menunode. Target = Mydv. Item (i). Item ("Target")
Menunode. NavigateUrl = Mydv. Item (i). Item ("url")
Num MENU1.ITEMS.ADD (Menunode)
AddNode (Menunode, Menunode. Value)
Mydv. RowFilter = "Parentid=0"
Next
End If
End Sub
32
Sub AddNode () Sub AddNode (ByVal pnode as MenuItem, ByVal pid as Integer)
Dim Menunode as MenuItem
Dim J as Integer
Mydv. RowFilter = "parentid=" & PID
Panax for j = 0 to MYDV. Count-1
Menunode = New MenuItem
Menunode. Text = Mydv. Item (j). Item ("text")
Menunode. Value = Mydv. Item (j). Item ("id")
Menunode. Enabled = Mydv. Item (j). Item ("Enabled")
Menunode. Target = Mydv. Item (j). Item ("Target")
Menunode. NavigateUrl = Mydv. Item (j). Item ("url")
Pnode. Childitems.add (Menunode)
AddNode (Menunode, Menunode. Value)
Mydv. RowFilter = "parentid=" & PID
Next
48
End Sub
50
The Getdataview () function Getdataview (ByVal Sql As String, ByVal strtable as String) as DataView
Dim MyDataAdapter as SqlDataAdapter
Dim myDataSet as DataSet
MyConn.Open ()
MyDataAdapter = New SqlDataAdapter (SQL, myconn)
Myconn.close ()
myDataSet = New DataSet
Mydataadapter.fill (myDataSet, strtable)
Getdataview = New DataView (mydataset.tables (strtable))
The End Function
61
62
63End Class
64

The database structure is as follows:

The key to the above implementation is:

1, AddNode recursive process, to achieve the addition of subordinate nodes;

2, DataView RowFilter attribute, MYDV. RowFilter = "parentid=" & PID, filter out the subordinate nodes of a node and add it.

The above code, there is no optimization, is not necessarily the best, hope!



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.