473,434 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,434 software developers and data experts.

Maintaining View State For Dynamically Created Controls

I am building a custom control that I want to server as a container for child
controls that can be dynamically added to this control. I can persist the
child controls that are added to my custom control but can to get them to
retain view state.

Below is a striped down version of my control. It exposes one property of
type System.Web.UI.Control that the containing page my set with a dynamically
created control My custom control will then add it to its control tree and
render it to the display. The information need to recreate the control is
stored in a System.Web.UI.Triplet object. On postback, the child control is
reinstanced and given its origonal id and added to the Custom Controls
control tree and rendered to the display.

The only problem is that the ViewState for the child control is not being
retained.

Has anyone solved this puzzle???

Thanks for your help

Earl

CUSTOM WEB SERVER CONTROL

Imports System.ComponentModel
Imports System.Web.UI

<ToolboxData("<{0}:TabMenuA runat=server></{0}:TabMenuA>")> Public Class
ContainerControl
Inherits System.Web.UI.WebControls.WebControl

#Region "Declarations"

Private _ChildControlToRender As System.Web.UI.Control

#End Region

#Region "Properties"

<Bindable(True), Category("Data"), DefaultValue("")> Public Property
ChildControlToRender() As System.Web.UI.Control
Get
Return _ChildControlToRender
End Get
Set(ByVal Value As System.Web.UI.Control)
_ChildControlToRender = Value
End Set
End Property

#End Region

#Region "Methods"

Protected Overrides Sub CreateChildControls()
If Not Page.IsPostBack Then
Controls.Add(_ChildControlToRender)
End If

End Sub
Protected Overrides Sub LoadViewState(ByVal savedState As Object)
Dim objViewStates() As Object
objViewStates = CType(savedState, Object())
Dim Trip As System.Web.UI.Triplet
Trip = objViewStates(1)
_ChildControlToRender = Activator.CreateInstance(CType(Trip.Second,
System.Type))
_ChildControlToRender.ID = Trip.First
Me.Controls.Add(_ChildControlToRender)
MyBase.LoadViewState(objViewStates(0))
End Sub

Protected Overrides Function SaveViewState() As Object
Dim objViewStates(1) As Object
Dim Trip As New System.Web.UI.Triplet
Trip.First = _ChildControlToRender.ID
Trip.Second = _ChildControlToRender.GetType
Trip.Third = Me.ID
objViewStates(1) = Trip
objViewStates(0) = MyBase.SaveViewState()
Return objViewStates
End Function

#End Region
End Class

CONTAINING PAGE CODE

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
Dim myButton As Button = New Button
myButton.Text = "Hello1"
myButton.ID = "ButtonA"
myButton.EnableViewState = True
ContainerControl1.ChildControlToRender = myButton
End If

End Sub

Nov 18 '05 #1
0 1754

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Michael | last post by:
Hi, Any one know the problem of the following error: Thanks, m Failed to load viewstate. The control tree into which viewstate is being loaded must match the control tree that was used to...
2
by: Brad | last post by:
I have an intranet app that has just started sporadically getting the following error "The viewstate is invalid for this page and might be corrupted." By sproadic I mean 3-4 times during the past...
3
by: MattC | last post by:
I have created a container class and instantiated it in one of my pages. I fill the object on page load when not on a postback. On a button click i want to do somethign with the values in that...
1
by: Novice | last post by:
Hey all, I have finally managed to create a Custom WebControl and am using a technique from another programmer to maintain state between pages - I would just like to validate this idea. ...
2
by: Chad | last post by:
I have a problem that I am desperate to understand. It involves dynamically adding controls to a Table control that is built as a result of performing a database query. I am not looking to...
3
by: jclark0731 | last post by:
I am trying to redirect to the same page after a post-back is fired to ensure that the post-back never makes it into history (avoids the post-back/refresh problem). While doing this, I wish to...
5
by: Amelyan | last post by:
How can I get state of dynamically created controls (RadioButton, CheckBox, TextBox.Text) on post back when I click submit button? The only way I know is by traversing Response.Form enumberator;...
2
by: Pete Moss | last post by:
During a postback event, I am having trouble retrieving the state of a CheckBox Control that I am dynamically adding to a DataGrid Control using ASP.NET 1.1. I have no trouble adding the...
0
by: mark.norgate | last post by:
Hi I'm having a problem in adding controls to a page programmatically in response to a button click. Composite user controls added programmatically in the CreateChildControls() method work...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.