تقييمات الطلاب
( 5 من 5 )
١ تقييمات
فيديو شرح Part 162 Binding asp net treeview control to database table ضمن كورس ASP.net شرح قناة kudvenkat، الفديو رقم 162 مجانى معتمد اونلاين
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 cs ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
SqlConnection con new SqlConnection(cs);
SqlDataAdapter da new SqlDataAdapter("spGetTreeViewItems", con);
da.SelectCommand.CommandType CommandType.StoredProcedure;
DataSet ds new 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 treeNode new TreeNode();
treeNode.Text level1DataRow["TreeViewText"].ToString();
treeNode.NavigateUrl level1DataRow["NavigateURL"].ToString();
DataRow[] level2DataRows level1DataRow.GetChildRows("ChildRows");
foreach (DataRow level2DataRow in level2DataRows)
{
TreeNode childTreeNode new TreeNode();
childTreeNode.Text level2DataRow["TreeViewText"].ToString();
childTreeNode.NavigateUrl level2DataRow["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)