Hello everyone. I have to re-post this as I have yet to receive any response. I hope someone will help me this time. Thanks.
I am experimenting navigation using TreeView postback. This is how it goes?
Upon successful login in a login page, a main page is displayed. The main page uses a Master page that contains a TreeView bound to a sitemap.
In the main page, upon clicking a TreeView node, I want to programmatically navigate to a different page by constructing on-the-fly a URL and then redirecting to it (the URL). This is where I got stuck at.
The code is as follows:
Web.sitemap:
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
<siteMapNode title="Main" description="Main">
<siteMapNode title="Submit" description="Submit ECS"/>
<siteMapNode title="Verify" description="Verify ECS" />
<siteMapNode title="Approve" description="Approve ECS" />
</siteMap>
Main.master:
<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />
<asp:TreeView ID="TreeView1" runat="server" DataSourceID="SiteMapDataSource1" OnSelectedNodeChanged="TreeView1_SelectedNodeChanged" ShowLines="True">
</asp:TreeView>
Main.master.c:
protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
// Get name of current user
string currentUser = Membership.GetUser().UserName.ToString();
// Construct URL on-the-fly and redirect to it
switch (TreeView1.SelectedNode.Text)
{
case "Main":
Response.Redirect("Main.aspx");
break;
case "Submit":
Response.Redirect("SubmitECS.aspx?User=" + currentUser);
break;
case "Verify":
Response.Redirect("VerifyECS.aspx?User=" + currentUser);
break;
case "Approve":
Response.Redirect("ApproveECS.aspx?User=" + currentUser);
break;
}
}
Main.aspx:
<%@ Page Language="C#" MasterPageFile="Main.master" AutoEventWireup="true" CodeFile="Main.aspx.cs" Inherits="Main" Title="Navigation Using TreeView" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
</asp:Content>
After reading some relevant posts in this forum, I have learnt that:
- With TreeView, we can either navigate or postback. (I would have opted for navigation had the URLs been hard-coded. But since I have to construct URLs on-the-fly, I have to go for postback.)
- To postback, each TreeNode?s NavigateUrl property should remain empty. On the other hand, to navigate, each TreeNode?s NavigateUrl property should be assigned with proper value. (That is why, if you notice, I omit url attribute for each siteMapNode element in Web.sitemap. I assume url attribute is bound to TreeNode?s NavigateUrl property.)
- In order for Selection event (e.g. SelectedNodeChanged) to fire, we should make TreeView postback, thus, the empty NavigateUrl property. (Notice that I construct the URL on-the-fly in the SelectedNodeChanged event handler.)
However, I encounter 2 problems:
- None of the TreeView nodes seems clickable. When mouse is hovered over a node, the mouse cursor remains an arrow, instead of a pointing finger.
- As a result, the SelectedNodeChanged event never gets fired, even if I ?force-click? any node in the TreeView.
With these problems, I fail to programmatically navigate to a different page upon clicking a node in the TreeView.
I observe that only if I add url attributes (with proper values) in siteMapNode elements in Web.sitemap, will the TreeView nodes become clickable. However, consequently, this makes the TreeView navigate, instead of postback, therefore not firing the SelectedNodeChanged event, which I need to handle, in order to construct the URL on-the-fly.
I am sure I have overlooked something or done something wrong but I just don?t know what it is.
Would appreciate your valuable advice. Thanks.