客户网站需要显示部门及员工列表,这个用ASP.net中的TreeView树形列表控件再合适不过了。但是因操作需要,客户需要每个客户名字前面的复选框换成单选按钮。如何做到呢?下面的代码可以轻松搞定。
前台页面代码:
<asp:TreeView runat="server" ID="tvAi" Font-Bold="true" ShowLines="true" EnableClientScript="true"
ShowExpandCollapse="true" ShowCheckBoxes="All"
SelectedNodeStyle-ForeColor="Azure" >
</asp:TreeView>
后台数据绑定代码:
private void BindTreeview()
{
var pk = this.GetTreeViewNodes(); // this is simply to get the records to bind into the GridView
tvAi.Nodes.Clear();
int redindex = 1;
foreach (var parentNode in pk)
{
TreeNode master = new TreeNode(parentNode.Value.ToString(), parentNode.Key.ToString());
master.ShowCheckBox = false;
tvAi.Nodes.Add(master);
var sk = this.GetSubnodesOfTheParentNodes(parentNode.Key);
foreach (var chileNode in sk)
{
TreeNode child = new TreeNode(chileNode.Value.ToString(), chileNode.Key.ToString());
child.ShowCheckBox = false;
child.Text = "<input type=radio name=rdoC" + redindex.ToString() + " value ="+chileNode.Value.ToString()+" />" + child.Text;
master.ChildNodes.Add(child);
}
redindex++;
}
}