Dynamically creating ASP.NET controls from the code behind files

您所在的位置:网站首页 valmm Dynamically creating ASP.NET controls from the code behind files

Dynamically creating ASP.NET controls from the code behind files

2024-07-04 07:27| 来源: 网络整理| 查看: 265

Dynamically creating ASP.NET controls from the code behind files Posted Date: 15 Dec 2004 |Updated: 15-Dec-2004 |Category: ASP.NET/Web Applications |Author: Tony John |Member Level: Gold |Points: 15 |

This article demonstrates how you can dynamically generate ASP.NET controls in runtime using the code behind code and access the properties of the dynamically created controls.

Dynamically creating ASP.NET controlsASP.NET is a very powerful and flexible technology, allowing programmers to develop sophisticated web applications within a short period of time. It is so easy to drag and drop controls in a web form and start writing the business logic in the code behind file.Ofcourse, dragging and dropping controls from the toolbox is the easiest approach. But sometimes we may need to generate ASP.NET controls in the server side using the VB.NET or C# code. For example, consider a case where you are developing an application where you want to display a list of checkboxes to the user, depending on the number of records in a database table. In this case, you cannot drag and drop few checkbox controls, because you will not know how many checkboxes you are going to need until you retrieve the records from the database.

In this article, I will discuss how you can create ASP.NET controls at runtime using C# syntax. You can easily convert this to VB.NET syntax with little effort.Create a new ASP.NET web application using Visual Studio.NET. It will create a sample web page with the name webform1.aspx. Let us use this page for our experimentation.Drag and drop a panel control into the webform. We will dynamically generate a checkbox and add to this panel.

Double click on the webform1.aspx page. This will add a page load event handler in the code behind file. Add few lines of code in the Page_Load event handler as shown below: 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); }The above code will create a checkbox dynamically and add to the panel in the form. Using this approach, you can create any other ASP.NET control during runtime and add to the form.

How to specify a position for the dynamically dynamically created ASP.NET control?The most common method is to use to position controls and text in html. In our case, since we are dynamically generating the controls, we may want to use ASP.NET table control to dynamically create rows and cells in the table and add our dynamic controls into it.Drag and drop an ASP.NET table control into the form and give it the name 'tblDynamic'. Now, go back to the Page_Load event handler in the code behind file. The following code sample will create 5 textboxes and labels. We will create 5 rows and 2 cells in each row dynamically and add our dynamicaly created textbox/label controls to the table so that they will be aligned properly and will appear in the page in proper format.private void Page_Load(object sender, System.EventArgs e){ for ( int i = 0; 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); }}

How to access the dynamically created ASP.NET controls?If we drag and drop controls to the web form, the designer automatically generates some code in the aspx page, gives an id to the control and creates the required declarations in the code behind file. This allows us to easily access the control from code behind file and set/get any of it's properties.

But what will happen to the controls created during runtime? How can we access it's properties?

The easiest way to access these dynamically created textboxes is, iterate through all controls in the form and check if it is textbox. In the above example, we have prefixed the ID with "txt_" and then appended the serial number to it for all dynamically generated controls.

Use the following recursive function to iterate through the list of all controls in the form. We are passing the form itself as parameter to the the function. The function will iterate through all children of the passed control and checks if the child control is a textbox and if the id starts with "txt_". If it matches, it sets the Text property to "You Got It!"void IterateControls(Control parent){ foreach (Control child in parent.Controls) { if (child.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox") && child.ID.IndexOf("txt_") == 0) { TextBox textbox = (TextBox)child; textbox.Text = "You Got It !"; }

if (child.Controls.Count > 0) { IterateControls(child); } }}How to test it?Add the above method to your code behind file. Drag and drop a button control to the form and double click on it to create a button clicked event handler. Call the above method from the button click event handler as shown below:IterateControls(this);Now, run the application and see. When you launch the application, it will dynamically create 5 textboxes and when you click on the button, it will populate the value 'You Got It !" in all the dynamically created textboxe controls.



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3