aspx code:
<div>
<asp:TreeView ID="TreeView1" runat="server" ImageSet="Arrows" ShowLines="True">
<ParentNodeStyle Font-Bold="False" />
<SelectedNodeStyle Font-Underline="True" ForeColor="#5555DD" HorizontalPadding="0px" VerticalPadding="0px" />
<NodeStyle Font-Names="Verdana" Font-Size="8pt" ForeColor="Black" HorizontalPadding="5px" NodeSpacing="0px" VerticalPadding="0px" />
<HoverNodeStyle Font-Underline="True" ForeColor="#5555DD" />
</asp:TreeView>
</div>
aspx.cs code
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
fill_Tree_WithConnection();
}
void fill_Tree_WithConnection()
{
SqlConnection SqlCon = new SqlConnection(@"Data Source=SQLEXPRESS;Initial Catalog=mydb;User ID=sa;pwd=sa");
SqlCon.Open();
SqlCommand SqlCmd = new SqlCommand("Select * from ParentTable", SqlCon);
SqlDataAdapter da = new SqlDataAdapter(SqlCmd);
DataSet myDataset = new DataSet();
da.Fill(myDataset);
string[,] ParentNode = new string[100, 2];
int count = 0;
for (int k = 0; k < myDataset.Tables[0].Rows.Count; k++)
{
ParentNode[count, 0] = myDataset.Tables[0].Rows[k]["ParentID"].ToString();
ParentNode[count++, 1] = myDataset.Tables[0].Rows[k]["ParentContent"].ToString();
}
for (int loop = 0; loop < count; loop++)
{
myDataset = new DataSet();
TreeNode root = new TreeNode();
root.Text = ParentNode[loop, 1];
root.Target = "_blank";
SqlConnection SqlCon1 = new SqlConnection(@"Data Source=SQLEXPRESS;Initial Catalog=mydb;User ID=sa;pwd=sa");
SqlCon1.Open();
SqlCommand SqlCmd1 = new SqlCommand("Select * from ChildTable where ParentID=" + ParentNode[loop, 0], SqlCon1);
SqlDataAdapter da1 = new SqlDataAdapter(SqlCmd1);
da1.Fill(myDataset);
for (int k = 0; k < myDataset.Tables[0].Rows.Count; k++)
{
TreeNode child = new TreeNode();
child.Text = myDataset.Tables[0].Rows[k]["ChildContent"].ToString();
child.Target = "_blank";
root.ChildNodes.Add(child);
}
TreeView1.Nodes.Add(root);
}
TreeView1.CollapseAll();
}
Output will be like this
Tag: treeview in .net, treeview in asp.net, treeview in asp.net 2.0, treeview in asp.net 3.5, treeview in asp.net binding, treeview in asp.net example, treeview in asp.net tutorial
No comments:
Post a Comment