从CheckboxList到Treeview

时间:2014-12-30 09:55:31

标签: asp.net treeview checkboxlist

我有一个需要更改为树视图的复选框列表。我这样做但现在我想要的是检索在树视图(onclick)上检查的项目,就像在复选框列表上完成一样。 在复选框列表中,我可以通过cblist.Items [k]获取项目。选择
我怎么能用树视图做到这一点?

另一个问题,我怎么算他们?

   private void Tree()
{
    try
    {
        Dados d = new Dados();
        DataTable dtTree = d.getTreeView("%");

        DataSet ds = new DataSet("table");
        ds.Tables.Add(dtTree);

        ds.Relations.Add("ChildRows", ds.Tables[0].Columns["ID"],
              ds.Tables[0].Columns["ParentID"]);

        foreach (DataRow level1DataRow in ds.Tables[0].Rows)
        {
            if (string.IsNullOrEmpty(level1DataRow["ParentID"].ToString()))
            {
                TreeNode parentTreeNode = new TreeNode();
                parentTreeNode.Text = "<span style=\"color:" + level1DataRow["Color"].ToString() + "\">" + level1DataRow["Description"].ToString() + "</span>";
                GetChildRows(level1DataRow, parentTreeNode);
                Treeview1.Nodes.Add(parentTreeNode);

            }
        }
    }
    catch (Exception ex)
    {
        //something
    }
}

private void GetChildRows(DataRow dataRow, TreeNode treeNode)
{
    DataRow[] childRows = dataRow.GetChildRows("ChildRows");
    foreach (DataRow row in childRows)
    {
        TreeNode childTreeNode = new TreeNode();
        childTreeNode.Text = "<span style=\"color:" + row["Color"].ToString() +"\">" + row["Description"].ToString() +"</span>"; 
        childTreeNode.Value = row["LocalID"].ToString();
        treeNode.ChildNodes.Add(childTreeNode);


        if (row.GetChildRows("ChildRows").Length > 0)
        {
            GetChildRows(row, childTreeNode);
        }
    }
}

和复选框列表

   private void cbox()
{
    try
    {
        Dados d = new Dados();
        string instalacao = "";
        DataTable dtList = d.getLocal("%");

        DataView dataView = new DataView(dtList);
        dataView.Sort = " Color asc, Description asc";

        cblist.DataSource = dataView;
        cblist.DataTextField = "Description";
        cblist.DataValueField = "LocalID";
        cblist.DataBind();
    }
    catch (Exception ex)
    {
        //something
    }
}

1 个答案:

答案 0 :(得分:0)

使用TreeView.SelectedNode

    <%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

    void Select_Change(Object sender, EventArgs e)
    {

        Message.Text = "You selected: " + LinksTreeView.SelectedNode.Text;

    }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >

    <head runat="server">
    <title>TreeView SelectedNodeStyle Example</title>
</head>
<body>
        <form id="form1" runat="server">

            <h3>TreeView SelectedNodeStyle Example</h3>

            <asp:TreeView id="LinksTreeView"
                Font-Names= "Arial"
                ForeColor="Blue"
                SelectedNodeStyle-ForeColor="Green"
                SelectedNodeStyle-VerticalPadding="0"
                OnSelectedNodeChanged="Select_Change"   
                runat="server">

                <LevelStyles>

                    <asp:TreeNodeStyle ChildNodesPadding="10" 
                        Font-Bold="true" 
                        Font-Size="12pt" 
                        ForeColor="DarkGreen"/>
                    <asp:TreeNodeStyle ChildNodesPadding="5" 
                        Font-Bold="true" 
                        Font-Size="10pt"/>
                    <asp:TreeNodeStyle ChildNodesPadding="5" 
                        Font-UnderLine="true" 
                        Font-Size="10pt"/>
                    <asp:TreeNodeStyle ChildNodesPadding="10" 
                        Font-Size="8pt"/>

                </LevelStyles>

                <Nodes>

                    <asp:TreeNode Text="Table of Contents"
                        SelectAction="None">

                        <asp:TreeNode Text="Chapter One">

                            <asp:TreeNode Text="Section 1.0">

                                <asp:TreeNode Text="Topic 1.0.1"/>
                                <asp:TreeNode Text="Topic 1.0.2"/>
                                <asp:TreeNode Text="Topic 1.0.3"/>

                            </asp:TreeNode>

                            <asp:TreeNode Text="Section 1.1">

                                <asp:TreeNode Text="Topic 1.1.1"/>
                                <asp:TreeNode Text="Topic 1.1.2"/>
                                <asp:TreeNode Text="Topic 1.1.3"/>
                                <asp:TreeNode Text="Topic 1.1.4"/>

                            </asp:TreeNode>

                        </asp:TreeNode>

                        <asp:TreeNode Text="Chapter Two">

                            <asp:TreeNode Text="Section 2.0">

                                <asp:TreeNode Text="Topic 2.0.1"/>
                                <asp:TreeNode Text="Topic 2.0.2"/>

                            </asp:TreeNode>

                        </asp:TreeNode>

                    </asp:TreeNode>
                    <asp:TreeNode Text="Appendix A" />
                    <asp:TreeNode Text="Appendix B" />
                    <asp:TreeNode Text="Appendix C" />

                </Nodes>

            </asp:TreeView>

            <br /><br />

            <asp:Label id="Message" runat="server"/>

        </form>
    </body>
</html>