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

    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?



    ---------------------- testPage.aspx ----------------------------
    <%@ Register TagPrefix="ac" namespace="aimClientW" assembly="aimClientW" %>
    <%@ Page Language="vb" AutoEventWireup="false" Codebehind="testPage.aspx.vb"
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <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=""
    <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>
    <asp:button id="Button1" runat="server" Text="Hide">
    <asp:button id="Button2" runat="server" Text="Show">

    ---------------------- 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
    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
    '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.
    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
    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
    End If
    End Sub
    End Class
    Chris Herring, Jul 15, 2003
    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.