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.

28 Comments

  1. Kevin January 29, 2009 Reply
  2. Muhammad Adnan January 29, 2009 Reply
  3. Thanigainathan.S January 29, 2009 Reply
  4. Naresh January 29, 2009 Reply
  5. Anonymous January 29, 2009 Reply
  6. g2k January 29, 2009 Reply
  7. Jorge January 29, 2009 Reply
  8. interpeo January 29, 2009 Reply
  9. Raj January 30, 2009 Reply
  10. Rajeev January 30, 2009 Reply
  11. Follower January 30, 2009 Reply
  12. Ravi Roy January 30, 2009 Reply
  13. khalid January 30, 2009 Reply
  14. C January 30, 2009 Reply
  15. Andy January 31, 2009 Reply
  16. Salman Farsi February 1, 2009 Reply
  17. vdx660 February 2, 2009 Reply
  18. hassan February 3, 2009 Reply
  19. Sandeep February 4, 2009 Reply
  20. Russell February 4, 2009 Reply
  21. kirthika February 22, 2009 Reply
  22. venkat February 26, 2009 Reply
  23. Jerome Dimairho March 11, 2009 Reply
  24. zahy April 13, 2009 Reply
  25. Ehsan May 8, 2009 Reply
  26. Hill Hill July 8, 2009 Reply
  27. Gaurav Jain October 8, 2009 Reply
  28. Nitin Goel May 5, 2010 Reply
  29. Adding Controls during Runtime ( Dynamically ) in ASP.NET | ProgramInDotnet August 7, 2011 Reply

Leave a Reply


x

Interesting BlogPosts to read

Passing Array of Integers to ASP.NET Web API REST Service
You can pass an array of integer to an ASP.NET Web Web API REST service by setting the [FromUri] att...
Event on Building intelligent bots with the Microsoft Bot Framework in Manchester
Want to learn about building intelligent bots using Microsoft's Bot Framework. Here's an interesting...
Barracuda Introduces Email Threat Scan for Microsoft Office 365
With the number of advanced threats on the rise and affecting companies of all sizes, Barracuda toda...