Link to home
Start Free TrialLog in
Avatar of mousemat24
mousemat24

asked on

NavigateURL does not remember what you've selected in a Treeview

I really need somones help who has good knowledge in ASP.NET treeview. I'm using VS2005/C#

I have a treeview control, that is populated from a SQL db, all of the navigateURL has a URL. My problem is when I click on anyone of the URL's it dosnt remember what node I clicked on i.e. it dosnt highlight the node.

Can some one please help me in getting this to work i.e. when a user clicks on a URL, it hightlights the selected node.

Thank you
Mousemat24
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Are you talking about something like SmartNavigation, when you use the Go Back button on the browser?

Bob
Avatar of mousemat24
mousemat24

ASKER

No TheLearnedOne.

I have a treeview, all the nodes have a navigateURL prop assigned to it, so that when a user clicks on the selectednode, it opens up a new page. But the problem I'm having is the selected node isnt highlighted, so they dont know where they are in the menu?

Does that makes sense? if not, please let me know
mousemat24
>>when a user clicks on the selectednode, it opens up a new page
Can you show me how you do this?

Bob
for (i = 0; i < tempFor1; i++)
            {
                if (Convert.ToInt32(ds.Tables[0].Rows[i]["MNU_ParentNodeID"]) == 0)
                {
               
                    TreeNode tn = new TreeNode(ds.Tables[0].Rows[i]["MNU_DisplayName"].ToString(), ds.Tables[0].Rows[i]["MNU_URL"].ToString(), "");

                    RecursivelyAddToNode(tn, Convert.ToInt32(ds.Tables[0].Rows[i]["MNU_ID"]), ds);
                    TreeView1.Nodes.Add(tn);

                }
            }

============================================================

private void RecursivelyAddToNode(TreeNode mymenuitem, int ParentID, DataSet ds)
    {
       
        DataView dv = new DataView(ds.Tables[0]);
        dv.RowFilter = ("MNU_ParentNodeID=" + ParentID.ToString());
        foreach (DataRowView r in dv)
        {
       
            TreeNode tn = new TreeNode(r["MNU_DisplayName"].ToString(), r["MNU_URL"].ToString(), "");
            mymenuitem.ChildNodes.Add(tn);
            RecursivelyAddToNode(tn, Convert.ToInt32(r["MNU_ID"]), ds);
        }
    }


==============================================================

protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
    {
        string valueOfTreenode          = TreeView1.SelectedNode.Value;
        string valueOfTreenodeIndexOf   = valueOfTreenode.IndexOf("{").ToString();
        Label1.Text = TreeView1.SelectedNode.Value.ToString();

        if (Convert.ToInt16(valueOfTreenodeIndexOf) == -1)
        {
            Response.Redirect(valueOfTreenode);
        }
        else
        {
            string getUnidOutFromValue = valueOfTreenode.Substring(valueOfTreenode.LastIndexOf("{"));
            Web_Ref.WebService getContent = new Web_Ref.WebService();

            DataSet ds = new DataSet();
            ds = getContent.displayContent(getUnidOutFromValue);
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                content.InnerHtml = dr["CNT_Body"].ToString();
            }            
        }        
    }


Hope that helps, really would like some help
TheLearnedOne

As you can see Im using a Response.Redirect(<ASPX FILE>). What I want is when a user clicks on that node it will open the page up (through the master page), and highlight the selectedNode. There must be a way to get this to work with a Response.Redirect

Thanks
Hi, mousemat24

As I told, I'm not really working with ASP. And do not much care about points :)

As I pointed in the other toopic, when a node is in navigation mode (navigateURL is assigned), all selection events are disabled for that node.
Therefore the method 'TreeView1_SelectedNodeChanged' shouldn't be invoked at all.

Did you try to NOT assign NavigateUrl but put you url into value property? Eventually you are reading the url from the value property in your method...
I've tried using the value, but when you do a redirect it dosnt know what you've selected.

Please see this:

http://channel9.msdn.com/ShowPost.aspx?PostID=110278

There must be a way to do this?
Hmmm...

I'm not good enough in ASP, I told.

However what if you e.g. try to set session variable to the url, then redirect and set the selected node from this session variable?
Does it make sense at all?
I've done that, no luck, really need some help on this question

Thanks
I give up.

Still check that navigateURL prop is set to blank string for ALL nodes. Otherwise I believe select mode is never working...
Have you set the SelectedNodeStyle.ForeColor/BackColor for the TreeView?

Bob
TheLearnedOne

the problem is, if the selected item is deep within a menu, it wont expand the menu, so if I can get that to work, maybe getting the SelectedNodeStyle.ForeColor/BackColor might work ok. This is really doing my brains in!! :-)
If you don't set the SelectedNodeStyle, then you won't see the selected node, in order to verify that it is working.

Bob
Just done thie Bob

TreeView1.SelectedNodeStyle.BackColor = System.Drawing.Color.Aqua;

It dosnt work when I have a Response.Redirect

1) Are you using master pages?

2) When you do a Response.Redirect, is the TreeView still visible?

3) If you are using Response.Redirect, and not master pages, then usually the TreeView would no longer be visible, unless you have some other kind of magic.

Bob
1) I'm using a masterpage
2) It is visible, but the menu gets collapsed, so my selectednode isnt highlighted
3) No magic - using a master page

Annoying problem isnt it
>>It is visible, but the menu gets collapsed, so my selectednode isnt highlighted
Can you explain this a little more, please?  How does it get collapsed?

Bob
Bob, I've set the menu prop to CollapseAll

TreeView1.CollapseAll();

The reason being I will have alot of menu options, so I want to Collapse it. As I mentioned it works with the rest of the menuNodes i.e. It opens up the correct menu and remembers what you clicked on.

When I'm pulling out information from SQL, I place the content into a literal control, content.innerHTML = <FROM SQL>

But there are other menus that point to another page i.e. ASPX. Because of this I have to use Response.Redirect, theres where it stops working.

Hope thats clear?
If not please let me know
mousemat24
If you don't collapse the TreeView, does it highlight the node correctly?

Bob
No it dosnt Bob, it dosnt work with any response.redirect
Does it work with Server.Transfer?  Do you need Response.Redirect?

Bob
Nope sorry, I tried that too :-(
Any other stuff I can do Bob?
Thanks
You could try MaintainScrollPositionOnPostback  in the @Page directive:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TreeViewExample.aspx.cs" Inherits="TreeViewExample" MaintainScrollPositionOnPostback="true" %>

Bob
TheLearnedOne

That too dont work, I placed that in one of my ASPX file that uses the Response.Redirect

:-(
Hi Bob

I think I found the answer!!!!

Go here and download the ZIP file

http://blog.binaryocean.com/2006/01/19/SaveTreeViewNodesExpansionCollapseStateCSAndVB.aspx

It expands, but I cant figure out how to highlight the selectedNode. Do you thanks you can help me?

thanks
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Bob

Did you try the URL, that clearly show the problem, i.e. the selected node isnt highlighted, Can you help in getting the hightlighted node color to change

Thanks
Let's revisit an old comment:

Did you set the TreeView.SelectedNodeStyle with that new code?

Bob
I've implemented the code from:

http://blog.binaryocean.com/2006/01/19/SaveTreeViewNodesExpansionCollapseStateCSAndVB.aspx

It dosnt hightlight the TreeView.SelectedNodeStyle, The problem I'm having is that, I dont know where to palce the code in the CS file. Its way to complicating for me, do you think you can look into it for me Bob?

Thanks
No, unfortunately I have deadlines to meet, and I don't have the time.  I was hoping that this would be a simple solution, but it has turned out to be that.

Bob
ok Bob

Thanks for helping out anyway
Mousemat24
Even though this problem never got sorted out, I'd like to give you the points TheLearnedOne for helping out, but I dont know what to accept?

Thanks