473,654 Members | 3,289 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Created controls not storing viewstate

v1.1 and v2.0

We have a problem with viewstate not being stored.

What's happening is that we create controls in CreateChildCont rols 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 CreateChildCont rols()
Dim txtbox1 As New TextBox
txtbox1.ID = "txt1"
AddHandler txtbox1.TextCha nged, AddressOf Text_Changed
txtbox1.AutoPos tBack = 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.TextCha nged, AddressOf Text_Changed
txtbox2.AutoPos tBack = True
If Not Page.IsPostBack Then txtbox2.Text = "text box 2"
Panel2.Controls .Add(txtbox2)
End Sub

Private Sub Text_Changed(By Val sender As System.Object, ByVal e As System.EventArg s)
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 ?

--
Adrian Parker
Ingenuity At Work Ltd
Mar 1 '06 #1
3 2062
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
"INamingContain er" interface, this is very important for custom control
which will contain nested child controls.

2. In the "CreateChildCon trols" 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.Collecti ons.Generic
Imports System.Componen tModel
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.W ebControls
<DefaultPropert y("Text"), ToolboxData("<{ 0}:WebCustomCon trol1
runat=server></{0}:WebCustomCo ntrol1>")> _
Public Class WebCustomContro l1
Inherits WebControl
Implements INamingContaine r

Private _panel As Panel
Private _txt1 As TextBox
Private _txt2 As TextBox
<Bindable(True) , Category("Appea rance"), DefaultValue("" ),
Localizable(Tru e)> 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 CreateChildCont rols()
Controls.Clear( )

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

Controls.Add(_p anel)

_txt1 = New TextBox()
_txt1.ID = "txt1"
_txt1.AutoPostB ack = True
AddHandler _txt1.TextChang ed, AddressOf TextBox_TextCha nged

Controls.Add(_t xt1)
_txt2 = New TextBox()
_txt2.ID = "txt2"
_txt2.AutoPostB ack = True
AddHandler _txt2.TextChang ed, AddressOf TextBox_TextCha nged

Controls.Add(_t xt2)
If Not Page.IsPostBack Then

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

End If

End Sub
Private Sub TextBox_TextCha nged(ByVal sender As Object, ByVal e As
EventArgs)

Page.Response.W rite("<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.)



Mar 2 '06 #2
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
--
Adrian Parker
Ingenuity At Work Ltd

"Steven Cheng[MSFT]" <st*****@online .microsoft.com> wrote in message news:PS******** ******@TK2MSFTN GXA03.phx.gbl.. .
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
"INamingContain er" interface, this is very important for custom control
which will contain nested child controls.

2. In the "CreateChildCon trols" 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.Collecti ons.Generic
Imports System.Componen tModel
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.W ebControls
<DefaultPropert y("Text"), ToolboxData("<{ 0}:WebCustomCon trol1
runat=server></{0}:WebCustomCo ntrol1>")> _
Public Class WebCustomContro l1
Inherits WebControl
Implements INamingContaine r

Private _panel As Panel
Private _txt1 As TextBox
Private _txt2 As TextBox
<Bindable(True) , Category("Appea rance"), DefaultValue("" ),
Localizable(Tru e)> 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 CreateChildCont rols()
Controls.Clear( )

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

Controls.Add(_p anel)

_txt1 = New TextBox()
_txt1.ID = "txt1"
_txt1.AutoPostB ack = True
AddHandler _txt1.TextChang ed, AddressOf TextBox_TextCha nged

Controls.Add(_t xt1)
_txt2 = New TextBox()
_txt2.ID = "txt2"
_txt2.AutoPostB ack = True
AddHandler _txt2.TextChang ed, AddressOf TextBox_TextCha nged

Controls.Add(_t xt2)
If Not Page.IsPostBack Then

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

End If

End Sub
Private Sub TextBox_TextCha nged(ByVal sender As Object, ByVal e As
EventArgs)

Page.Response.W rite("<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.)


Mar 2 '06 #3
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.)

Mar 2 '06 #4

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

Similar topics

8
4274
by: Invalidlastname | last post by:
Hi, We are developing an asp.net application, and we dynamically created certain literal controls to represent some read-only text for certain editable controls. However, recently we found an issue which is related to the repeater. In the code shown below, if I call Repeater1.Controls.Count in the OnInit (the code fragment was highlighted in yellow) , the viewstate for the repeater will be lost during the postback. You can re-produce this...
0
2105
by: John Crowley | last post by:
I'm having an odd problem with viewstate and a dynamically created control inside a repeater template. Basically, I have a repeater setup like this in the aspx:
3
2867
by: John Crowley | last post by:
I've run into a problem where drop down lists are refusing to data bind with the following error on the DataBind() call Specified argument was out of the range of valid values. Parameter name: value Stack trace is showing the error in OnDataBinding event, which I don't hook in the cases where I'm getting this error ArgumentOutOfRangeException: Specified argument was out of the range of valid values Parameter name: value...
1
2579
by: Gopal Krish | last post by:
I'm have coded a simple menu (using link buttons as menu items) in a user control to be reused across many ASPX pages. In the page_load method I dynamically create the link buttons as follows LinkButton myLB = new LinkButton(); ......... .........
4
3270
by: Keith Patrick | last post by:
I'm running into a wall with a custom control that gets populated via asynchronous I/O. Basically, my control takes its datasource and renders some controls that have events (OnClick). However, there's the well-known problem of dynamic controls not firing unless they are created during the page's initialization, so OnPreRender is too late to create any dynamic controls. That being the case, how can I populate a control asynchronously...
8
2091
by: novus | last post by:
Hi, In ASP.net 2.0 I make a control which add the same controls dynamically. In the oninit event I add the controls to the controls collection. After that the loadviewstate event fills in the information on postbacks. The control can add and delete controls that is why on the postback I don't know how many controls there are. At the moment I am able to get the controls rendering but I have problems to save the count of the dynamic...
6
4407
by: Peter Zolja | last post by:
Hi, I'm building a webcontrol that contains a dynamic list of other controls. My problem is that when I add or remove an item the synchronization between the ViewState and the Controls collection seems to break -- or at least that's my theory for now. Here's what I do; to add an item I do that following on the PostBack of a button: 1. Create a Button object and set its properties
23
12533
by: LvBohemian | last post by:
I am playing around with creating some menus dynamically, and they create fine and show up ok when I want them to; but when I select a menu NavigateUrl at run time, the applicable url shows up as anticipated in the content place holder but the menu disappears... the menu is part of the master page and is not in the
5
6755
by: Amoril | last post by:
I've read quite a few different message on various boards and for some reason I'm still having trouble wrapping my head around this viewstate maintenance and trying to get these dynamically created link buttons to stay wired up to their click events. I have what is basically a simply survey question generation page. The page first displays a few static fields and a dropdownlist of various options for the user to select. When the user...
0
8376
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8290
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8708
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8594
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7307
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6161
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4294
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2716
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1916
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.