40

The Label control in ASP.NET seems to render <span> tags, but is there a server control to render HTML within a <div>?

Sure, I could set display: block and it might look the same, but I'd rather not be nesting divs inside spans. Also I'd prefer not to use <%= MyVariable %> because that might not behave nicely on postbacks.

Any suggestions, please?

James
  • 7,343
  • 9
  • 46
  • 82

6 Answers6

66

Of course: ASP.NET has a built-in control called Panel!

And you may use it as follows:

<asp:Panel ID="myPanel" runat="server">
    <!-- Other markup here like client and server controls/elements -->
</asp:Panel>

It's a container, so you add Controls to it in the code-behind like:

myPanel.Controls.Add(new LiteralControl("Hello World"));

You can add the Literal control (or any others) in the markup if you like and just assign to its Text property if you want it to update dynamically at runtime.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206
  • If I would decide to use Panel how will I add new HTML element attributes like ones for which Panel doesn't have a corresponding property? – Mircea Ion Feb 22 '13 at 14:19
  • @MirceaIon As any other WebControl, you've the `Attributes` property: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.webcontrol.attributes.aspx – Matías Fidemraizer Feb 22 '13 at 14:50
31

I think you need HtmlGenericControl class. It has a constructor which accepts a string variable which initializes a new instance of the HtmlGenericControl class with the specified tag:

var div = new HtmlGenericControl("div");

It is also has InnerHtml and InnerText properties (you mentioned this in a comment to the previous answer).

Oleks
  • 31,955
  • 11
  • 77
  • 132
  • Thanks. Could I put this directly in the page markup, or does it have to be added to a Panel? – James Apr 15 '11 at 10:21
  • 2
    You could use it in markup like: `
    my inner text here
    `
    – Oleks Apr 15 '11 at 10:23
  • 1
    Cool. What's the difference between the InnerText and InnerHtml properties? Should I just use InnerHtml? – James Apr 15 '11 at 10:25
  • 4
    `InnerText` property provides automatic HTML encoding/decoding. See: e.g., if the `InnerText` property is set to ` Hello `, the `<` and `>` symbols are converted to `<` and `>`, respectively. Fot the `InnerHtml` property the rendered output would be: ` Hello `. – Oleks Apr 15 '11 at 10:28
8

Try the Panel control.

Widor
  • 13,003
  • 7
  • 42
  • 64
6

Try this:

<div class="myclass">
<asp:Literal ID="mytext" runat="server"></asp:Literal>
</div>

Set your text inside Literal, which renders without html tag

ndmeiri
  • 4,979
  • 12
  • 37
  • 45
Eyad Eyadian
  • 111
  • 1
  • 3
2
<asp:Panel>
<div id="NoRecords" runat="server" visible="false">No records are available.</div>
</asp:Panel>

Code-Behind

 protected void MyRepeater1_PreRender(object sender, EventArgs e)
{
    if (MyRepeater1.Items.Count == 0)
    {
        NoRecords.Visible = true;
    }
    else
    {
        NoRecords.Visible = false;
    }
}
Pablo
  • 1,953
  • 4
  • 28
  • 57
0
div runat="server" id="myserversideDiv" 

my inner text here. It has inner text and inner html property and most of asp.net server control property. Try that.

Jin Thakur
  • 2,711
  • 18
  • 15