我需要创建一个带有复选框的树视图控件。但是,我的treeview需要有父和子的复选框。 在我的情况下,业务逻辑是用户可以选择“MS Office”但可能不想要“Microsoft”。
我遇到了asp.net TreeView: how to use CheckBox in TreeView这有助于,但是,我似乎无法为“Microsoft”创建一个复选框。 “Adobe”节点。我该怎么做呢?
html代码为:
<form id="form1" runat="server">
<div>
<h2 style="color:Green">TreeView: ShowCheckBoxes</h2>
<asp:Label
ID="Label1"
runat="server"
Text="Checked Favorite"
Font-Size="Medium"
ForeColor="SeaGreen"
Font-Italic="true"
>
</asp:Label>
<br /><br />
<asp:TreeView
ID="TreeView1"
runat="server"
DataSourceID="XmlDataSource1"
>
<DataBindings>
<asp:TreeNodeBinding DataMember="Products" Text="Products" />
<asp:TreeNodeBinding DataMember="Type" TextField="Name" />
<asp:TreeNodeBinding DataMember="Product" TextField="Name" ValueField="ID" />
</DataBindings>
</asp:TreeView>
<br />
<asp:XmlDataSource
ID="XmlDataSource1"
runat="server"
>
<Data>
<Products>
<Type Name="Microsoft">
<Product ID="1" Name="MS Office"/>
<Product ID="2" Name="ASP.NET"/>
</Type>
<Type Name="Adobe">
<Product ID="1" Name="ColdFusion"/>
<Product ID="2" Name="Flex"/>
<Product ID="2" Name="Photoshop"/>
</Type>
</Products>
</Data>
</asp:XmlDataSource>
<asp:Button ID="Button1" runat="server" Text="Submit Favorite" OnClick="Button1_Click" />
</div>
</form>
背后的代码是:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace TreeviewCheckboxSample
{
public partial class Treeview : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "Your favorite:";
if (TreeView1.CheckedNodes.Count > 0)
{
foreach (TreeNode node in TreeView1.CheckedNodes)
{
Label1.Text += "<br />" + node.Text.ToString();
}
}
else
{
Label1.Text = "Checked before submit.";
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
TreeView1.ShowCheckBoxes = TreeNodeTypes.Leaf;
TreeView1.ForeColor = System.Drawing.Color.Black;
TreeView1.BackColor = System.Drawing.Color.White;
TreeView1.Width = 200;
Button1.Font.Bold = true;
Button1.ForeColor = System.Drawing.Color.Crimson;
}
}
}
}
答案 0 :(得分:2)
ShowCheckBoxes
property并未真正涵盖此案例。
但是,您可以在绑定树后更改每个节点上的ShowCheckBox
property。该页面包含一些示例代码,甚至可以根据节点的深度来执行此操作,这看起来就像您所追求的那样。