Wednesday, April 23, 2008 12:53 AM InfinitiesLoop

TRULY Understanding Dynamic Controls by Example

Many of the comments I've received in the various dynamic controls entries I've written have been questions for help with a specific scenario. A lot of those scenarios are similar. One in particular I keep hearing is something as follows:

The user is presented with a drop down list of potential items to add to a list of items. As they select an item, it is added to the list. A different type of control is designated to represent each type of item the user can select. Each control provides a unique user interface that is unique to the type it represents.

Dynamic Controls Scenario

The uniqueness of the scenario is that there is a different control designated to 'represent' each data item, depending on the type of that data. The data itself might also be editable within each control.

It is of course necessary to 'dynamically' load the control responsible for each item, since you can't know what they are ahead of time. You also don't know how many there are, and they can be added and removed by the user in the meantime.

Rather than discuss designs for such a page at length (boring!!!), I decided to implement the scenario in a sample application. Also great for really understanding things, I implemented it in 3 different ways, each one progressively better than the last. Hopefully that helps with not only understanding dynamic control scenarios, but understanding alternatives to them or ways of minimizing their use when possible. If a picture is worth a 1000 words, a sample application is worth a 1000 blog entries, I say! Please have a look!

Dynamic Controls By Example Sample Application

Download the sample application here:

TRULY Understanding Dynamic Controls By Example

Filed under: , ,

Comments

# re: TRULY Understanding Dynamic Controls by Example

Thursday, April 24, 2008 1:00 AM by kemaltolga

Good stuff...thanks for providing the code.

# re: TRULY Understanding Dynamic Controls by Example

Saturday, April 26, 2008 4:15 PM by Andrei Rinea

Excellent work!

Thank you.

# re: TRULY Understanding Dynamic Controls by Example

Thursday, May 29, 2008 9:39 PM by InfinitiesLoop

Rose -- it already runs with 3.5. What problem are you having?

# re: TRULY Understanding Dynamic Controls by Example

Friday, May 30, 2008 4:44 PM by Rose

Yes it does.

I did warn you I was a complete newbie ;)

To anyone interest the problem was/is with visual studio 2008. The error is related to the project virtual folders. I tried it with a visual studio installation at work and it went along fine.

Last night I googled about it and searched around lightly on vs2008 for the related sections, but it was late and most information is related to iis folders. Anyways should be pretty simple to fix Ill post it around here for other newbies.

By the way, the error is:

It is an error to use a section as a section registered as allowDefenition='MachineToApplication' beyond application level.

Thank you Infi for the great articles, I banged my had all day long at work figuring it out but you made it ho so much more fun with good guidance.

PS1 : Cant wait for part 4

PS2: I haven't got a clear picture yet. But from the small talks at work I dont think I can charm those knuckle heads at work to use repeaters :((( wich is only gona give me more head pains figuring out theyr system. (they even made theyr on orm based on datasets. dear god)

# re: TRULY Understanding Dynamic Controls by Example

Saturday, June 21, 2008 11:31 PM by Vamsee

Thanks for the post! I was hoping to see the code but the link to download the sample application seems to be broken.

# re: TRULY Understanding Dynamic Controls by Example

Sunday, June 22, 2008 11:54 PM by InfinitiesLoop

Vamsee-- its working for me. Try again, my ISP may have had some downtime. It wouldn't be the first time.

# re: TRULY Understanding Dynamic Controls by Example

Thursday, July 10, 2008 12:25 AM by AnthonyV

Sweet! I love you! (I'd kiss you, and I don't even swing that way!)

Your code had *just* the right info I was looking for; I've been banging my head against the keyboard for days.  (.. makes for messy code)

Thanks so much, and keep up the awesome work!

# re: TRULY Understanding Dynamic Controls by Example

Thursday, July 10, 2008 1:56 AM by InfinitiesLoop

AnthonyV -- cool, glad I could help. First time I've gotten such an offer. Thanks?

# re: TRULY Understanding Dynamic Controls by Example

Saturday, October 11, 2008 12:09 PM by Stephan

Very usefull. I'm still studying the code. Thanks for the example(s).

Just wondering (not tried it yet), but would it be possible to use this same code to nest user controls. For example use a repeater in the usercontrol containing other custom usercontrols. So add a button in the UC to add an item to the repeater on the UC, which itself is also dynamic.

Just curious.

Thanks for any response.

# re: TRULY Understanding Dynamic Controls by Example

Wednesday, November 12, 2008 4:00 AM by Kris

Hey Dave,

I was wondering why the Viewstate grows so dramatically when using dynamic controls.

If I change one of the controls in the above sample to include a gridview with following data.

    ArrayList data = new ArrayList();

       for (int i = 0; i < 90; i++)

       {

           data.Add("Some data to fill up the ViewState lots of data ...... to fill up the gridview yadadadada");

       }

       gvItems.DataSource = data;

       gvItems.DataBind();

With the above data our viewstate gets really dirty.

After reading your "Truly understanding ViewState" article I can only conclude that the viewstate is that large because adding a control makes the placeholder statebag marked as dirty ?

Hopefully you can provide us some insights on this ?

Thx in advance

# re: TRULY Understanding Dynamic Controls by Example

Wednesday, November 12, 2008 4:38 PM by InfinitiesLoop

Kris -- thanks for reading :) ViewState is big in this case probably because your 'lots of data' is being bound to the GridView, which then inserts that data into the data fields you have defined (or the auto generated ones), which is done in a way so that if you dont re-bind the GridView on the next request it is preserved. If you want to bind it every request you should disable viewstate on the GridView, though you can run into problems with some of GridView's features when viewstate is off. If all you really want is a read-only view of the data (e.g. no EditItemTemplate, no SelectedItem, nothing like that), then defintely turning off ViewState and binding all the time is the way to go.

# re: TRULY Understanding Dynamic Controls by Example

Thursday, November 13, 2008 10:00 AM by Kris

Hey Dave,

Thx for the reply.

We already disabled the viewstate on the grid it was something else.

Somebody used a datakey on the gridview which contained a large set of object which got serialized in the viewstate.

Removing this key got your viewstate from 400 Kb to 20 Kb :)

# re: TRULY Understanding Dynamic Controls by Example

Monday, July 20, 2009 4:45 AM by Titom

Thanks so much for your example.

I loose a lot of time searching  "SHIFTING ID" PROBLEM.

Very usefull.

# re: TRULY Understanding Dynamic Controls by Example

Monday, November 16, 2009 2:43 PM by Jean-Michel Donatelli

OMG!!! A thousand thank you to you my friend! Thanks to your sample, I was finally able (after over a week of research) to make my page work with controls dynamically added to a gridview.

Thanks again!