在Treeview ASP.NET(Web appln)中找到一个节点并突出显示它 [英] Find a node in Treeview ASP.NET ( Web appln) and Highlight it

查看:65
本文介绍了在Treeview ASP.NET(Web appln)中找到一个节点并突出显示它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

****这是一个ASP.NET Web应用程序,而不是WINDOWS FORMS *************
问题/问题:要在树形视图中的任何位置搜索子节点.我需要在文本框中键入节点名称,然后在树形视图中搜索节点名称,并在找到时突出显示该节点名称.我不怎么做我尝试了一段时间,但没有找到解决方案.所以,任何人都可以提出一些想法吗?
仅供参考:我创建了一个ASP.NET Web应用程序树形视图,以填充父节点和每个父节点的对应子节点.用户可以在树视图中添加任意数量的子节点.
下面给出的是我到目前为止为填充任意数量的孩子和孩子级别的树视图所做的代码.
文件后面的代码:

****THIS IS A ASP.NET Web APPLICATION NOT WINDOWS FORMS*************
Problem/ Question: To search a child node anywhere in the treeview. I need to type a Node name in the textbox and search the node name in the Treeview and highlight the node name on finding. I don’t how to do it. I tried for sometime but didn’t find the solutions.So, can any body give some idea ?
FYI : I have created a ASP.NET Web application treeview to populate Parent nodes and corresponding child nodes for each Parent Node. The user can add any number of child nodes in the treeview.
Given below is the code that I have done till now for populating the treeview for any number of childs and child levels.
Code behind file :

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)// Checks if the page is loaded first time
        {
            PopulateRootLevel(); // Populates the root or parent 
        }        
    }
public void PopulateRootLevel()
{
SqlConnection objConn = new SqlConnection(@"");
SqlCommand objCommand = new SqlCommand(@"select dp.CustomerName,dh.RowId,(select count(*) FROM DistributorHierarchy WHERE ParentID=dh.RowId) childnodecount FROM DistributorHierarchy dh INNER JOIN DistributorProfile dp ON dh.CustomerNumber = dp.CustomerNumber where dh.ParentID = 0 ", objConn);
SqlDataAdapter da = new SqlDataAdapter(objCommand);
DataTable dt = new DataTable();
da.Fill(dt);
PopulateNodes(dt, TreeView1.Nodes);                 
}
public void PopulateSubLevel(int ParentID, TreeNode parentNode)
{
SqlConnection objConn = new SqlConnection();
SqlCommand objCommand = new SqlCommand(@"select dp.CustomerName,dh.RowId,(select count(*) FROM DistributorHierarchy WHERE ParentID=dh.RowId) childnodecount  FROM DistributorHierarchy dh INNER JOIN DistributorProfile dp ON dh.CustomerNumber = dp.CustomerNumber where dh.ParentID=@ParentID", objConn);
objCommand.Parameters.Add("@ParentID", SqlDbType.Int).Value = ParentID;
SqlDataAdapter da = new SqlDataAdapter(objCommand);
DataTable dt = new DataTable();
da.Fill(dt);
PopulateNodes(dt, parentNode.ChildNodes);
}
public void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
{
PopulateSubLevel(Int32.Parse(e.Node.Value), e.Node);
}
public void PopulateNodes(DataTable dt, TreeNodeCollection nodes)
{
        foreach (DataRow dr in dt.Rows)
        {
            TreeNode tn = new TreeNode();
            tn.Text = dr["CustomerName"].ToString();
            tn.Value = dr["RowId"].ToString();
            nodes.Add(tn);
            tn.PopulateOnDemand = ((int)(dr["childnodecount"]) > 0);
        }
}    

}

推荐答案

最快的方法是

quickest way is a

///Pass into this the base Node.
TreeNode recursiveFind(TreeNode Node, obj yourCritera){

TreeNode HoldTN = null;
 foreach(TreeNode tn in Node.Children)
{
      if([YOUR SEARCH CRITERA]){
         return tn;
      }
      else{
          HoldT = recursive(tn);
          if(HoldTN!=null){return HoldTN;}
      }
      
}
return null;

}




至于突出显示,我相信您可以使用节点背景"进行设置...


上面的方法可以正常工作,但是当您开始获得带有子节点的大量节点时(相对于分支数量少而节点数量多的节点而言),导航节点将变得缓慢.

可能需要考虑一种寻址方法:
对于
的树

a {b {d,e} c {f,g}}

f的地址为0.1.0;当您想执行更复杂的导航技巧时,此方法非常有用,并且在您想要获取节点时也简化了代码:




As For Highlighting, I believe you can set this using the Node Background...


The above will work, however navigating the nodes will become sluggish when you start getting a LARGE number of nodes with children(as opposed to a low number of Branchings to a high number of nodes)

may want to consider an addressing approach:
for a tree that is


a{b{d,e}c{f,g}}

f would have the address 0.1.0; this works very well when you want to do more complicated navigational tricks, and also simplifies the code when you want to get a node:

GetNode(NodeAddress Address){}//recursive get


以下是我用来突出显示树状视图中所选内容的代码

Following is code used by me to highlight selected now in tree view

#region code done for selecting node by pranay rana
                    string filename = this.Request.FilePath.ToString().Substring(this.Request.FilePath.ToString().LastIndexOf(@"/") + 1);

                    //MyMembershipProvider _MyMembershipProvider = new MyMembershipProvider();
                    DataSet menu = _MyMembershipProvider.GetMenuByFileName(filename);
                    if (menu.Tables[0].Rows.Count == 1)
                    {
                        string valpath = menu.Tables[0].Rows[0]["Mnu_Parent_Name"].ToString() + @"/" +
                                                menu.Tables[0].Rows[0]["Mnu_Pkey"].ToString();
                        if (MenuTree.FindNode(valpath) != null)
                        {
                            MenuTree.FindNode(valpath).Selected = true;
                        }
                    }
                  #endregion code done for selecting node by pranay rana


这篇关于在Treeview ASP.NET(Web appln)中找到一个节点并突出显示它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆