Unpossible
Unpossible

Reputation: 10697

Making asp:Label a specific HTML tag

New to VB.net, just trying to figure out how to properly manipulate the asp:Label control.

I have a page that, based on if there are results, etc should display an <h1></h1> tag with a header, then the data. As I am using the code-behind model, my user facing page essentially just has the following:

<asp:Label ID="lblMessage" runat="server" 
    Visible="false" />

<asp:DataList ID="dlCurriculumLists" runat="server" 
    DataSourceID="sdsCurriculumLists"
    DataKeyField="Entry No_" 
    RepeatLayout="Flow"
    Visible="false">
    <ItemTemplate>
            <div>
                <asp:HyperLink ID="hlCurriculum" runat="server"  
                    Text='<%# DataBinder.Eval(Container.DataItem, "Title") %>'  
                    NavigateUrl='<%# DataBinder.Eval(Container.DataItem, "File Path") %>'
                    ToolTip='<%# DataBinder.Eval(Container.DataItem, "Title") %>'
                    Target="_blank"
                    Style="font-weight: bold;">
                </asp:HyperLink>
            </div>
    </ItemTemplate>
</asp:DataList>

On my code-behind page, I then set the asp:Label and asp:DataList to Visible="true" based on the data from the database. Here's the catch - if there is data, I want to set lblMessage to be an H1, and if not, just standard Label text. I realize I can emulate the look through the CSS, but was just hoping there was another way (maybe similar to the ItemTemplate concept) to specify the HTML type of the Label control - it appears to be a by default.

Upvotes: 0

Views: 2773

Answers (3)

uncoder
uncoder

Reputation: 1876

Aside from what already has been suggested here, you can also implement your own ASP.NET control with any properties you want, then modify its rendering on the fly, depending on the properties' values. It is pretty fun to do and is not as hard as one might think. Here is some information on the subject: http://msdn.microsoft.com/en-us/library/vstudio/zt27tfhy(v=vs.100).aspx

Upvotes: 0

Heinzi
Heinzi

Reputation: 172270

For people coming from a VB background, it's a common mistake to think that the most basic control for displaying arbitary text is a Label.

That's not true. A label should label something, usually another UI control (that's what the AssociatedControlId property is for).

If you just want to display arbitrary text or HTML markup, use something more basic instead. Some examples are asp:Literal, asp:Placeholder or asp:Localize.

Using, for example, an asp:Literal called myLiteral, you can easily create a heading in code:

myLiteral.Text = "<h1>" & Server.HtmlEncode(myHeading) & "</h1>"

Upvotes: 2

Jamie Dixon
Jamie Dixon

Reputation: 53991

As far as I know, the asp:Label component will always generate a <label> HTML tag when its AssociatedControlId attribute is set.

What you could do instead is use a Literal Control and populate it with the HTML you wish at runtime.

UPDATE

One thing you could do to make this work as required using your current Label control is to have a theme for the label that marks it up as a H1. You could then toggle the controls EnableTheming property as required.

Upvotes: 2

Related Questions