Creating a Treeview Menu in ASP.NET with C#
Using the Code
Download http://www.asp.net/IEWebControls/Download.aspx?tabindex=0&tabid=1. Install the downloaded file. Go to the IE Web Controls <install> directory and edit the Build.bat to add a path to csc.exe (by, default csc.exe is found in C:\WINNT\Microsoft.NET\Framework\<framework version>). Execute Build.bat from the command prompt. A DLL file by the name of Microsoft.Web.UI.WebControls.dll is created in the <IE Web Controls>/build directory. Follow these steps:
- Copy the DLL file to the bin directory of the new application's folder or add a reference to it (VS.Net users).
- Register the control using the Register tag <%@ Register TagPrefix="ie" Namespace="Microsoft.Web.UI.WebControls" Assembly="Microsoft.Web.UI.WebControls"%>.
- Add the control using <ie:TreeView runat="server" ID= "Treeview1"></ie:treeview>.
- Inside the .cs file, use the namespace Microsoft.Web.UI.WebControls.
- Use the following code to programatically add the values to the Treeview control.
- Declare a variable as protected: Microsoft.Web.UI.WebControls.TreeView Treeview1;
/// <summary>
/// The following function adds two master levels and their
/// respective child nodes to the Treeview control
/// </summary>
public void PopulateTree()
{
TreeNode nodeProdDetail, nodeProdMaster;
nodeProdMaster = new TreeNode();
nodeProdMaster.Text = "Master1";
nodeProdMaster.ID = "1";
nodeProdDetail = new TreeNode();
nodeProdDetail.Text = "Detail1";
nodeProdDetail.ID = "1.1";
nodeProdDetail.NavigateUrl = "SomePage.aspx";
nodeProdMaster.Nodes.Add(nodeProdDetail);
Treeview1.Nodes.Add(nodeProdMaster);
nodeProdMaster = new TreeNode();
nodeProdMaster.Text = "Master2";
nodeProdMaster.ID = "2";
nodeProdDetail = new TreeNode();
nodeProdDetail.Text = "Detail2";
nodeProdDetail.ID = "2.1";
nodeProdDetail.NavigateUrl = "SomeOtherPage.aspx";
nodeProdMaster.Nodes.Add(nodeProdDetail);
Treeview1.Nodes.Add(nodeProdMaster);
}About the Author
He has been in Software field for last 6 years. Always been programming on Microsoft technologies. He started with VB, ASP and then gradually moved on to ASP.Net. C# is the preferred language for development. When not working on some piece of code he likes to play table tennis, cricket and football (world cup fever)Downloads
- TreeViewSample.zip - Project Demo
- TreeViewSample_src.zip - Source Code
No comments:
Post a Comment