views:

118

answers:

5

I have the following on an ASP.NET page:

<li><asp:Hyperlink id="myHyperlink" runat="server">My Text</asp:Hyperlink></li>

Sometimes in code I need to be able to dynamically add a STRONG tag around the hyperlink so that it looks like this:

<li><strong><asp:Hyperlink id="myHyperlink" runat="server">My Text</asp:Hyperlink></strong></li>

Is there some elegant way to do this in code? I know I could get it done using Literals but I just wondered if there was some special method that I didn't know about that allowed you to insert tags like that.

Thanks, Corey

A: 

You could implement a control that derives from HyperLink that renders an outer based on a property, if the STRONG tag was a requirement (for whatever reason).

HackedByChinese
A: 

myHyperlink.Bold=true

NickAtuShip
A: 

the Hyperlink element has a Font-Bold property that would do the same thing as adding a strong tag.

Jason
Actually it emits `style="font-weight:bold;"`
p.campbell
which will make it bold, same as a strong tag.
Jason
+3  A: 

if this is something you're regularly needing to do, i would create a new server control and inherit the hyperlink control. Add a property for Strong, and override the render method to add the tag if strong=true. Note - this is not from an IDE so syntax might not be perfect

Public Class StrongTextBox
        Inherits Hyperlink

    Public Overrides Sub RenderEndTag(ByVal writer As System.Web.UI.HtmlTextWriter)

        MyBase.RenderEndTag(writer)
        If Strong Then
            writer.Write("</strong>")
        End If

    End Sub


    Public Overrides Sub RenderBeginTag(ByVal writer As System.Web.UI.HtmlTextWriter)

        If Strong Then
            writer.Write("<strong>")
        End If
        MyBase.RenderEndTag(writer)

    End Sub

private mStrong as Boolean
Public Property Strong as Boolean

   Get
                Return mStrong 
   End Get

   Set(ByVal value As Boolean)
                mStrong  = value
   End Set

End Property
NickAtuShip
What did you end up using?
NickAtuShip
+1  A: 

If you change your li tag to runat server, like so:

<li runat="server" id="myLi"><asp:Hyperlink id="myHyperlink" runat="server">My Text</asp:Hyperlink></li>

you can add the strong tag like this:

var strongTag = new System.Web.UI.HtmlControls.HtmlGenericControl("strong");

Page.Controls.Remove(myHyperlink);

myLi.Controls.Add(strongTag);
strongTag.Controls.Add(myHyperlink);

As most others have noted though, use of STRONG is not really recommended.

kristian