SelectedNode wrong when SelectedNodeChanged event fires

Discussion in 'ASP .Net Web Controls' started by biker, Aug 8, 2006.

  1. biker

    biker Guest

    Looking back, I guess my first request was a bit long and confusing...I hope
    this works out better.

    I have a treeview on a master page that, when I click on a leaf node, fires
    the SelectedNodeChanged event. Only problem is, the treeview.SelectedNode
    points to the wrong node within the event code.

    Can anyone help?? Please??
    biker, Aug 8, 2006
    #1
    1. Advertising

  2. Is it pointing to the old selection, the new one, or a completely different
    one altogether?
    Keith Patrick, Aug 8, 2006
    #2
    1. Advertising

  3. biker

    biker Guest

    A different one. No matter where in the tree I click, it goes to the first
    sibling node of the node I click on. If I have
    A
    a
    b
    c
    B
    d
    e
    f

    and click on any of a, b or c, it goes to a...if I click on any of d, e or
    f, it goes to d.
    biker, Aug 8, 2006
    #3
  4. I want to say that I've run into this problem before when I was customizing
    a tree control (I did one that displays usercontrols as nodes...quite the
    pain, lemme tell you). By any chance are you using a custom control? If
    not, could you post some of the markup so I can try to reproduce?
    Keith Patrick, Aug 17, 2006
    #4
  5. biker

    biker Guest

    No, no usercontrols in the treeview. Here is an extract from my code...maybe
    you can see what is happening, I've been staring at it for too long:

    TREEVIEW PLACED IN A TABLE CELL ON THE MASTER PAGE:

    <asp:TreeView ID="tvMenu" runat="server" DataSourceID="Menu"
    ShowExpandCollapse="False" NodeIndent="8">
    <ParentNodeStyle Font-Bold="True" />
    <HoverNodeStyle Font-Italic="True" />
    <RootNodeStyle Font-Bold="True" />
    <NodeStyle ForeColor="Firebrick" HorizontalPadding="3px" />
    <DataBindings>
    <asp:TreeNodeBinding DataMember="PACS" SelectAction="None" />
    <asp:TreeNodeBinding DataMember="ANode"
    ImageUrl="~/Public/bullet-red.gif" SelectAction="None" TextField="Name"
    ValueField="Permit" />
    <asp:TreeNodeBinding DataMember="BNode"
    ImageUrl="~/Public/bullet-red.gif" SelectAction="None" TextField="Name"
    ValueField="Permit" />
    <asp:TreeNodeBinding DataMember="CNode"
    ImageUrl="~/Public/bullet-narrow-red.gif" TextField="Name"
    ValueField="Permit" TargetField="Target" />
    </DataBindings>
    </asp:TreeView>
    <asp:XmlDataSource ID="Menu" runat="server"
    DataFile="~/Menu.xml"></asp:XmlDataSource>



    CODE-BEHIND FILE FOR MASTER PAGE:

    Partial Class PACSMasterPage
    Inherits System.Web.UI.MasterPage

    Private appsess As PACS.AppSession

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles Me.Load
    appsess = New PACS.AppSession(Session)
    End Sub

    Protected Sub tvMenu_DataBound(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles tvMenu.DataBound
    tvMenu_Traverse(tvMenu.Nodes(0))
    End Sub

    Protected Sub tvMenu_Traverse(ByVal node As TreeNode)
    For Each n As TreeNode In node.ChildNodes
    If n.ChildNodes.Count > 0 Then
    tvMenu_Traverse(n)
    If n.Value <> "ALL" Then
    If Not Validate_User(n.Value, appsess.USER_ACCESS_CSV)
    Then
    n.Collapse()
    End If
    End If
    End If
    Next
    End Sub

    Protected Function Validate_User(ByVal permit As String, ByVal access As
    String) As Boolean
    For Each a As String In Split(access, ",")
    For Each p As String In Split(permit, ",")
    If p = "ALL" Then Return True
    If a = p Then Return True
    Next
    Next
    Return False
    End Function

    Protected Sub lnkLogOff_Click(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles lnkLogOff.Click
    Dim j As Common.JavaScript = New Common.JavaScript()
    j.WindowClose()
    j.Send(MyBase.Page)
    End Sub

    Protected Sub tvMenu_SelectedNodeChanged(ByVal sender As Object, ByVal e
    As System.EventArgs) Handles tvMenu.SelectedNodeChanged
    If Validate_User(tvMenu.SelectedNode.Value, appsess.USER_ACCESS_CSV)
    Then
    If tvMenu.SelectedNode.Target <> String.Empty Then
    Response.Redirect(tvMenu.SelectedNode.Target)
    End If
    End Sub
    End Class




    XML FILE FOR TREEVIEW DATASOURCE

    <?xml version="1.0" standalone="yes"?>
    <PACS Name="PACS Menu" Permit="ALL">
    <ANode Name="Data Interface" Permit="ALL">
    <BNode Name="Data Logger" Permit="ALL">
    <CNode Name="New Entry" Permit="ALL"
    Target="/PACS/App_Forms/DataInt/DataLogger_new.aspx" />
    <CNode Name="Edit Existing Entry" Permit="ALL"
    Target="/PACS/App_Forms/DataInt/DataLogger_select.aspx" />
    </BNode>
    <CNode Name="Log Inspection Results" Permit="ALL" Target="" />
    <CNode Name="View Inspection Results" Permit="ALL" Target="" />
    </ANode>
    <ANode Name="Reports" Permit="ALL">
    <BNode Name="PACS Standard Reports" Permit="ALL">
    <CNode Name="Assembly Defect Summary" Permit="ALL"
    Target="/PACS/App_Forms/Rpts/reports.aspx?type=100" />
    <CNode Name="Traceability" Permit="ALL"
    Target="/PACS/App_Forms/Rpts/reports.aspx?type=900" />
    <CNode Name="Data Screening" Permit="ALL"
    Target="/PACS/App_Forms/Rpts/reports.aspx?type=910" />
    <CNode Name="Data Screening Detail" Permit="ALL"
    Target="/PACS/App_Forms/Rpts/reports.aspx?type=920" />
    <CNode Name="Part Project Detail" Permit="ALL"
    Target="/PACS/App_Forms/Rpts/reports.aspx?type=930" />
    </BNode>
    </ANode>
    <ANode Name="Maintenance Functions" Permit="ALL">
    <CNode Name="Parts / Assemblies" Permit="ALL"
    Target="/PACS/App_Forms/Maint/products.aspx" />
    <CNode Name="Projects" Permit="ALL" Target="" />
    <CNode Name="Attribute Groups" Permit="ALL"
    Target="/PACS/App_Forms/Maint/Quality.aspx" />
    <CNode Name="Defect Rate Groups" Permit="ALL" Target="" />
    </ANode>
    <ANode Name="Administration" Permit="ADMIN">
    <BNode Name="Maintain Users" Permit="ADMIN">
    <CNode Name="Create New User" Permit="ADMIN"
    Target="/PACS/App_Forms/Admin/user_new.aspx" />
    <CNode Name="Update Existing User" Permit="ADMIN"
    Target="/PACS/App_Forms/Admin/user_update.aspx" />
    </BNode>
    <CNode Name="Purge Test Data" Permit="ALL" Target="" />
    <CNode Name="Purge Unit Test Data" Permit="ALL" Target="" />
    <CNode Name="Shift Times" Permit="ALL" Target="" />
    </ANode>
    </PACS>
    biker, Aug 21, 2006
    #5
  6. biker

    Guest

    Hi biker,

    I wish I had some words of wisdom as I am experiencing the exact same
    problem. Have you found a solution or workaround?

    Thanks,
    Stacy
    , Sep 19, 2006
    #6
  7. biker

    biker Guest

    No, nothing I've tried was satisfactory...and I think I stumped Keith, too.

    "" wrote:

    > Hi biker,
    >
    > I wish I had some words of wisdom as I am experiencing the exact same
    > problem. Have you found a solution or workaround?
    >
    > Thanks,
    > Stacy
    >
    >
    biker, Sep 20, 2006
    #7
  8. biker

    Guest

    Hi again,

    I have found, with a great deal of help from a coworker with more
    experience in Web development, a way to make my code work. We simply
    set the Value property of each node to the ChildNodes.Count value as
    the tree is recursively built. My tree is built programmatically. For
    the example tree you gave, the value field should be set as follows:

    A (value = "0")
    a (value = "0")
    b (value = "1")
    c (value = "2")
    B (value = "1")
    d (value = "0")
    e (value = "1")
    f (value = "2")

    We also noted, that at PageLoad, the SelectedNode property of the tree
    is correct, but with the value properties set, the SelectedNode
    property is correct in the SelectedNodeChanged event also.

    I hope this imformation helps you,
    Stacy
    , Sep 20, 2006
    #8
  9. biker

    biker Guest

    I put the Permit value in my XML in the Value field. Sadly, this does not
    influence the outcome. Perhaps the fact you are manually loading the tree
    whereas I am linking the treeview to the XML through a datasource...but I
    think that is another of the things I tried. Maybe I'll give that another
    shot to make sure.

    Thanks for the suggestion; I'm glad you found a solution for your situation.

    "" wrote:

    >
    > Hi again,
    >
    > I have found, with a great deal of help from a coworker with more
    > experience in Web development, a way to make my code work. We simply
    > set the Value property of each node to the ChildNodes.Count value as
    > the tree is recursively built. My tree is built programmatically. For
    > the example tree you gave, the value field should be set as follows:
    >
    > A (value = "0")
    > a (value = "0")
    > b (value = "1")
    > c (value = "2")
    > B (value = "1")
    > d (value = "0")
    > e (value = "1")
    > f (value = "2")
    >
    > We also noted, that at PageLoad, the SelectedNode property of the tree
    > is correct, but with the value properties set, the SelectedNode
    > property is correct in the SelectedNodeChanged event also.
    >
    > I hope this imformation helps you,
    > Stacy
    >
    >
    biker, Sep 21, 2006
    #9
  10. biker

    ReyN Guest

    you have your treenodebindings with SelectAction set to NONE, which in
    effect raises no events when a node is selected.

    if you need to use the SelectedNodeChanged event when a node is
    selected, then SelectAction must be set to either Select or SelectExpand
    ReyN, Sep 22, 2006
    #10
  11. biker

    biker Guest

    Actually, when I reset things to the way they were when I was testing, I
    forgot to set the SelectAction to Select. That's the value I used during my
    testing. The event was triggering, but the SelectedNode value was pointing
    to the wrong node. Events were definately firing, and I had breakpoints in
    the code to check the values.

    "ReyN" wrote:

    > you have your treenodebindings with SelectAction set to NONE, which in
    > effect raises no events when a node is selected.
    >
    > if you need to use the SelectedNodeChanged event when a node is
    > selected, then SelectAction must be set to either Select or SelectExpand
    >
    >
    biker, Sep 22, 2006
    #11
  12. biker

    ReyN Guest

    ReyN, Sep 23, 2006
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrew Robinson

    TreeView SelectedNodeChanged Event

    Andrew Robinson, Dec 5, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    11,226
    Steven Cheng[MSFT]
    Dec 7, 2005
  2. tanya foster
    Replies:
    1
    Views:
    3,577
    tanya foster
    Jun 5, 2006
  3. Replies:
    0
    Views:
    435
  4. Velislav

    TreeView SelectedNodeChanged event

    Velislav, Nov 24, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    674
    Velislav
    Nov 24, 2006
  5. Replies:
    3
    Views:
    982
Loading...

Share This Page