fixed code
private void LoadTreeNodes()
{
tvModules = new TreeNode
{
Children = new List<TreeNode>();
};
string sql = "";
DataSet ds = new DataSet();
DataSet ds1 = new DataSet();
int i = 0;
try
{
sql = "select * from tblWebModule where parentmenucode='00'";
ds = obj.ExecuteQuery(sql);
if (ds.Tables[0].Rows.Count > 0)
{
foreach (DataRow dr in ds.Tables[0].Rows)
{
TreeNode TN = new TreeNode();
TN.Text = dr["MenuName"].ToString();
TN.Value = dr["ChildMenuCode"].ToString();
TN.Children = new List<TreeNode>();
sql = $"select * from tblWebModule where parentmenucode='{dr["ChildMenuCode"].ToString()}'";
ds1 = obj.ExecuteQuery(sql);
foreach (DataRow rw in ds1.Tables[0].Rows)
{
TreeNode CN = new TreeNode();
CN.Text = rw["MenuName"].ToString();
CN.Value = rw["ChildMenuCode"].ToString();
TN.Children.Add(CN);
}
tvModules.Children.Add(TN);
}
}
}
catch (Exception ex)
{
throw ex;
}
}
sample razor page (requires styling for button.collaspe):
<ul class="menu">
@foreach (var node in tvModules.Children)
{
<li>
<button class="collaspe" type="button">-</button>
<input type="checkbox" name="checklist" value="@node.Value">
@node.Text
<ul>
@foreach (var child in node.Children)
{
<li>
<input type="checkbox" name="checklist" value="child.Value">
@child.Text
</li>
}
</ul>
</li>
}
</ul>
@section Scripts {
<script>
$('ul.menu button.collaspe').click(function () {
const open = this.innerText == "-";
this.innerText = open ? "+" : "-";
$(this).closest("li").find("ul")[open ? "hide" : "show"]("slow");
});
</script>
}