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 : 20

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!

ansrikanth : On May 19, 2008 8:01 AM said:

Hi Joe,

I do strongly agree with JohnYahoo... Yes, Video will definetly gives good impact than a simple article, the way you are telling abt each and every line is really impressive.

Thats really great job from you..

I have already commented about this in the blog..

You have gone through a static variable.. which is really dangerous in Web applications( as per my best of knowledge ).. isn't there any other possiblity w/o using static.. i have tried Session variable but it failed, co'z i read some where that, Session varible will be come into picture after the page gone through complete life cycle.. not at the time preinit..

Please do suggest me the work around for this...

thanwa : On May 21, 2008 3:52 PM said:

Hi Joe,

Your presentation is awesome. Thank you very much. But have you ever got the following error?:

===================================

System.InvalidOperationException was unhandled by user code

 Message="RegisterRequiresControlState can only be called before and during PreRender."

===================================

Error occurs when I try to add new control to PlaceHolder.

I used the same approach but I'm working with Gridview instead of Textbox. I can render the page at the first load but when I hit the button to add more Gridview into the page, I got this error. Here is the section of the code I use to add every Gridview to PlaceHolder:

===================================

       Dim gv As GridView

       For Each gv In loadedGridView

           myPlaceHolder.Controls.Add(gv)

       Next

===================================

loadedGridView is an array of Gridview and I create it during  "OnInit(ByVal e As EventArgs)".

I'm not sure if I should put the whole source code here but if you need to see the full source, I can do it.

Thank you,

Thanwa

jagannath : On June 25, 2008 5:01 AM said:

Hi Friends,

I would like to need complete video tutorial of expreesion blend,vs2008,silverlight2 beta step by step..

Thank u

jimdgilbert : On June 27, 2008 5:31 PM said:

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

Why video? Why not regular article?

Did SergeyS happen to notice that he was in the ASP.NET AJAX Videos › section??

Why on Earth would you browse the video section, and then complain because there is a video, IN the video section??

Joe, you just keep banging out the videos.  For me, it shaves tons of time off figuring out how to use tools when I watch you step me through them in the videos.

Thank you for all you do for us.

zcumbag : On July 09, 2008 10:33 AM said:

What about removing an item? I thought it would be fairly similar but I end up with errors...

mark_bert : On July 17, 2008 4:25 PM said:

I'm in process of implementing the ideas in the presentation inside a wizard control with user controls. Regarding several posts in this thread, here are a couple thoughts:

1. The use of STATIC for the counters holds the state across multiple users/sessions. I implemented a session variable that seems to be working fine.

2. To use user controls, you need to add a reference to the control in your .aspx page like this:

<%@ Reference Control="userControls/myUserControl.ascx" %>

Then in your codebehind reference the class from the UC like this:

myUserControl offenderAddress = (address)Page.LoadControl("userControls/myUserControl.ascx" );

lbk : On August 04, 2008 8:48 AM said:

Hi,

I have a problem getting the code to work, when placing the “Add TextBox” button in a tabcontainer, the GetPostBackControl returns null, causing the textbokses to loose content, and no textbokses are added.

Everything works fine when the button is placed ouside the tabcontrol.

Any ideas?

John Jiang : On August 04, 2008 3:30 PM said:

what this sample has anything to do with Ajax? We can do this in asp.net 1.0

cv_vikram : On August 08, 2008 9:18 AM said:

Its a good tutorial thanks....

JonasButt : On August 16, 2008 8:29 PM said:

I don't really think this a best practice of how one should dynamically add controls to a ASP.NET web page.

1. As pointed out in the first comment, using the global counter variable will not work well when multiple users will be using the page simultaneously. Storing this in the Session object would resolve this.

2. Furthermore, why would you create such an elaborate piece of code to determine what caused the page request? Isn't a click event handler of the button fit for this purpose?

Leave a Comment

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

Page view counter
Microsoft Communities