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

13 Comments

  • Good stuff...thanks for providing the code.

  • Excellent work!

    Thank you.

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

  • 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)

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

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

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

  • 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.

  • 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

  • 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.

  • 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 :)

  • Thanks so much for your example.

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

    Very usefull.

  • 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!

Comments have been disabled for this content.