Your full service technology partner! 
-Collapse +Expand
Search C# Group:

-Collapse +Expand C# To/From
-Collapse +Expand C# Study Test
-Collapse +Expand C# Store

Prestwood eMagazine

October Edition
Subscribe now! It's Free!
Enter your email:

   ► KBProgrammingC#WebForms Cod...   Print This     
  From the February 2016 Issue of Prestwood eMag
C# WebForms Coding Tasks:
Programattically Adding Content to Web Pages in C#
Posted 13 years ago on 12/12/2008
Take Away: Visual web developer offers a few different ways to add dynamic content to a web page. This will help you choose which one is right for your application. In General, there are Data-Aware components, Labels, and PlaceHolders.


Ok, so the wonder and beauty of Visual Web Developer is NOT the easy way you can add content to your pages in the design environment, it's also how you can program your website to deliver dynamic content.  This can be content based on information from a database, from user interaction with the website, or from any other source you like.

Almost any ASP component can be modified at runtime in your C# code.  But lets say you are tasked to add something to a page based on state data. 

Example 1: Labels

Let's say you are adding a warning to a webform when the user's account is within 10 days of expiration.  The easiest way to do this is to add an ASP Label to the webform and then in the .cs code do something like

if (ExpDays<=10)
  {Label1.text="Please renew your account.  The expiration date is approaching.");}

Example 2: Adding Multiple Components

Even though a single label allows you to add an unlimited amount of text and markup to your page, sometimes you need to add more to a page, such as images, buttons, links, etc.  For this, we'll embed a placeholder object in the aspx file and then from the cs code we can add things to the placeholder's Controls array.  Note that adding doesn't need to occur in order, but in this example we add items in the order we will see them.

PlaceHolder1.Controls.Add(new LiteralControl("A Literal Control contains text and <b>markup</b>. "));
Image thisImage = new Image(); //create a new image
thisImage.ImageUrl = @"~/Uploads/" + FileName;  //Setting the image property
PlaceHolder1.Controls.Add(thisImage);  //add the image to the controls in the placeholder
PlaceHolder1.Controls.Add(new LiteralControl("<br />"));

A LiteralControl is the way we add plain text or HTML to the Controls array in the placeholder object.

As you can see, nearly any ASP object can be added dynamically using this method.  Note that the Image here is a ASP:Image, not a Windows Image object.

Also note that there are more objects that have the Controls Array, such as Panels and TableCells.  All of these can be added to in this way by your C# code at runtime.


Share a thought or comment...
Write a Comment...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.

Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P1182A1
Enter key:
Article Contributed By Bryan Valencia:

Bryan Valencia is the author of millions of lines of code and writes for PrestwoodBoards and Visual Studio Journey on-line magazines.

Visit Profile

 KB Article #101680 Counter
Since 12/12/2008
Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site:

©1995-2021 Prestwood IT Solutions.   [Security & Privacy]