Quantcast
Channel: All About ASP.NET and ASP.NET Core 2 Hosting BLOG
Viewing all articles
Browse latest Browse all 427

ASP.NET Hosting - ASPHostPortal.com :: How to Add Controls to an ASP.NET Web Page Programmatically

$
0
0

How to Add Controls to an ASP.NET Web Page Programmatically

In order to programmatically add a control to a page, there must be a container for the new control. For example, if you are creating table rows, the container is the table. If there is no obvious control to act as container, you can use a PlaceHolder or Panel Web server control.

In some instances, you might want to create both static text and controls. To create static text, you can use either a Literal or a Label Web server control. You can then add these controls to the container as you would any other control. For information about view state in controls created at run time, see Dynamic Web Server Controls and View State.

To add a control to an ASP.NET Web page programmatically

  • Create an instance of the control and set its properties, as shown in the following example:

Label myLabel = new Label();
myLabel.Text = "Sample Label";

  • Add the new control to the Controls collection of a container already on the page, as shown in the following example:

Panel Panel1= new Panel();
Panel1.Controls.Add(myLabel);

Note

Because the Controls property is a collection, you can use the AddAt method to place the new control at a specific location — for example, in front of other controls. However, this can introduce errors into the page. For details, see Dynamic Web Server Controls and View State.

The following code example shows the event handler for the SelectedIndexChanged event of a control named DropDownList1. The handler creates as many label controls as the user has selected from the drop-down list. The container for the controls is a PlaceHolder Web server control named Placeholder1.

Security Note

User input in a Web page can include potentially malicious client script. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, Script Exploits Overview.

private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
    DropDownList DropDownList1 = new DropDownList();
    PlaceHolder PlaceHolder1 = new PlaceHolder();

  // Get the number of labels to create.
 int numlabels = System.Convert.ToInt32(DropDownList1.SelectedItem.Text);
 for (int i=1; i<=numlabels; i++)
 {
   Label myLabel = new Label();

   // Set the label's Text and ID properties.
   myLabel.Text = "Label" + i.ToString();
   myLabel.ID = "Label" + i.ToString();
   PlaceHolder1.Controls.Add(myLabel);
   // Add a spacer in the form of an HTML <br /> element.
   PlaceHolder1.Controls.Add(new LiteralControl("<br />"));
 }
}

Best ASP.NET Hosting Recommendation

ASPHostPortal.com provides its customers with Plesk Panel, one of the most popular and stable control panels for Windows hosting, as free. You could also see the latest .NET framework, a crazy amount of functionality as well as Large disk space, bandwidth, MSSQL databases and more. All those give people the convenience to build up a powerful site in Windows server. ASPHostPortal.com offers ASP.NET hosting starts from $1/month only. They also guarantees 30 days money back and guarantee 99.9% uptime. If you need a reliable affordable ASP.NET Hosting, ASPHostPortal.com should be your best choice.

 


Viewing all articles
Browse latest Browse all 427