Urgent!!! Treeview in ASP.NET

Posted by .Netlearner under ASP.NET on 8/7/2012 | Points: 10 | Views : 2578 | Status : [Member] | Replies : 6
Hello All,

I have a tree view control in my aspx page and using stored procedure i have retrieved data from the database to the treeview.While executing it shows no error but the tree view is not visible.It just shows an empty page can anyone please help with this.Do i want to add any additional code ? Whats the problem kindly guide me.
ASPX Code:

<asp:TreeView ID="trpart" runat="server" 
ImageSet="Inbox"

OnSelectedNodeChanged="trpart_SelectedNodeChanged"
OnTreeNodePopulate="trpart_TreeNodePopulate" ShowExpandCollapse="False">
<ParentNodeStyle Font-Bold="False" ForeColor="black" />
<HoverNodeStyle Font-Underline="True" />
<SelectedNodeStyle Font-Underline="True" HorizontalPadding="0px"
NodeSpacing="2px" VerticalPadding="0px" />
<NodeStyle HorizontalPadding="0px"
NodeSpacing="2px" VerticalPadding="0px" />
</asp:TreeView>


aspx.cs code:

SqlCommand cmd1;
SqlDataReader dr1;
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["con"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{

LoadTree();
trpart.ExpandAll();
trpart.Visible = true;
}

protected void LoadTree()
{
trpart.Visible = true;
con.Open();
cmd1 = new SqlCommand("select a.PartID,a.PartName,b.StatusID,b.Status,c.SStatusID,c.Substatus from tblpart a,tblstatus b,tblsubstatus c where c.StatusID=b.StatusID and b.PartID=a.PartID", con);
dr1 = cmd1.ExecuteReader();
while (dr1.Read())
{
TreeNode aa = new TreeNode();
aa.Text = dr1["PartName"].ToString();
aa.Value = dr1["PartID"].ToString();

}
trpart.ExpandAll();
trpart.Visible = true;
}
protected void trpart_TreeNodePopulate(object sender, TreeNodeEventArgs e)
{
if (e.Node.ChildNodes.Count == 0)
{
switch (e.Node.Depth)
{
case 0:
GetStatus(e.Node);
break;
}
}
}

protected void trpart_SelectedNodeChanged(object sender, EventArgs e)
{
}

protected void GetStatus(TreeNode n)
{
con.Open();
cmd1 = new SqlCommand("select a.PartID,a.PartName,b.StatusID,b.Status,c.SStatusID,c.SubStatus from tblpart a,tblstatus b,tblsubstatus c where c.StatusID=b.StatusID and b.PartID=a.PartID", con);
dr1 = cmd1.ExecuteReader();
while (dr1.Read())
{
TreeNode aa = new TreeNode();
aa.Text = dr1["Status"].ToString();
aa.Value = dr1["StatusID"].ToString();
aa.SelectAction = TreeNodeSelectAction.Select;
n.ChildNodes.Add(aa);
}
}


Thanks in advance




Responses

Posted by: Sheonarayan on: 8/7/2012 [Administrator] HonoraryPlatinum | Points: 25

Up
0
Down
Looks like you are not populating the TreeView in LoadTree method. You are just creating new TreeNode in the while loop but not adding them into the TreeView. This might be the mistake.

Regards,
Sheo Narayan
http://www.dotnetfunda.com

.Netlearner, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: .Netlearner on: 8/7/2012 [Member] Starter | Points: 25

Up
0
Down
How to add that to the tree view it will be more helpful if u explain with some sample code,how to add the tree node with the treeview

.Netlearner, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: .Netlearner on: 8/7/2012 [Member] Starter | Points: 25

Up
0
Down
And How to add child nodes to the child nodes can anyone please guide me with that too

.Netlearner, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sheonarayan on: 8/7/2012 [Administrator] HonoraryPlatinum | Points: 25

Up
0
Down
Hello .NetLearner,

Read this article http://www.dotnetfunda.com/articles/article1726-how-to-display-hierarchical-items-in-treeview-and-how-to-display-a-button-o.aspx that will help you understand all you have asked.

Thanks

Regards,
Sheo Narayan
http://www.dotnetfunda.com

.Netlearner, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Taanu51 on: 8/7/2012 [Member] Starter | Points: 25

Up
0
Down
Hi..check this link it may helps you

http://www.developerscode.com/2012/08/how-to-use-treeview-control-in-aspnet.html

Let me know if you have any issues...!

Tanisha Sayyad
.Net and Android Developer
INDIA(Andhra Pradesh)

.Netlearner, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: .Netlearner on: 8/7/2012 [Member] Starter | Points: 25

Up
0
Down
I Added
 trpart.Nodes.Add(aa);
in Loadtree method now am getting the root nodes but the cild nodes are not getting displayed

.Netlearner, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response