473,406 Members | 2,956 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,406 software developers and data experts.

Dynamic Controls and ViewState Management

I do admit I am fairly new to ASP.NET. With that, I've also poured over dozens of websites and forum posts, sifted through pages of text, and spent hours stuck on this problem. I'm now pleading for your help. Here's the deal. I have a form that by default has this:
URL to be unblocked: __________________

The underline being a text box. Below this field, I have a text link that says "Add another", which essentially creates this:

URL to be unblocked: __________________

__________________

Again, both underlines being text boxes. The user can continue to hit "add another" as many times as he/she would like, dynamically creating many textboxes. On the code-behind, I have a property called URLControlsCount that gets and sets a viewstate variable that tracks how many textboxes should show up (if the variable does not yet exist in the ViewState, it returns 1 so that at least one textbox will always show up, as you will see below). Now, on Page_Init, I call a function called CreateURLControls() which contains the following:

Expand|Select|Wrap|Line Numbers
  1. Dim i As Integer
  2. For i = 1 to URLControlsCount
  3.         Dim newTextBox As TextBox = New TextBox()
  4.         newTextBox.ID = "url" + i.ToString()
  5.         URLFieldsPlaceHolder.Controls.Add(newTextBox)
  6. Next
The Add Another link runs the following code:

Expand|Select|Wrap|Line Numbers
  1. Dim newTextBox As TextBox = New TextBox()
  2. URLControlsCount += 1
  3. newTextBox.ID = "url" + URLControlsCount.ToString()
  4. URLFieldsPlaceHolder.Controls.Add(newTextBox)
Now the problem I'm having is that, because I call CreateURLControls() from Page_Init, the ViewState has not been loaded, therefore URLControlsCount doesn't hold anything and so the wrong number of textboxes is created. If I move the CreateURLControls() call to Page_Load, it creates the right number of controls, but then if I want to get the value of the textboxes by using ctype(URLFieldsPlaceHolder.FindControl(url1), TextBox).Text, I get an empty string...I suppose because the ViewState has not been "bound" to the controls yet (not until the end of Page_Load). So I'm stuck in the predicament.

Can anyone shed some light? Do I need to store URLControlsCount in a session variable or something to get around the issue? Thanks!!
Jun 19 '07 #1
0 970

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Leo J. Hart IV | last post by:
OK, here's another question for the experts: I am building a multi-step (3 steps actually) form using a panel for each step and hiding/displaying the appropriate panel/panels depending on which...
2
by: geodev | last post by:
Hello, I’m currently writing an asp.net application that will be running on a Windows XP Professional workstation utilising IIS and MSDE Database. At a later date this application will need to...
7
by: John | last post by:
Hi all, I need finality on this once and for all please. I have a main page which contains a couple of placeholders and within these placeholders, depending on what the user presses, I load...
4
by: Larry Grady | last post by:
Anyone up for a challenge? I've been struggling with this for a few days and was hoping someone could help me. Pouring through all the messageboards I just can't find the solution. We have a...
1
by: Andrew Robinson | last post by:
I have a <asp:table> control with a large number of dynamically created LinkButtons. I am using the command event, command name and command argument values in my LinkButtons to trigger actions...
4
Frinavale
by: Frinavale | last post by:
Introduction Sometimes, when developing web applications, we need to be able to dynamically load controls based on user selections. The following article describes a simple scenario where TextBox...
0
by: Scott Roberts | last post by:
I always thought that the viewstate "keys" included the control ID. As long as the control IDs were unique, there shouldn't be any conflicts. Well, it appears that that may not be the case with...
1
by: MaryamSh | last post by:
Hi, I am creating a Dynamic Search in my application. I create a user control and in Page_load event I create a dynamic dropdownlist and 2 dynamic button (Add,Remove) By pressing Add button...
0
by: MaryamSh | last post by:
Create Dynamic Dropdownlist Controls and related event -------------------------------------------------------------------------------- Hi, I am creating a Dynamic Search in my application. I...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
0
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...
0
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
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,...
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...

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.