38

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
  • 6,911
  • 9
  • 40
  • 80

6 Answers6

62

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>
Matías Fidemraizer
  • 59,064
  • 16
  • 107
  • 181
  • 1
    I feel a bit stupid here, but the Panel hasn't got a Text/InnerHtml property. How can I write directly into it, like with a Label? – James Apr 15 '11 at 10:02
  • 3
    You add Controls to it - it's a container. E.g. `myPanel.Controls.Add(new LiteralControl("Hello World"));` – Widor Apr 15 '11 at 10:04
  • @James Widor has answered your second question! :) – Matías Fidemraizer Apr 15 '11 at 10:09
  • @Widor Thanks. Could I put the asp:Literal control directly onto the page, or has that got disadvantages? – James Apr 15 '11 at 10:16
  • Yes, 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. – Widor Apr 15 '11 at 10:23
  • 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
  • I came across this and for the amount of upvotes I find there is a relative low amount of information. Obviously one can google it but adding a view lines for an example would improve the quality of this answer a lot. – Madmenyo May 14 '15 at 11:10
  • @MennoGouw And what do you want to explain here? At the end of the day, using ASP.NET WF controls, a DIV is a Panel :D – Matías Fidemraizer May 14 '15 at 11:53
  • @MatíasFidemraizer Look at the answer of Pablo. No need for searching how to use a panel, imho a much better answer but only a single vote. Hack, even the answer of Widor is better with the link in it, posted just a minute after you but has only 8 upvotes. – Madmenyo May 14 '15 at 11:57
  • @MennoGouw Actually OP asked for a control to render a `
    `, and I answered OP required `Panel`. This is why the answer is very useful. If you're a Web Forms developer you know how to use any basic server control like Panel... Also, check I did provide no link. I mean, at the end of the day, this is what you're looking for if you open this Q&A...
    – Matías Fidemraizer May 14 '15 at 13:31
  • @MatíasFidemraizer I'm not going to discuss if this answer is good or bad. Just saying this answer would benefit from some extra details. – Madmenyo May 14 '15 at 13:35
  • @MennoGouw Happy?? :) – Matías Fidemraizer May 14 '15 at 13:40
  • Yeah, very much. Just hoping you would agree? I am fairly new to ASP and now I would not need to look further on this topic. Now the amount of upvotes resembles the answers quality imho. – Madmenyo May 14 '15 at 13:42
  • @MennoGouw Ok! Well, I'm not sure about your point, because I believe MSDN is better than SO to get conclusions. My answer would be enough to search for `Panel` class on MSDN. BTW, I'm agree that a code snippet might be useful too to quickly check if this control is what you're looking for – Matías Fidemraizer May 14 '15 at 13:45
  • Official documentation beets anything if it is thorough like MSDN. But imho SO is a great source for quickly looking up snippets and if your not exactly sure what to look for then SO does a better job at finding a solution then MSDN. – Madmenyo May 14 '15 at 13:48
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).

Alex
  • 30,696
  • 10
  • 74
  • 131
  • 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
    `
    – Alex 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 `` symbols are converted to `<` and `>`, respectively. Fot the `InnerHtml` property the rendered output would be: ` Hello `. – Alex Apr 15 '11 at 10:28
8

Try the Panel control.

Widor
  • 12,075
  • 6
  • 35
  • 60
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,728
  • 12
  • 32
  • 41
Eyad Eyadian
  • 91
  • 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,013
  • 2
  • 17
  • 40
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,265
  • 15
  • 12