Introduction:
In this article,i am going to explain about how to effectively use asp.net treeview in asp.net 2.0 and above versions.
Main:
The ASP.NET 2.0TreeView control can be used to display hierarchical data from a data source. You can create a TreeView control programmatically in the .aspx file as shown in the following code snippet:
You can easily add nodes to the TreeView control programmatically,
We can easily bind any kind of data using datasourceid property,see the below simple examble,
<asp:TreeView ID="TreeView1" runat="server"
DataSourceID="SqlDataSource1" BackColor="White" Font
Bold="True" Font-Italic="True" ForeColor="Black">
<ParentNodeStyle Font-Bold="True" ForeColor="Black"
BackColor="SkyBlue" />
<SelectedNodeStyle Font-Underline="True" HorizontalPadding="0px"
VerticalPadding="0px" BackColor="#C04000" />
<NodeStyle Font-Names="Verdana" Font-Size="8pt" ForeColor="Black"
HorizontalPadding="5px" NodeSpacing="0px"
VerticalPadding="0px" BackColor="#00C0C0" />
<DataBindings>
<asp:TreeNodeBinding DataMember="Address" ValueField="Value"/>
<asp:TreeNodeBinding DataMember="Employee" ValueField="Name"/>
</DataBindings>
<LeafNodeStyle BackColor="#FFE0C0" />
</asp:TreeView>
<asp:TreeView ID="TreeView1" runat="server" DataSourceID="SqlDataSource1" BackColor="White" Font Bold="True" Font-Italic="True" ForeColor="Black"> <ParentNodeStyle Font-Bold="True" ForeColor="Black" BackColor="SkyBlue" /> <SelectedNodeStyle Font-Underline="True" HorizontalPadding="0px" VerticalPadding="0px" BackColor="#C04000" /> <NodeStyle Font-Names="Verdana" Font-Size="8pt" ForeColor="Black" HorizontalPadding="5px" NodeSpacing="0px" VerticalPadding="0px" BackColor="#00C0C0" /> <DataBindings> <asp:TreeNodeBinding DataMember="Address" ValueField="Value"/> <asp:TreeNodeBinding DataMember="Employee" ValueField="Name"/> </DataBindings> <LeafNodeStyle BackColor="#FFE0C0" /> </asp:TreeView> |
In code behind sample,
private void CreateTreeView()
{
for (int i = 0; i < 10; i++)
{
TreeNode treeNode = new TreeNode();
treeNode.Text = "Node Item: " + i.ToString();
treeNode.Value = "Node Item: " + i.ToString();
treeNode.ShowCheckBox = true;
treeNode.ToolTip =
"This is Node Item: " + i.ToString();
TreeView1.Nodes.Add(treeNode);
}
this.Panel1.Controls.Add(TreeView1);
}
private void CreateTreeView() { for (int i = 0; i < 10; i++) { TreeNode treeNode = new TreeNode(); treeNode.Text = "Node Item: " + i.ToString(); treeNode.Value = "Node Item: " + i.ToString(); treeNode.ShowCheckBox = true; treeNode.ToolTip = "This is Node Item: " + i.ToString(); TreeView1.Nodes.Add(treeNode); } this.Panel1.Controls.Add(TreeView1); } |
Conclusion:
Hope this helps,
Happy Coding.
found your site on del.icio.us today and really liked it.. i bookmarked it and will be back to check it out some more later