Maintain viewstate when not rendered

Discussion in 'ASP .Net Building Controls' started by Chris Herring, Jul 15, 2003.

  1. I have a custom control which contains some child controls. Not all of the
    child controls are always rendered. I would like to maintain viewstate for
    the child controls, even when they are not rendered. How do I accomplish
    this?

    Here is an example. This is VB code from Visual Studio. This code assumes
    you have a project named "AimClientW"
    Run testPage.aspx, enter some text in the textbox, click the hide button and
    then the show button, and the text you typed is gone. How do I make the
    textbox maintain viewstate when not rendered?

    Thanks

    Chris

    ---------------------- testPage.aspx ----------------------------
    <%@ Register TagPrefix="ac" namespace="aimClientW" assembly="aimClientW" %>
    <%@ Page Language="vb" AutoEventWireup="false" Codebehind="testPage.aspx.vb"
    Inherits="aimClientW.testPage"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
    <meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
    <meta content="JavaScript" name="vs_defaultClientScript">
    <meta content="http://schemas.microsoft.com/intellisense/ie5"
    name="vs_targetSchema">
    </HEAD>
    <body MS_POSITIONING="FlowLayout">
    <form id="Form1" method="post" runat="server">
    <ac:testcontainer id="testcontainer1" runat="server">
    <asp:CheckBox id="chkTest" runat="server"></asp:CheckBox>
    <asp:textbox id="txtTest" runat="server"></asp:textbox>
    </ac:testcontainer>
    <asp:button id="Button1" runat="server" Text="Hide">
    </asp:button>
    <asp:button id="Button2" runat="server" Text="Show">
    </asp:button>
    </form>
    </body>
    </HTML>

    ---------------------- testPage.vb ----------------------------

    Public Class testPage
    Inherits System.Web.UI.Page
    #Region " Web Form Designer Generated Code "
    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub
    InitializeComponent()
    End Sub
    Protected WithEvents chkTest As System.Web.UI.WebControls.CheckBox
    Protected WithEvents txtTest As System.Web.UI.WebControls.TextBox
    Protected WithEvents testcontainer1 As aimClientW.TestContainer
    Protected WithEvents Button1 As System.Web.UI.WebControls.Button
    Protected WithEvents Button2 As System.Web.UI.WebControls.Button
    'NOTE: The following placeholder declaration is required by the Web Form
    Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object
    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Init
    'CODEGEN: This method call is required by the Web Form Designer
    'Do not modify it using the code editor.
    InitializeComponent()
    End Sub
    #End Region
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles Button1.Click
    Me.testcontainer1.hideChildren = True
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles Button2.Click
    Me.testcontainer1.hideChildren = False
    End Sub
    End Class

    -------------------------------
    testContainer.vb -----------------------------------------
    Public Class TestContainer
    Inherits Control
    Private mHide As Boolean = False
    Public Property hideChildren() As Boolean
    Get
    Return mHide
    End Get
    Set(ByVal Value As Boolean)
    mHide = Value
    End Set
    End Property
    Protected Overrides Sub render(ByVal pWriter As HtmlTextWriter)
    If Not mHide Then
    Dim ctl As Control
    For Each ctl In Me.Controls
    ctl.RenderControl(pWriter)
    Next
    End If
    End Sub
    End Class
    ---------------------------------------------------------------------------
     
    Chris Herring, Jul 15, 2003
    #1
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.