470,638 Members | 1,579 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,638 developers. It's quick & easy.

Span vs. DIV for asp:Label Control

Hello All--

I have a number of <asp:Label CssClass="someclass">Text</asp:Labelcontrols
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:Labelcontrols with the <divelements,
but that seems to make the <asp:Labelcontrol unneccessary, doesn't it?

Thanks,
pagates
Sep 19 '06 #1
4 6193
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/
pagates wrote:
Hello All--

I have a number of <asp:Label CssClass="someclass">Text</asp:Labelcontrols
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:Labelcontrols with the <divelements,
but that seems to make the <asp:Labelcontrol unneccessary, doesn't it?

Thanks,
pagates
Sep 19 '06 #2
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)
"pagates" <pa*****@discussions.microsoft.comwrote in message
news:15**********************************@microsof t.com...
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:Labelcontrols with the <divelements,
but that seems to make the <asp:Labelcontrol unneccessary, doesn't it?

Thanks,
pagates

Sep 19 '06 #3
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" <pa*****@discussions.microsoft.comwrote in message
news:15**********************************@microsof t.com...
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:Labelcontrols with the <divelements,
but that seems to make the <asp:Labelcontrol unneccessary, doesn't it?

Thanks,
pagates

Sep 20 '06 #4
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 Goldin wrote:
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" <pa*****@discussions.microsoft.comwrote in message
news:15**********************************@microsof t.com...
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:Labelcontrols with the <divelements,
but that seems to make the <asp:Labelcontrol unneccessary, doesn't it?

Thanks,
pagates
Sep 20 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by moondaddy | last post: by
2 posts views Thread by John Holmes | last post: by
4 posts views Thread by Jeronimo Bertran | last post: by
2 posts views Thread by Shapper | last post: by
2 posts views Thread by footballhead | last post: by
1 post views Thread by Steve Hershoff | last post: by
???
1 post views Thread by Stoney L | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.