ASP.Net TreeView with SiteMap is ignoring Node.Selected

北战南征 提交于 2020-01-14 05:34:46

问题


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


回答1:


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?




回答2:


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)

<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />
<asp:TreeView ID="TreeView1" runat="server" DataSourceID="SiteMapDataSource1" ShowExpandCollapse="false">
    <NodeStyle ForeColor="White" Font-Bold="true" NodeSpacing="5" HorizontalPadding="5" 
        ImageUrl="~/Images/Page.png" />
    <SelectedNodeStyle Font-Bold="true" NodeSpacing="5" HorizontalPadding="5" Font-Underline="true" 
        ImageUrl="~/Images/Page_Hot.png"/>
    <HoverNodeStyle ForeColor="Navy" />
</asp:TreeView>


来源:https://stackoverflow.com/questions/6313042/asp-net-treeview-with-sitemap-is-ignoring-node-selected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!