Span vs. DIV for asp:Label Control

G

Guest

Hello All--

I have a number of <asp:Label CssClass="someclass">Text</asp:Label> controls
on my page. I would like them to be shown as
<div class="someclass">Text</div>
rather than
<span class="someclass">Text</span>

When it uses a span, it doesn't seem to use all the class definitions,
particularly for margins and positioning.

I realize I can surround my <asp:Label> controls with the <div> elements,
but that seems to make the <asp:Label> control unneccessary, doesn't it?

Thanks,
pagates
 
B

Brennan Stehling

Perhaps the most simple approach would be to change the Label control
would to inherit from it and override the value of the TagKey.

Here is some VB.NET code.

Imports System.Web.UI
Imports System.Web.UI.WebControls

<ToolboxData("<{0}:DivLabel runat=server></{0}:DivLabel>")> _
Public Class DivLabel
Inherits Label

Protected Overrides ReadOnly Property TagKey() As
System.Web.UI.HtmlTextWriterTag
Get
Return HtmlTextWriterTag.Div
End Get
End Property

End Class


Brennan Stehling
http://brennan.offwhite.net/blog/
 
B

bruce barker \(sqlwork.com\)

the difference between a span and div is the default display style. a div
defaults to block and a span to inline. if you want you labels to be block
style, just set the style

..someclass { display:block; }


-- bruce (sqlwork.com)
 
E

Eliyahu Goldin

If you would like to use divs, why not to go ahead and just do it?

<div class="someclass" runat="server" id="myDiv">Text</div>
 
B

Brennan Stehling

A Label allows you to set many properties for the Font and other
control settings which is helpful at design-time. And you do not want
to simply set the span to use "display: block" in the CSS because there
is a bug in IE6 which does not truly make that span act as a block
level element. You will want to ensure that it does start as a block.

Doing so will save you from a good deal of trouble.

Brennan Stehling
http://brennan.offwhite.net/blog/


Eliyahu said:
If you would like to use divs, why not to go ahead and just do it?

<div class="someclass" runat="server" id="myDiv">Text</div>

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]


pagates said:
Hello All--

I have a number of <asp:Label CssClass="someclass">Text</asp:Label>
controls
on my page. I would like them to be shown as
<div class="someclass">Text</div>
rather than
<span class="someclass">Text</span>

When it uses a span, it doesn't seem to use all the class definitions,
particularly for margins and positioning.

I realize I can surround my <asp:Label> controls with the <div> elements,
but that seems to make the <asp:Label> control unneccessary, doesn't it?

Thanks,
pagates
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top