Adding Controls during Runtime ( Dynamically ) in ASP.NET

Well i have been wandering around here and ther for quite a few days in understanding how to dynamically create the controls in ASP.NET during the runtime in VS 2008.

All it ended out to be a very simple one.

This is achieved through the methods Controls.add that is available with the panel or with each page.

Well this works fine with panels and few other Containers but special care has to be taken when adding to the page.Controls.I l update regarding the same in some time.

All that i have in the page is a simple Button and a Panel. When i click on the Button i need to generate a set of Controls in the Runtime.

Code Behind Page (.cs )

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class Default2 : System.Web.UI.Page
{
Label lb1;
TextBox txt;
Button btn1;
protected void Button1_Click(object sender, EventArgs e)
{
lb1=new Label();
txt = new TextBox();
txt.ID = "txt1";
btn1 = new Button();
btn1.ID = "bt1";
btn1.Text = "Click";
Panel1.Controls.Add(txt); Panel1.Controls.Add(btn1); Panel1.Controls.Add(lb1); 
lb1.Text = "Dynamic";
}
}

Well this seems to be easy but there are a lot of things to be explored in the same which i have been trying out and update frequently…………..

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

30 Responses

  1. Just because you can do this doesn’t mean that you should. It is a VERY BAD idea to add controls at runtime like that. The more this is used to more difficult it becomes to rework the page. Develop for the person that is gonna have to debug your page, not for you right now. Avoid this at all costs!!!

  2. I don’t know why it got considered as article of the day @ asp.net 😀 anyhow you added all the controls how about managing them as after postback by other button they will vanish and how about data in them say grid…….. its long story man and long topic to cover up.

    Anyhow good luck

  3. Thanks for the info. I am a beginner in ASP.NET and your article has given me inspiration that I can also write Article of the day stuff.

  4. I’m not a big fan of dynamic control because of the viewstate. For instance, the control is generated when the button_click event is handled. Subsequent page load may lose those controls.

  5. Hi, good article, specially for beginners.

    @Kevin, I’m quoting:
    “It is a VERY BAD idea to add controls at runtime” can you further explain what you mean by “bad idea”???

    My web apps would be very poor without the runtime “Controls.Add” magic tool!

    Thanx

  6. Can anyone tell me the criteria to get article published on ASP.net …???? I can’t believe an article of this type is recognized by the no. 1 website on ASp.net.

    I can write much better article than this…!!!

  7. Hi,

    I think it’s a Incomplete article. i appreciate senthil for writing an article and publish in asp.net website. If you add button we should need to handle it Click event at least. We need to preserve the dynamic controls if the page post back. We can’t say this an article, but a small tip for how to create asp.net controls at run time.

  8. That is a bad idea?
    Why you think that is a “client” and not use the server processes unless imprescendible. For that, there is the Javascript, Ajax etc. ..
    We talk about webforms, not WinForms.
    (and the code is “simple” for newbies very good applications for Windows)

  9. Creating controls at runtime you will come accross several setbacks here are some of them:

    1.> Client Side validation (Using Required field etc validator.

    2.> You will have to redraw the controls everytime even on postback.

    3.> If there are hundereds of such controls in a page you will face hell in load testing.

    I suggest it is best to have controls hidden inside a div and making them visible on demand using javascript.

    Anyone interested just mail me and I will sed the sample.

  10. “Can anyone tell me the criteria to get article published on ASP.net …???? I can’t believe an article of this type is recognized by the no. 1 website on ASp.net.

    I can write much better article than this…!!!”

    This is a little bit harsh don’t you think. The author’s intent was not get recognized as the article of the day on some site. The authors intent was to share some information. At least he/she put their idea out there. Learn from it, improve it, and I’m sure the author would address constructive criticism or comments. I personally would tell you to sod off.

  11. Hey Everyone,

    It is very easy to work with controls created like this. You have to realize that if you try to read the objects in during the page_load event, they will disapear. You have to read them during the PreInit() event.

    Once you do that you can manage the objects just fine.

  12. Hi Everyone,

    It is good article for the beginner’s. But adding controls like this way always create some serious issues when you are going to validate this kind of controls to client side or want to work at client side. So, special care needs to add controls at runtime.

    Thanks

  13. You can put a routine to restore the controls add on the Page_Load and the controls will not disappear every time th page is loaded. I was able to make a website which had functions similar to WebParts but without the stupid aspdbnet.mdb on the App_Data folder.

  14. This article might have been relevant 9 years ago when we were all learning the new framework, but hardly is it worth posting on asp.net. The vast range of knowledge in our “profession” is mind boggling. I’m sure lots of people reading asp.net are not professionals, but should those people really be posting tutorials? Also, most of the responses to this article are so wrong. The following comments are flat out wrong:

    “Just because you can do this doesn’t mean that you should. It is a VERY BAD idea to add controls at runtime like that. The more this is used to more difficult it becomes to rework the page. Develop for the person that is gonna have to debug your page, not for you right now. Avoid this at all costs!!!”

    “Very simple any way good work.”

    “Creating controls at runtime you will come accross several setbacks here are some of them:

    1.> Client Side validation (Using Required field etc validator.

    2.> You will have to redraw the controls everytime even on postback.

    3.> If there are hundereds of such controls in a page you will face hell in load testing.

    I suggest it is best to have controls hidden inside a div and making them visible on demand using javascript.

    Anyone interested just mail me and I will sed the sample.”

  15. That’s textbook coding 4 u. lol… It only works in the classroom. Reminds me of where I came from. Nice one!

  16. that’s well, but did u try to attach events to these controls?, what happen when page is postback , i think u still have to work hardly for it.

  17. The page’s ProcessPostData event is fired before your “Button1_Click” event. So, you won’t be able to use any server-side event of your dynamically added TextBox or Button.

    In other terms, you create a Button, but too late in the page lifecycle to be able to respond to the future click on this button.

    This is an awful code, don’t use it.

    Solutions :
    – if you can, add the controls to the form in the Page_Load, Page_InitComplete or Page_Init event.
    – if you can’t, declare a static invisible ASP.NET Panel in the page with your controls in it, and set the panel’s Visible property in the Button1_Click event.
    – if you can’t AND if you can’t know in advance how many and which controls to add dynamically, use Javascript to set and HiddenField value in the “onclick” client Function of the Button1. In the Page_Load, test the hiddenfield value to know if you clicked the button or not to add the controls as needed.

  18. Adding controls at runtime is not too difficult…
    but i want to know that how can i add event…on that run time added control…

    Please help me out ot this….
    and plzzzz reply me at

    guddu28@gmail.com

  19. Hi,
    I have a panel with all control and has design panel as per client requirement. at the inside panel there are one button name “add more”. when you click add more button the same panel comes after first panel with all the same control design structure.Please let me know how to add a panel with same control and how to i will read second panel control value in Data table. i can click more than time add more button, however multiple panel will show on same page with same control design.Please help me at er.nitin123@gmail.com

  20. Pingback: Adding Controls during Runtime ( Dynamically ) in ASP.NET | ProgramInDotnet

Leave a Reply