I have an asp.net 2.0 page that contains 2 UpdatePanels
.
The first panel contains a TreeView
control, when I select a node in the three view control it triggers an update of the second UpdatePanel
only. This much is behaving correctly.
There are two buttons on the page outside of an update panel (previous/next). These buttons trigger an update of both panels. The behaviour of the buttons is to select the adjacent node in the tree. The first time I click on one of these buttons I get the expected behaviour, and adjacent node is selected and the both panels are updated to reflect this change.
The problem happens when I click any of these buttons again. The selected node of the treeview seems to remember the previously selected node and the buttons act on this node. So the behaviour of the previous/next buttons is to do nothing or jump back two.
Edit - Sample code that demonstrates my problem
The 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 behind
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;
}
It is like the viewstate of the tree is not being saved?