How To Dynamically Add Controls to a Web Page

In this video I will demonstrate a technique to dynamically add an indeterminate number of controls to your web page.

Presented by Joe Stagner

Duration: 31 minutes, 4 seconds

Date: 16 April 2008

Watch the video   |   Download the video   |   Get VB code  or  C# code

Video downloads: WMV | Zune | iPod | PSP | MPEG-4 | 3GP

Audio downloads: AAC | WMA | MPEG-4 | MPEG-3 | MPEG-2

Comments : 10

Leave a Comment

Plague2006 : On April 18, 2008 7:59 AM said:

Hi,

Some questions I have regarding this documentry. I assume that Shared keyword is the equivalent to Static in C#. And under that assumption, I raise the question on how the value modification will affect when multiple users are using the page simultaneously. When the value is increased by one user clicking on the button, won't that affect the other users who may not have clicked the button?

I'm not criticizing but trying to understand. I hope you can clear it up for me.

DamijanD : On April 18, 2008 3:51 PM said:

Yes in real world static would cause troubles. You should use session instead.

SergeyS : On April 18, 2008 11:10 PM said:

Why video? Why not regular article? Even more extensive comments of the code would be much better off. Anyway you've spent more than half an hour on the video.

How this is to be handled by search engine?

Why is it better for a developer to spent a lot of time downloading 100M video, then spend 0.5 hour to look through it, than just to spend 5 min to grasp the written idea?

Millions of ASP.NET developers can't get spoken English. Is it oficial MS position to phase them off the MS road?

loveASPNET : On April 19, 2008 6:00 AM said:

Hello Joe,

Your presentation was really cool.

Can you demonstrate the same with the use of usercontrols.

like how we can add usercontrols dynamically and keep the viewstate of the usercontrols inbetween postbacks.

Thanks,

Robin

Dollarjunkie : On April 20, 2008 2:20 AM said:

Hi Joe, following the request from the last poster, I would appreciate it if you actually give some information on dynamically adding Usercontrols and removing usercontrols from page.

JustAnotherGuy : On April 21, 2008 10:56 AM said:

Hi Joe, Just tried building a page like the one in this video but using a dropdownlist instead of the button.

But I am getting an issue in the GetPostBackControl function. The ctrlName property populates with the clientid of the dropdownlist control but then the findcontrol function return nothing when looking for it. I have tried stripping the ctrlName down to just the dropdownlistname by breaking the returned name into an array but it still returns nothing.

My page uses a master page but all of the code and controls are in the aspx and codebehind of the aspx.

Any thoughts?

JustAnotherGuy : On April 21, 2008 11:48 AM said:

The demo code from this video does not work once you place it all in an aspx page that has a master page (even if the master is completely empty).

The FindControl in GetPostBackControl returns nothing.

Does anybody have any thoughts on how to resolve this? This is useful code but I need it to work in a masterpage situation.

kentto : On April 23, 2008 1:04 AM said:

@JustAnotherGuy

I'm assuming it's not finding the controls because the ContentPlaceHolder is a container control that's on the Master Page and not on the Web Form, try passing the Master Page instance by changing the line in the Page_PreInit to

Control myControl = GetPostBackControl(Master.Page);

and the comparison to take into account that the container will be prefixed to the control name of btnAddTextBox to something like

if ((myControl.ClientID.ToString().Contains("btnAddTextBox")))

and see if that helps.

JohnYahoo : On April 23, 2008 1:40 PM said:

Joe

This is a superb demonstration of something I have been trying to do for some time.  In the past I have had to resort to fat client programming to do this.  Thank you.  Thank you.

There is a saying "No good deed goes unpunished" so I must address some of the comments above.

>>>"Millions of ASP.NET developers can't get spoken English."<<

I often watch these videos with the sound turned off.  You can still get everything that happens if you watch closely.  Isn't this video format ideal for people of all languages?  I think so.

>>"Why video? Why not regular article?"<<

There are plenty of regular articles out there.  None of them can show the complete picture of the solution like a video.  The video leaves nothing to figure out.  It is all there, even if the sound is off.

Somebody is going to complain, regardless of what you do.  If it is visual, the blind people will be hurt.  If it is audio, the deaf people will be hurt.  If it is available on some day of the week, it offends somebody's religion.

There would be fewer complaints if the language were Esperanto, because almost nobody would view the page.  And why not Cobol?  Or RPG?  Don't you know you are offending the Linux community?

This has become one of my favorite web sites.  I would like to see it survive and thrive.  It really hurts me when people slam it with bogus unfounded criticism.

jonny75904 : On May 02, 2008 3:04 PM said:

Hi Joe,

I am trying to use this example to create tabs dynamically. The C# source does not build (it says MyPlaceHolder does not exist in the current context).  

I'm fairly stupid and am unable to solve this problem. HELP!

Leave a Comment

You must be logged in to leave a comment. Click here to log in.