Part 162 Binding asp net treeview control to database table

  • دعنا نشارك هذا :
ترتيب الدرس : 162
المدة : 00:14:29
التحكم :
Link for code samples used in the demo
http://csharp-video-tutorials.blogspot.com/2013/10/part-162-binding-aspnet-treeview.html

Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.
https://www.youtube.com/channel/UC7sEwIXM_YfAMyonQCrGfWA/?sub_confirmation1

Link for all dot net and sql server video tutorial playlists
http://www.youtube.com/user/kudvenkat/playlists

In this video, we will discuss binding asp.net treeview control to database table. This is continuation to Part 161, please watch Part 161 before proceeding.

1. Create the required database table using the script below.
Create table tblTreeViewItems
(
ID int identity primary key,
TreeViewText nvarchar(50),
NavigateURL nvarchar(50),
ParentId int references tblTreeViewItems(ID)
)

2. Create a stored procedure that returns data from tblTreeViewItems table.
Create proc spGetTreeViewItems
as
Begin
Select ID, TreeViewText, NavigateURL, ParentId
from tblTreeViewItems
End

3. Drag and drop a treeview control on the webform

4. Copy and paste the following ado.net code in the code-behind file.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetTreeViewItems();
}
}

private void GetTreeViewItems()
{
string csConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
SqlConnection connew SqlConnection(cs);
SqlDataAdapter danew SqlDataAdapter("spGetTreeViewItems", con);
da.SelectCommand.CommandTypeCommandType.StoredProcedure;
DataSet dsnew DataSet();
da.Fill(ds);

ds.Relations.Add("ChildRows", ds.Tables[0].Columns["ID"],
ds.Tables[0].Columns["ParentId"]);

foreach (DataRow level1DataRow in ds.Tables[0].Rows)
{
if (string.IsNullOrEmpty(level1DataRow["ParentId"].ToString()))
{
TreeNode treeNodenew TreeNode();
treeNode.Textlevel1DataRow["TreeViewText"].ToString();
treeNode.NavigateUrllevel1DataRow["NavigateURL"].ToString();

DataRow[] level2DataRowslevel1DataRow.GetChildRows("ChildRows");
foreach (DataRow level2DataRow in level2DataRows)
{
TreeNode childTreeNodenew TreeNode();
childTreeNode.Textlevel2DataRow["TreeViewText"].ToString();
childTreeNode.NavigateUrllevel2DataRow["NavigateURL"].ToString();
treeNode.ChildNodes.Add(childTreeNode);
}
Treeview1.Nodes.Add(treeNode);
}
}
}

Note: Please include the following using declarations.
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

The above code works as expected only with 2 levels of TreeNode objects. If you include a third level, those TreeNodes will not be displayed in the TreeView control. For example, if you execute the following insert sql script. These rows will not be displayed in the TreeView control, we will discuss fixing this in our next video.
Insert into tblTreeViewItems values ('AAA', '~/AAA.aspx', 5)
Insert into tblTreeViewItems values ('BBB', '~/BBB.aspx', 5)

سيتم تفعيل التعليقات قريبا

y
Sep 23, 2020 07:32:27
youtube