本文介绍了Treelistview在用户控制中不可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设计了Treelistview,但没有出现在用户控制中



我尝试过:



I have designed Treelistview ,but not appearing in user control

What I have tried:

private void InitializeData()
        {
            SqlDataAdapter adp = new SqlDataAdapter("select acdesc,acno,speactype,acremarks from chartaccount where ParentAcNo is null order by AcNo", cn);
            DataTable dt = new DataTable();
            adp.Fill(dt);

            foreach (DataRow dataRow in dt.Rows)
            {
                Node parent = new Node(dataRow["acdesc"].ToString(), dataRow["acno"].ToString(), dataRow["speactype"].ToString(), dataRow["acremarks"].ToString());
                string acno = dataRow["acno"].ToString();
                if (acno != "" && acno != null)
                {
                    SqlDataAdapter adp1 = new SqlDataAdapter("select acdesc,acno,speactype,acremarks from chartaccount where ParentAcNo ='" + acno + "'", cn);
                    DataTable dt1 = new DataTable();
                    adp1.Fill(dt1);
                    foreach (DataRow dataRow1 in dt1.Rows)
                    {
                        parent.Children.Add(new Node(dataRow1["acdesc"].ToString(), dataRow1["acno"].ToString(), dataRow1["speactype"].ToString(), dataRow1["acremarks"].ToString()));

                    }

                }
                data.Add(parent);
            }
            //foreach (ListViewItem item in treeListView.Items)
            //{
            //    item.BackColor = Color.AntiqueWhite;
            //}
            for (int i = 0; i < treeListView.Items.Count; i++)
            {
                treeListView.Items[i].BackColor = Color.YellowGreen;
                //for (int j = 0; j < listView2.Items[i].SubItems.Count; j++)
                //{
                //    listView2.Items[i].SubItems[j].BackColor = Color.Black; ;
                //}
            }

        }

推荐答案

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="TreeViewControl.ascx.cs" Inherits="TreeViewControl" %>

<asp:TreeView ID="TreeView1" runat="server" ImageSet="XPFileExplorer" NodeIndent="15">
    <HoverNodeStyle Font-Underline="True" ForeColor="#6666AA" />
    <NodeStyle Font-Names="Tahoma" Font-Size="8pt" ForeColor="Black" HorizontalPadding="2px"

        NodeSpacing="0px" VerticalPadding="2px"></NodeStyle>
    <ParentNodeStyle Font-Bold="False" />
    <SelectedNodeStyle BackColor="#B5B5B5" Font-Underline="False" HorizontalPadding="0px"

        VerticalPadding="0px" />
</asp:TreeView>







树控代码文件:






Tree Control Code File:

public partial class TreeViewControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            DataTable dt = this.GetData("SELECT Id, Name FROM VehicleTypes");
            this.PopulateTreeView(dt, 0, null);
        }
    }

    private void PopulateTreeView(DataTable dtParent, int parentId, TreeNode treeNode)
    {
        foreach (DataRow row in dtParent.Rows)
        {
            TreeNode child = new TreeNode
            {
                Text = row["Name"].ToString(),
                Value = row["Id"].ToString()
            };
            if (parentId == 0)
            {
                TreeView1.Nodes.Add(child);
                DataTable dtChild = this.GetData("SELECT Id, Name FROM VehicleSubTypes WHERE VehicleTypeId = " + child.Value);
                PopulateTreeView(dtChild, int.Parse(child.Value), child);
            }
            else
            {
                treeNode.ChildNodes.Add(child);
            }
        }
    }

    private DataTable GetData(string query)
    {
        DataTable dt = new DataTable();
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand(query))
            {
                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.Connection = con;
                    sda.SelectCommand = cmd;
                    sda.Fill(dt);
                }
            }
            return dt;
        }
    }
}





我们要使用UserControl的父页面:





Parent Page where we want to use the UserControl:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CS.aspx.cs" Inherits="CS" %>
<%@ Register TagPrefix="My" TagName="UserInfoBoxControl" Src="~/TreeViewControl.ascx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        body
        {
            font-family: Arial;
            font-size: 10pt;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
<h3>
    Vehicle Details</h3>
<hr />
        <My:UserInfoBoxControl runat="server" ID="MyUserInfoBoxControl" />
    </form>
</body>
</html>


这篇关于Treelistview在用户控制中不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 21:54