Friday, 2 April 2010

Dynamic User Controls – Part 1 – Session Intro

I recently gave three talks around Ireland on this topic for the Irish Microsoft Technology User Group (MTUG). I travelled to Derry, Dublin and Cork over three days and met some great folks. I was also very lucky to be presenting on the same night as Adam Kinney from Microsoft which was an excellent chance for me to get some news on Silverlight and the new bits from MIX10. A big thanks to Alan Bradley, David Gargan and Joe Gill on the MTUG side and Martha Rotter and Enda Flynn from Microsoft for facilitating this and also making sure I got to where I was supposed to go.

As promised to all those who attended my talks I am posting the session as a series of blog posts along with the bits from the sessions. The bits can be downloaded in 3 versions

If you are using the Visual Studio 2010 RC version make sure to download the Designer patch for the RC. This version will also work with the Beta 2 version of Visual Studio 2010.

So on with the speaker bits. I am not putting the PowerPoint online as it does not contain anything other than pictures and some text and its pretty useless without the speaker notes. So I am going to instead to put the speaker notes in this blog post with the PowerPoint slides as images.


The first question you should be asking is why do you want user controls in the first place. And the answer is usually one of the following three options

  • You don’t know the number or type of controls that you will need until runtime. This is possibly the most common reason to use dynamic controls.
  • You have a large number of the same type of control that you need to add to the application and you don’t want to spend the time manually putting them onto the design surface. Most people will be familiar with this because I doubt you have ever manually created 100 list items in a DataList for example
  • And finally its because of an architectural decision such as a custom server control and you have no choice.



The next question is what is the different between static and dynamic controls. Well firstly static controls StaticVsDynamicControls 

  • Can be added to the design surface at design time
  • They can be programmed against with intellisense because they are in the .designer.cs file
  • And finally they survive post back and the page life cycle.

On the other hand dynamic controls

  • Dynamic controls are added to the page control tree at runtime
  • They cannot be programmed against using intellisense in the traditional way. You can declare an object of the type of control and bring it to your instance of the control and program against it in that way
  • Dynamic controls do not survive the post back unless you give them a place to stay. And that is the crux of the problem involving dynamic controls.



Now that we know what the issue is how do we solve it. Normally when you are using dynamic controls you add the controls to a placeholder and this works ok to a point. A better way of doing it is to use a repeater with a placeholder in the ItemTemplate and use custom methods of the ItemCreated and ItemCommand events. There are a few good reasons for this.

  • You can databind your list to your repeater allowing to quickly create a lot of controls
  • You can make your controls have databinding properties.
  • You can use the fact that the repeater is IEnummerable to get the information in and out
  • The ItemCommand event allows you to handle events from your controls
  • And finally it gives you a way to easily store state by using the repeater.



So that was the intro part of the session. As you can see I didn’t use a lot of time in PowerPoint because I believe that developers learn better from seeing the code rather than from a slide deck. The next posts will be all on the code parts of the sessions.

If you want to learn more about the whole viewstate relationship with dynamic controls you can read Dave Reed‘s excellent blog posts on the subject. I would like to take this chance to thank him for his kind permission in allowing me to use some of his material for my sessions.

No comments: