C# 如何防止我的树视图折叠?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/651144/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 11:51:15  来源:igfitidea点击:

How do I prevent my treeview from collapsing?

c#.netasp.nettreeview

提问by Kartik

I am using ASP.NET with C# 2.0 and Visual Studio 2005. I am using a Master page and content pages. I have a treeview menu in the master page and when a user selects any menu item I redirect to that content page.

我在 C# 2.0 和 Visual Studio 2005 中使用 ASP.NET。我正在使用母版页和内容页。我在母版页中有一个树视图菜单,当用户选择任何菜单项时,我重定向到该内容页面。

My problem is that after a user navigates to the content page all the treenodes refresh and the structure is collapsed. I want the selected treenode to stay expanded.

我的问题是,在用户导航到内容页面后,所有树节点都会刷新并且结构已折叠。我希望选定的树节点保持展开状态。

Can anybody help me out?

有人可以帮我吗?

回答by Kartik

When you refresh the treeview you want to call treeView1.ExpandAll();

当您刷新要调用 treeView1.ExpandAll(); 的树视图时;

Also add an event for the BeforeCollapse and set the event's Cancel property to true, to prevent the user from collapsing your treenodes.

还要为 BeforeCollapse 添加一个事件,并将事件的 Cancel 属性设置为 true,以防止用户折叠您的树节点。

private void treeView1_BeforeCollapse(object sender, TreeViewCancelEventArgs e)
{
    e.Cancel = true;
}

Hope this helps.

希望这可以帮助。

-jeremy

-杰里米

回答by hunter

Try using the OnTreeNodeDataBound event and the treeView.SelectedNode property

尝试使用 OnTreeNodeDataBound 事件和 treeView.SelectedNode 属性

Also, might want to check how/ when you're binding your TreeView to it's DataSource. You might be rebinding it on IsPostBack which will re-render the tree.

此外,可能想要检查如何/何时将 TreeView 绑定到它的 DataSource。您可能会在 IsPostBack 上重新绑定它,这将重新渲染树。

The TreeView should maintain its nodes on PostBack.

TreeView 应在 PostBack 上维护其节点。

回答by Shaun Humphries

Even though you are using a Master page, once the user navigates to the content page it is rendered as a new/different page. Because of the Master page the same treeview is loaded but not the same instance. You will need to store and load what nodes were expanded.

即使您使用的是母版页,一旦用户导航到内容页,它就会呈现为一个新的/不同的页面。由于母版页,加载了相同的树视图但不是相同的实例。您需要存储和加载扩展的节点。

回答by Cerebrus

This is a common enough problem that is automatically handled by ASP.NET if you use a SiteMapDataSource control as the datasource for your TreeView. In this case, you haven't mentioned what the Datasource of your TreeView is.

如果您使用 SiteMapDataSource 控件作为 TreeView 的数据源,这是一个很常见的问题,ASP.NET 会自动处理该问题。在这种情况下,您没有提到 TreeView 的数据源是什么。

You also haven't mentioned if the TreeView contains links (the NavigateUrlproperty is set) or Text items that postback for each click. If it is the former, then as far as I know, you are out of luck! This is because none of the Selection events are raised for TreeNodes which have their NavigateUrlset. They just function as regular hyperlinks.

您还没有提到 TreeView 是否包含链接(NavigateUrl属性已设置)或每次单击回发的文本项。如果是前者,那么据我所知,你倒霉了!这是因为没有为NavigateUrl设置了它们的 TreeNode 引发任何 Selection 事件。它们只是用作常规超链接。

If however, it is the latter, then you can try out the following steps :

但是,如果是后者,那么您可以尝试以下步骤:

a.Handle the SelectedNodeChangedevent of the TreeView. In this event handler, retrieve the current value of the SelectedNode.ValuePathproperty and store it in ViewState/Session. Use the Valueof the of the SelectedNode to conditionally redirect the page to URL mapped to it.

一种。处理SelectedNodeChangedTreeView的事件。在此事件处理程序中,检索SelectedNode.ValuePath属性的当前值并将其存储在 ViewState/Session 中。使用ValueSelectedNode 的 有条件地将页面重定向到映射到它的 URL。

Something like the following:

类似于以下内容:

protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e) 
{ 
  TreeNode selNode = TreeView1.SelectedNode; 
  string pathToNode = selNode.ValuePath; 
  Session.Add("SelPath", pathToNode); 

  switch (selNode.Value) 
  { 
  //Redirect to URL accordingly. 
  } 
} 

b.On subsequent load of the Master page (the page to which you redirected), retrieve the value of the ValuePathset earlier and find the previously Selected node and Expandit.

在母版页面(您重定向到的页面)的后续加载中,检索ValuePath先前设置的值并找到之前的 Selected 节点及其Expand

Something like the following:

类似于以下内容:

protected void Page_Load(object sender, EventArgs e) 
{ 
  if (Page.IsPostBack)
  { 
    string pathToNode = (string)Session("SelPath"); 
    Session.Remove("SelPath"); 
    TreeNode selNode = TreeView1.FindNode(pathToNode); 
    if (selNode != null) 
    { 
      selNode.Expand(); 
    } 
  } 
} 

Note that I haven't had an opportunity to test the code so this is mostly hypothetical.

请注意,我没有机会测试代码,因此这主要是假设性的。