This is a discussion on How can we Dynamically create web controls within the ASP and ASP.NET Programming forums, part of the Web Development category; Hello, How can we create web controls dynamiclly? How can we change the size of the controls such as height ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| Hello, How can we create web controls dynamiclly? How can we change the size of the controls such as height dynamically? |
| Sponsored Links |
| |||
| Hi, You can dynamically create web Control.. public class WebForm1 : System.Web.UI.Page { // Added by hand; will create instance in OnInit. protected System.Web.UI.WebControls.TextBox TextBox1; protected System.Web.UI.WebControls.TextBox TextBox2; override protected void OnInit(EventArgs e) { // Create dynamic controls here. // Use "using System.Web.UI.WebControls;" TextBox1 = new TextBox(); TextBox1.ID = "TextBox1"; TextBox1.Style["Position"] = "Absolute"; TextBox1.Style["Top"] = "25px"; TextBox1.Style["Left"] = "100px"; form1.Controls.Add(TextBox1); TextBox2 = new TextBox(); TextBox2.ID = "TextBox2"; TextBox2.Style["Position"] = "Absolute"; TextBox2.Style["Top"] = "60px"; TextBox2.Style["Left"] = "100px"; form1.Controls.Add(TextBox2); // // CODEGEN: This call is required by the ASP.NET Web Form Designer. // base.OnInit(e); } This code dynamically creates two TextBox controls, sets their IDs and positions, and then binds them to the Form Controls collection. The code also wires up the TextChanged events of the text boxes to a handler (TextBox_TextChanged). private void Page_Load(object sender, System.EventArgs e) { if(!IsPostBack) { // Set the initial properties for the text boxes. TextBox1.Text = "TextBox1"; TextBox2.Text = "TextBox2"; } } } Last edited by poornima : 03-14-2008 at 09:14 PM. |
| |||
| private void Page_Load(object sender, System.EventArgs e) { CheckBox _checkbox = new CheckBox(); _checkbox.ID = "chkDynamicCheckBox"; _checkbox.Text = "This is a dynamically generated checkbox"; Panel1.Controls.Add (_checkbox); } (or) private void Page_Load(object sender, System.EventArgs e) { for ( int i = 0; i < 5; i++ ) { TableRow tr = new TableRow(); // Create column 1 TableCell td1 = new TableCell(); // Create a label control dynamically Label _label = new Label(); _label.ID = "lbl" + i.ToString(); _label.Text = "Enter Value " + i.ToString(); // Add control to the table cell td1.Controls.Add(_label); // Create column 2 TableCell td2 = new TableCell(); TextBox _text = new TextBox(); _text.ID = "txt_" + i.ToString(); // Add control to the table cell td2.Controls.Add(_text); // Add cell to the row tr.Cells.Add(td1); tr.Cells.Add(td2); // Add row to the table. tblDynamic.Rows.Add(tr); } } |
![]() |
| Thread Tools | |
| Display Modes | |
| |
LinkBacks (?)
LinkBack to this Thread: http://www.discussweb.com/asp-asp-net-programming/5268-how-can-we-dynamically-create-web-controls.html | |||
| Posted By | For | Type | Date |
| DiscussWeb IT Community - Technical Support and Technology Discussions | This thread | Refback | 02-16-2008 09:57 PM |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| ASP.NET Data Controls | Venkat | ASP and ASP.NET Programming | 133 | 10-11-2008 08:41 PM |
| How Create print preview page dynamically in ASP.NET | KiruthikaSambandam | ASP and ASP.NET Programming | 4 | 03-21-2008 04:42 AM |
| Code-behind to create node dynamically using XML File or any Stored Procedure | poornima | ASP and ASP.NET Programming | 3 | 02-14-2008 09:46 PM |
| WebParts controls | S.Vinothkumar | ASP and ASP.NET Programming | 5 | 11-16-2007 03:06 AM |
| Create database object with dynamically | S.Vinothkumar | C# Programming | 6 | 09-28-2007 11:49 PM |