Why is my asp: TreeView selected node reset when in UpdatePanel?

I have an asp.net 2.0 page containing 2 UpdatePanels.

The first panel contains the control TreeView, when I select node in three view modes, it only starts updating the second UpdatePanel. This is very true.

There are two buttons (previous / next) on the page outside the update panel. These buttons start updating both panels. The behavior of the buttons is to select an adjacent node in the tree. The first time I click on one of these buttons, I get the expected behavior, and the adjacent node is selected, and both panels are updated to reflect this change.

The problem occurs when I press any of these buttons again. The selected node of the tree structure seems to remember the previously selected node, and the buttons act on that node. Thus, the behavior of the previous / next buttons is to do nothing or bounce back.

Change . Sample code demonstrating my problem.

Markup

 <asp:UpdatePanel ID="myTreeViewPanel" runat="server">
    <ContentTemplate>
       <asp:TreeView runat="server" ID="myTreeView" OnSelectedNodeChanged="myTreeView_SelectedNodeChanged">
          <SelectedNodeStyle BackColor="#FF8000" />
       </asp:TreeView>
    </ContentTemplate>
    <Triggers>
       <asp:AsyncPostBackTrigger ControlID="myButton" EventName="Click" />
    </Triggers>
 </asp:UpdatePanel>
 <asp:UpdatePanel ID="myLabelPanel" runat="server">
    <ContentTemplate>
       <asp:Label runat="server" ID="myLabel" Text="myLabel"></asp:Label>
    </ContentTemplate>
    <Triggers>
       <asp:AsyncPostBackTrigger ControlID="myTreeView" EventName="SelectedNodeChanged" />
       <asp:AsyncPostBackTrigger ControlID="myButton" EventName="Click" />
    </Triggers>
 </asp:UpdatePanel>
 <asp:Button runat="server" ID="myButton" Text="myButton" OnClick="myButton_Click" />

The code

   protected void Page_Load ( object sender, EventArgs e )
   {
      if ( !IsPostBack )
      {
         myTreeView.Nodes.Add( new TreeNode( "Test 1", "Test One" ) );
         myTreeView.Nodes.Add( new TreeNode( "Test 2", "Test two" ) );
         myTreeView.Nodes.Add( new TreeNode( "Test 3", "Test three" ) );
         myTreeView.Nodes.Add( new TreeNode( "Test 4", "Test four" ) );
         myTreeView.Nodes.Add( new TreeNode( "Test 5", "Test five" ) );
         myTreeView.Nodes.Add( new TreeNode( "Test 6", "Test size" ) );
      }
   }
   protected void myTreeView_SelectedNodeChanged ( object sender, EventArgs e )
   {
      UpdateLabel( );
   }
   protected void myButton_Click ( object sender, EventArgs e )
   {
      // here we just select the next node in the three
      int index = myTreeView.Nodes.IndexOf( myTreeView.SelectedNode );
      myTreeView.Nodes[ index + 1 ].Select( );
      UpdateLabel( );
   }
   private void UpdateLabel ( )
   {
      myLabel.Text = myTreeView.SelectedNode.Value;
   }

Does it look like the tree view is not saved?

+3
source share
4 answers

From UpdatePanel Management Review [asp.net]

Controls that are not compatible with UpdatePanel controls

The following ASP.NET controls are not compatible with partial page updates and therefore are not supported inside the UpdatePanel control:

  • TreeView and Menu controls.
  • ...
+3

, AJAX, __ViewState.

, -. viewstate, , , AJAX, .

0

, node , .

protected override object SaveViewState()
{
   ViewState["SelectedNodePath"] = myTreeView.SelectedNode.ValuePath;
   return base.SaveViewState();
}

protected void Page_PreLoad(object sender, EventArgs e)
{
   if (ViewState["SelectedNodePath"] != null)
   {
      TreeNode node = myTreeView.FindNode(ViewState["SelectedNodePath"].ToString());
      if (node != null)
         node.Select();
   }
}

node LoadViewState(), . PreLoad.

, . - ?

0

I have almost the same problem. I am losing the CheckedNodes collection. I doubt that the same technique will solve my problems. Any idea?

Update: I solved the problem to a large extent and used some workarounds. See Details here: TreeView inside UpdatePanel problem - postback when checking and unchecking node and Using TreeView inside AJAX UpdatePanel

0
source

Source: https://habr.com/ru/post/1698301/


All Articles