16

Could someone please advise what the "correct" method is for adding HTML content to an ASP.NET page dynamically?

I am aware of the following declarative method.

//Declaration
<%= MyMethodCall() %>


//And in the code behind.
protected String MyMethodCall()
{
    return "Test Value";
}

Is there a better or best practice way?

EDIT: I am building a Galleriffic photo gallery dynamically depending on the images located in a specific folder.

Maxim Gershkovich
  • 45,951
  • 44
  • 147
  • 243

4 Answers4

29

Depends what you want to do.

For controls/text I normally use a LiteralControl and set the Text property as the HTML I want to add, then this control can be added anywhere on the page that you want it to appear

LiteralControl reference is here

ok seeing as you want it for Galleriffic, I guess it would pseudo-appear as such...

 LiteralControl imageGallery = new LiteralControl();
    string divStart = @"<div id='thumbs'><ul class='thumbs noscript'>";
    imageGallery.Text += divStart;
    foreach ([image in images])
    {
      string imageHTML = @"<li><a class='thumb' name='optionalCustomIdentifier' ref='path/to/slide' title='your image title'>
                           <img src='path/to/thumbnail' alt='your image title again for graceful degradation' /></a>
                           <div class='caption'>[caption]<div></li>";

      imageGallery.Text += imageHTML;
    }
    string divEnd = @"</ul></div>";
    imageGallery.Text += divEnd;

    this.[divOnPage].Controls.Add(imageGallery);
Pulah Nandha
  • 774
  • 4
  • 23
Dan
  • 1,489
  • 12
  • 16
  • What are the advantages of this approach as opposed to the method I specified? PS: Thank you for the comprehensive example! – Maxim Gershkovich Apr 01 '11 at 03:15
  • 4
    This code is awfull. Why not use a repeater? or a for in the .aspx. Concatenating strings to create HTML is not my idea of a best practice – Carlos Muñoz Apr 01 '11 at 03:21
  • 2
    Assembling markup in C# code should be the absolute last resort as it creates a maintenance nightmare that would make Cthulhu proud. At the very least, encapsulate into a user or server control. – Thomas Apr 01 '11 at 04:41
  • 2
    yes, you're right in terms of the string/HTML concat, I just quickly put that example together to illustrate the point – Dan Apr 01 '11 at 06:42
12

Aspx :

<div id="DIV1" runat="server"></div>

Code behind :

DIV1.InnerHtml = "some text";
Bengi Besçeli
  • 3,638
  • 12
  • 53
  • 87
  • For anything substantially, like a "photo gallery", I'd recommend to use something like react, but this question is from 2011, and for something quick and dirty, this answer is an excellent solution – Roland Oct 08 '18 at 09:28
6

There are several ways to do that, which to use really depends on your scenario and preference.

  • Web User Controls: Can be added dynamically and you get the full editor support of Visual Studio.
  • XML literals (VB.NET only): Very convenient way to quickly put together HTML in code.
  • Templates: Add a plain HTML document to your solution and include it as a resource. Then you'll get editor support and you won't clutter your code with HTML source.
Jakob Gade
  • 12,319
  • 15
  • 70
  • 118
0

Another option

//.aspx
<asp:Literal ID="myText" runat="server"></asp:Literal>


//.aspx.cs
protected Literal myText;
myText.Text = "Hello, World!";
Michael
  • 2,825
  • 3
  • 24
  • 30