带有 SiteMap 的 ASP.Net TreeView 忽略 Node.Selected
我创建了一个绑定到 SiteMap 的 TreeView。效果很好。
<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />
<asp:TreeView ID="TreeView1" runat="server" DataSourceID="SiteMapDataSource1">
</asp:TreeView>
现在我想更改所选节点的外观。
<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />
<asp:TreeView ID="TreeView1" runat="server" DataSourceID="SiteMapDataSource1">
<NodeStyle ImageUrl="~/Images/Page.png" />
<SelectedNodeStyle ImageUrl="~/Images/Page_Hot.png" />
</asp:TreeView>
问题是,当前页面不会在树上自动选择(为什么是 MSFT,为什么?)。这不是世界末日。因此,我在后面创建了一些代码,如下所示:
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
return;
TreeView1.TreeNodeDataBound += new TreeNodeEventHandler(TreeView1_TreeNodeDataBound);
}
void TreeView1_TreeNodeDataBound(object sender, TreeNodeEventArgs e)
{
var _CurrentUrl = Request.Url.AbsolutePath;
e.Node.Selected = (e.Node.NavigateUrl == _CurrentUrl);
}
但仍然没有选择节点。我的直觉告诉我这是错误的事件。
有什么帮助吗?
谢谢//杰瑞
I create have a TreeView bound to a SiteMap. It works great.
<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />
<asp:TreeView ID="TreeView1" runat="server" DataSourceID="SiteMapDataSource1">
</asp:TreeView>
Now I want to change the way the selected node looks.
<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />
<asp:TreeView ID="TreeView1" runat="server" DataSourceID="SiteMapDataSource1">
<NodeStyle ImageUrl="~/Images/Page.png" />
<SelectedNodeStyle ImageUrl="~/Images/Page_Hot.png" />
</asp:TreeView>
The thing is, the current page is not automatically selected on the tree (why MSFT, why?). This is not the end of the world. So, I created a little code behind like this:
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
return;
TreeView1.TreeNodeDataBound += new TreeNodeEventHandler(TreeView1_TreeNodeDataBound);
}
void TreeView1_TreeNodeDataBound(object sender, TreeNodeEventArgs e)
{
var _CurrentUrl = Request.Url.AbsolutePath;
e.Node.Selected = (e.Node.NavigateUrl == _CurrentUrl);
}
But still the node is not selected. My gut tells me it's the wrong event.
Any help?
Thanks // Jerry
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
AbsolutePath 与 NavigateUrl 可能值得怀疑,一个是相对路径(NavigateUrl),另一个顾名思义,是绝对路径。如果您在调试模式下运行,您是否可以看到这两个值确实相同?
The AbsolutePath vs the NavigateUrl might be suspect, one is a relative path (NavigateUrl) and the other, as the name implies, is an absolute path. If you run in debug mode can you see that the two values are indeed the same?
嗯,这真的很烦人。我想我的问题的“技术”答案是它一直有效。然而,真正的问题是 SelectedNodeStyle 中的 ImageUrl 似乎被忽略了。我将粘贴更多的解决方案,以便您可以看到我所拥有的内容,但无论我尝试什么,SelectedNodeStyle 中的 ImageUrl 都会被忽略。
(PS:.Net4)
Well, this is really annoying. I suppose the "technical" answer to my question is that it worked all along. However, the real issue is that ImageUrl in the SelectedNodeStyle appears to be ignored. I am going to paste a little more of my solution so you can see what I have, but no matter what I try, ImageUrl in the SelectedNodeStyle is ignored.
(PS: .Net4)