Reputation: 1067
I have added a treeview to my system. I could manage to show the content of the treeview. But the output is not same as I expected. Here I have added an image of the output. Please take look at it.
aspx code
<asp:TreeView ID="TreeView1" runat="server" ImageSet="XPFileExplorer"
NodeIndent="15">
<HoverNodeStyle Font-Underline="True" ForeColor="#6666AA" />
<NodeStyle Font-Names="Tahoma" Font-Size="8pt" ForeColor="Black" HorizontalPadding="2px"
NodeSpacing="0px" VerticalPadding="10px"></NodeStyle>
<ParentNodeStyle Font-Bold="False" />
<SelectedNodeStyle BackColor="#B5B5B5" Font-Underline="False" HorizontalPadding="0px"
VerticalPadding="2px" />
</asp:TreeView>
C# code
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dt=getDataObj.treeSketch();
PopulateTreeView(dt, 0, null);
}
}
private void PopulateTreeView(DataTable dsParent, int parentId, TreeNode treeNode)
{
foreach (DataRow row in dsParent.Rows)
{
TreeNode child = new TreeNode
{
Text = row["FUNCTION_NAME"].ToString(),
Value = row["FUNCTION_CODE"].ToString()
};
if (parentId == 0)
{
TreeView1.Nodes.Add(child);
dt = getDataObj.loadTree(child);
PopulateTreeView(dt, int.Parse(child.Value), child);
}
else
{
treeNode.ChildNodes.Add(child);
}
}
}
In here nodes in capital letters are main node and other are child nodes. Why doesn't it appear like a treeview? How to make it work?
Query
public DataTable loadTree(TreeNode child)
{
string strQuery = @"select FUNCTION_CODE,USER_TYPE from [WEB_FUNCTIONACCESS]
where FUNCTION_CODE = '"+ child +"' ";
return SqlHelper.ExecuteDataset(strConnStringAppeal, CommandType.Text, strQuery).Tables[0];
}
Upvotes: 0
Views: 316
Reputation: 6455
The problem is actually pretty obvious.
if (parentId == 0)
{
TreeView1.Nodes.Add(child);
dt = getDataObj.loadTree(child);
PopulateTreeView(dt, int.Parse(child.Value), child);
}
else
{
treeNode.ChildNodes.Add(child);
}
So the condition is that if parentId is 0 then add a root level node, otherwise add at the child level.
Now when you call this method, PopulateTreeView(dt, 0, null)
, the parentId
parameter is always 0.
Update:
I see you have a recursive call to the method, PopulateTreeView
, given the child value. int.Parse(child.Value)
does this always evaluate as 0?
The problem is you used the reference of child
which gives you a TreeNode object. What you really should be using is child.Value
which gives you a string value. Then you can append it to the query string there.
Upvotes: 1