Created controls not storing viewstate

A

Adrian Parker

v1.1 and v2.0

We have a problem with viewstate not being stored.

What's happening is that we create controls in CreateChildControls and add them to a container on the page (whether it be a panel or
a table etc). We only set the data values for the controls if not page.ispostback. But, if either of the textboxes change,
the page posts back and the changed event fires twice.

Protected Overrides Sub CreateChildControls()
Dim txtbox1 As New TextBox
txtbox1.ID = "txt1"
AddHandler txtbox1.TextChanged, AddressOf Text_Changed
txtbox1.AutoPostBack = True
If Not Page.IsPostBack Then txtbox1.Text = "text box 1"
Panel1.Controls.Add(txtbox1)

Dim txtbox2 As New TextBox
txtbox2.ID = "txt2"
AddHandler txtbox2.TextChanged, AddressOf Text_Changed
txtbox2.AutoPostBack = True
If Not Page.IsPostBack Then txtbox2.Text = "text box 2"
Panel2.Controls.Add(txtbox2)
End Sub

Private Sub Text_Changed(ByVal sender As System.Object, ByVal e As System.EventArgs)
Response.Write("text changed: " + sender.ID + "<br>")
End Sub

If instead of creating controls dynaically, I create them by dragging them onto the form then it works fine.

How do I force the page to store the created controls' viewstate ?
 
S

Steven Cheng[MSFT]

Hi Adrian,

Welcome the ASP.NET newsgroup.

As for the custom composite control developing problem you mentioned, I
think you can try checking the following things first:

1. Make sure your custom composite control class has implemented the
"INamingContainer" interface, this is very important for custom control
which will contain nested child controls.

2. In the "CreateChildControls" function, if you want to assign some
initial value for some certain sub controls' property, make sure you set
value after the sub control has been added into the container's control
collection.

As for postback event, from the code you provided, I think it should be ok
and I haven't found anything incorrect in the function. Therefore, the
problem could be caused by anything else in the control. Anyway, I've
pasted the code of my test control which runs correctly in my local
project, you can have a test on your side to see whether it works:

==================================
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls


<DefaultProperty("Text"), ToolboxData("<{0}:WebCustomControl1
runat=server></{0}:WebCustomControl1>")> _
Public Class WebCustomControl1
Inherits WebControl
Implements INamingContainer

Private _panel As Panel
Private _txt1 As TextBox
Private _txt2 As TextBox


<Bindable(True), Category("Appearance"), DefaultValue(""),
Localizable(True)> Property Text() As String
Get
Dim s As String = CStr(ViewState("Text"))
If s Is Nothing Then
Return String.Empty
Else
Return s
End If
End Get

Set(ByVal Value As String)
ViewState("Text") = Value
End Set
End Property

Protected Overrides Sub CreateChildControls()
Controls.Clear()

_panel = New Panel()
_panel.ID = "plMain"

Controls.Add(_panel)

_txt1 = New TextBox()
_txt1.ID = "txt1"
_txt1.AutoPostBack = True
AddHandler _txt1.TextChanged, AddressOf TextBox_TextChanged

Controls.Add(_txt1)


_txt2 = New TextBox()
_txt2.ID = "txt2"
_txt2.AutoPostBack = True
AddHandler _txt2.TextChanged, AddressOf TextBox_TextChanged

Controls.Add(_txt2)


If Not Page.IsPostBack Then

_txt1.Text = "Textbox1 initial text"
_txt2.Text = "Textbox2 initial text"

End If

End Sub


Private Sub TextBox_TextChanged(ByVal sender As Object, ByVal e As
EventArgs)

Page.Response.Write("<br/>" & sender.ID & ", new text: " &
sender.Text)
End Sub
End Class
=========================================

Hope this helps.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
A

Adrian Parker

Hi Steven,

I didn't say I was creating custom controls, I said I was adding controls dynamically. But your statement "make sure you set
value after the sub control has been added into the container's control collection" gave me a clue.. If you dynamically add a
control to a container on a page (in my example a panel) then you have to add it before assigning the initial value to it, else the
viewstate won't be stored.

Thanks
 
S

Steven Cheng[MSFT]

Thanks for your quick response,

Glad that it is of assistance.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Top