It is very easy to work with treeviews and DB. The table structure is one of the most important things to take into consideration (It must be a table that has recursivity). Let's see a small example.
Description:
This treeview will show the available menu options for an application. Normally it varies depending on the environment:DEV(1), TEST(2), PRODUCTION(3). However, for this small example we are just sending 1 (Dev environment) hardcoded. You could use a combobox and send its value.
1) The table:
1 CREATE TABLE [dbo].[Metal_Menu](
2 [ME_SEQ] [uniqueidentifier] ROWGUIDCOL NOT NULL CONSTRAINT [DF_Metal_Menu_ME_SEQ] DEFAULT (newid()),
3 [ME_PARENT] [uniqueidentifier] NULL,
4 [ME_DESC] [varchar](100) NULL,
5 [ME_URL] [varchar](500) NULL,
6 [ME_PAGE] [varchar](100) NULL,
7 [ME_TYPE] [int] NULL,
8 [Active] [bit] NULL CONSTRAINT [DF_Metal_Menu_Active] DEFAULT ((1)),
9 [CreatedBY] [varchar](255) NULL CONSTRAINT [DF_Metal_Menu_CreatedBY] DEFAULT ('web\iuser'),
10 [CreatedDate] [datetime] NULL CONSTRAINT [DF_Metal_Menu_CreatedDate] DEFAULT (getdate()),
11 [ModifBy] [varchar](255) NULL CONSTRAINT [DF_Metal_Menu_ModifBy] DEFAULT ('web\iuser'),
12 [ModifDate] [datetime] NULL CONSTRAINT [DF_Metal_Menu_ModifDate] DEFAULT (getdate()),
13 CONSTRAINT [PK_Metal_Menu] PRIMARY KEY CLUSTERED
14 (
15 [ME_SEQ] ASC
16 )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
17 ) ON [PRIMARY]
2. The HTML portion of the RADTREEVIEW:
1 <telerik:RadTreeView ID="rtvFrom" runat="server" EnableDragAndDrop="True" OnNodeDrop="RadTreeView1_HandleDrop"
2 OnClientNodeDropping="onNodeDropping" OnClientNodeDragging="onNodeDragging" MultipleSelect="true" EnableDragAndDropBetweenNodes="true" CheckBoxes="false">
3 <DataBindings>
4 <telerik:RadTreeNodeBinding Expanded="True" AllowDrop="false"/>
5 </DataBindings>
6 </telerik:RadTreeView>
3. The code behind (VB.Net) for this radtreeview:
1 rtvFrom.Nodes.Clear()
2 rtvFrom.DataTextField = "SYME_DESC"
3 rtvFrom.DataFieldID = "SYME_SEQ"
4 rtvFrom.DataValueField = "SYME_SEQ"
5 rtvFrom.DataFieldParentID = "SYME_PARENT"
6 ViewState.Add("TreeFrom", GetMenuTreeSync(1, strUser))
7 rtvFrom.DataSource = CType(ViewState("TreeFrom"), Data.DataSet)
8 rtvFrom.DataBind()
4. As you can see, up to his point. I am using a method "GetMenuTreeSync" to retrieve the information from the database. The method actually calls a stored procedure to get the information, but it could just execute a SQL sentence like this:
select * from ME_menu WHERE Active = 1 order by me_parent
Comments:
In this example I used a Viewstate object to store the dataset returned from the query, however you can use a simple local variable of dataset or datatable type.
No comments:
Post a Comment