1

我需要创建一个带有复选框的树视图控件。但是,我的树视图需要为父级和子级设置复选框。在我的情况下,业务逻辑是用户可能选择“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;
            }
        }
    }
}
4

1 回答 1

2

ShowCheckBoxes物业并没有真正涵盖这种情况。

但是,您可以在绑定树后更改每个单独节点的ShowCheckBox属性。该页面包含一些示例代码,甚至可以根据节点的深度执行此操作,这看起来就像您所追求的那样。

于 2012-10-01T13:19:19.383 回答