TreeView and EnableViewState

TreeView and EnableViewState

Post by Chris Simmon » Tue, 06 Aug 2002 12:42:28



Why is it that I cannot reference the contents of a TreeView when the
EnableViewState is set to false?  I understand it not being built
(unless I repopulate/DataBind it) on the postback, but I should still
be able to reference the contents of the control on the submitting
page, no?  Reason being I'd like to reference which nodes where
checked.  However, it seems the only way I can reference the nodes is
if I set the EnableViewState to true for the TreeView.  I'm wondering
if I'm missing the whole point here, but I'd really like to avoid
having to pass that huge __VIEWSTATE variable back and forth.  I have
no problem rebuilding the TreeView for every PostBack, but I would
like to see which items were checked in the server-side code.
Here is the code I have to illustrate:

aspx file:

AutoEventWireup="false" Inherits="treeview.index" Trace="true" %>

Assembly="Microsoft.Web.UI.WebControls, Version=1.0.2.226,
Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<TITLE>index</TITLE>
<META content="Microsoft Visual Studio 7.0" name="GENERATOR">
<META content="C#" name="CODE_LANGUAGE">
<META content="JavaScript" name="vs_defaultClientScript">
<META content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<BODY>
<FORM id="index" method="post" runat="server">
<IEWC:TREEVIEW id="tvTest" runat="server" enableviewstate="False">
</IEWC:TREEVIEW><BR>
<ASP:BUTTON id="btnSubmit" runat="server" text="Submit" />
</FORM>
</BODY>
</HTML>

cs file:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace treeview
{
public class index : System.Web.UI.Page
{
protected Microsoft.Web.UI.WebControls.TreeView tvTest;
protected System.Web.UI.WebControls.Button btnSubmit;
private void Page_Load(object sender, System.EventArgs e)
{
        if ( !IsPostBack )
        {
                PopulateTree();
        }
        else                    
        {
        // At this point, Count is 0 here, not 10 as I'd
        // expect it to be.
        for ( int i = 0; i < tvTest.Nodes.Count; i++ )
                if ( tvTest.Nodes[ i ].Checked )
                Trace.Write( tvTest.Nodes[ i ].Text + " checked." );
        }

Quote:}

private void PopulateTree()
{
        TreeNode tn;
        TreeNode tn2;
        TreeNode tn3;

        for ( int i = 0; i < 10; i++ )
        {
        tn = new TreeNode();
        tn.Text = "Node " + i.ToString();
        tvTest.Nodes.Add( tn );
        if ( i % 2 == 0 )
                tn.CheckBox = true;
        for ( int j = 0; j < 4; j++ )
        {
                tn2 = new TreeNode();
                tn2.Text = "Node " + i.ToString() + "." +
j.ToString();
                tn.Nodes.Add( tn2 );
                if ( j % 2 == 0 )
                        tn2.CheckBox = true;
                for ( int k = 0; k < 5; k++ )
        {
        tn3 = new TreeNode();
                tn3.Text =
                        "Node " + i.ToString() +
                        "." + j.ToString() +
                        "." + k.ToString();
                tn2.Nodes.Add( tn3 );
                if ( k % 2 == 0 )
                        tn3.CheckBox = true;
        }

Quote:}
}
}
}
}

--
Chris Simmons

http://www.NetChris.com
 
 
 

1. EnableViewState on a ListBox only works when inheriting directly from System.Web.UI.Page

Hi,

In order to make my WebForms have the same common controls like menus
and footers, I use my own base class, which is inheriting from
System.Web.UI.Page instead of using that class directly.

I run into problem when I add a ListBox to my WebForm and sets the
EnableViewState property to true. The state of the control won't
remain between post backs as it should - the control is left empty.
This does not happen if my WebForm inherits directly from
System.Web.UI.Page, but works just great. I have noticed that other
controls, like TextBoxes, work just fine in both cases.

Does anyone have some idea of how I could solve this problem?

Thanks,
Henrik

This code doesn't work:

    public class BaseForm : System.Web.UI.Page
    {
        // Code
    }

    public class MyForm : BaseForm
    {
        // Code with ListBox and EnableViewState set but not working
    }

This code works:

    public class MyForm : System.Web.UI.Page
    {
        // Code with ListBox and EnableViewState set and working
    }

2. Hi Partner.... Ab Initio Admin (VERY URGENT) in NJ and TX

3. DataGrid EnableViewState = False and Sorting

4. Hardware for Bentley Products

5. EnableViewState

6. PSION 5 USER GUIDE

7. EnableViewState doesn't change a thing

8. checkedListBox

9. DropdownList and enableViewState when fill with DataReader

10. enableviewstate problem

11. SelectedIndexChanged not firing when EnableViewState set to False

12. EnableViewState makes no difference

13. Why DataGrid events doesnt work with EnableViewState = false ?