Dynamically created Web User Control and ViewState problem

A

A

Hi everybody,

I'm building a Web page in ASP.NET / C#.

On that Web page, there is a button that when clicked will add a Web User
Control (contains some labels, textboxes, radio buttons, and such). The user
can click that button many times, each time adding one additional Web User
Control. The user can also click another button to delete one of the Web
User Control.

The problem I'm having is that the ViewState data sometimes won't match with
the correct Web User Control on postback (e.g. happens if I delete a middle
Web User Control).

I did some searching for the solution online, and I understand that the
problem I'm having is normal because of the way the ViewState works. But I
can't find the solution. I even tried to rename the IDs of all my controls
within that Web User Control.

I hope someone can point me in the right direction. Thanks.
 
W

Wilco Bauwer

It is probably the best idea to keep track somewhere of the controls
you need to display. Then you could databind that information against a
databound control, each time you updated that information. A databound
control like a Repeater will take care of ensuring that the controls
are still in-tact after each postback.

I have just posted a post related to dynamically created controls,
since I see a lot of people struggling with problems related to that.
You can read it at
http://wilcoding.xs4all.nl/wilco/View.aspx?NewsID=147.
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top