本文介绍了如何用数据库查询填充ASP:treeview,只有子填充? C#(WEB)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何填充asp:treeview只有子数据库填充数据库查询,例如从数据库中选择Database_Name,其中user_id ='1'并在子树视图中显示这些数据库。这是图像

我希望数据库保持相同并且打击子数据库名称,只有前3个记录替换为数据库名称只是显示,请告诉我,我怎么能这样做,谢谢。我搜索谷歌,但我找不到解决方案。这是我的数据库图片



我尝试过:



尝试:

How can i populate asp:treeview only child populate with database query like "Select Database_Name from database where user_id='1' and show these database in child treeview.Here is imageimage
I want "Database" remain same and blow child "Database Name",,only 1st 3 records replace with "Database Name" shown just,Kindly tell me how can i do this,Thanks.I search on google but i can't find solution.Here is my db pic image

What I have tried:

Try :

  SqlConnection cnn=new SqlConnection("Data Source=HAMEED_KHAN\\SQLEXPRESS;Initial Catalog=db_compiler;Integrated Security=True");

protected void Page_Load(object sender, EventArgs e)
{
}

protected void PopulateTreeView(object sender, EventArgs e)
{
 SqlCommand cmd=new SqlCommand("SELECT Database_Name FROM  Create_db",cnn);
DataTable dt=new DataTable();
Master.FindControl("TreeView1");
    TreeView tv = (TreeView)Master.FindControl("TreeView1");
TreeNodeCollection parentNode = tv.Nodes;
 SqlDataAdapter da=new SqlDataAdapter(cmd);
 da.Fill(dt);
 PopulateTreeView(parentNode,0, dt);// error come at parent node
} 



这是我的aspx代码:


Here is my aspx code :

<asp:TreeView ID="TreeView1" runat="server" ImageSet="Arrows"
                    Font-Size="Larger" onselectednodechanged="TreeView1_SelectedNodeChanged">
                    <HoverNodeStyle Font-Underline="True" ForeColor="#5555DD" />
                    <Nodes>
<asp:TreeNode Text="Database" Value="Database">
<asp:TreeNode  Text="Database Name" Value="Database Name"></asp:TreeNode>
<asp:TreeNode Text="Database Name" Value="Database Name"></asp:TreeNode>
<asp:TreeNode Text="Database Name" Value="Database Name"></asp:TreeNode>
<asp:TreeNode Text="More" Value="Database Name"></asp:TreeNode>
</asp:TreeNode>
 </Nodes>
 <NodeStyle Font-Names="Verdana" Font-Size="8pt" ForeColor="Black"
                        HorizontalPadding="5px" NodeSpacing="0px" VerticalPadding="0px" />
 <ParentNodeStyle Font-Bold="False" />
 <SelectedNodeStyle Font-Underline="True" ForeColor="#5555DD"
                        HorizontalPadding="0px" VerticalPadding="0px" />
</asp:TreeView>

推荐答案

protected void Page_Load(object sender, EventArgs e)
       {
           if (!Page.IsPostBack)
           {

               SqlCommand cmd = new SqlCommand("SELECT Database_Name FROM  Create_db", cnn);
               DataTable dt = new DataTable();
               Master.FindControl("TreeView1");
               TreeView tv = (TreeView)Master.FindControl("TreeView1");
               SqlDataAdapter da = new SqlDataAdapter(cmd);
               cnn.Open();
               da.Fill(dt);
               cnn.Close();

               var parentNode = new TreeNode("Database");
               if (dt.Rows.Count > 0)
               {
                   foreach (DataRow row in dt.Rows)
                   {
                       string value = row["Database_Name"] + "";
                       parentNode.ChildNodes.Add(new TreeNode(value));
                   }
               }
               tv.Nodes.Add(parentNode);
           }
       }


这篇关于如何用数据库查询填充ASP:treeview,只有子填充? C#(WEB)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 04:31