I’m trying to make a tabbed user interface using an asp:Panel and the asp:TreeView. I’m keeping both TreeViews initialized in the background and when I need to display one, I clear the panel and then add the appropriate TreeView.
I’m keeping the TreeView’s stored in the Session variable so they don’t go away when the page gets reloaded due to a postback.
But when I try to add the control, I get an ArgumentOutOfRangeException. Here’s the small snippet of code that matters:
<asp:UpdatePanel UpdateMode="Conditional" runat="server">
<ContentTemplate>
<asp:LinkButton ID="ContentButton" OnClick="ContentButton_Click" Text="Contents" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel UpdateMode="Conditional" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ContentButton" />
</Triggers>
<ContentTemplate>
<div id="TOCPanel" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
Here’s the related code in the C# file.
protected void IndexButton_Click( object sender, EventArgs e )
{
TOCPanel.Controls.Clear();
// _TreeView points to a property that retrieves the TreeView from Session["TreeView"]
TOCPanel.Controls.Add( _TreeView ); // This line fails
// TOCPanel.Controls.Add( new TreeView() ); // This fails with the same error message too
}
Is there any reason
Not sure what the problem is, but I’d suggest adding both TreeViews to the ContentTemplate, and then hiding/showing them with the Visible property. This gets rid of your dependency on Session as well. I’ve found manually adding and removing controls gets messy, especially when dealing with ViewState.