Display HTML from Literal Control

966 Views Asked by At

A recent CMS upgrade is causing me issues. Previously the area which contained the content was a HTMLGenericControl and I was able to get the innerHTML to append to a div which was rendered on the page. See code below.

        If TypeOf ctrl is HtmlGenericControl then 
          Dim contact as HtmlGenericControl = ctrl
              html.appendFormat(("<div class=""col-sm-6 col-md-6 col-lg-6"">{0}</div>" + Environment.NewLine), contact.innerHTML)
        End If

The new change has changed this content area to a Literal Control and I'm having problems getting the HTML from the Literal Control. I can get the control id by using .ClientID so I'm seeing still able to find the correct div but I need to get the Html Inside this div. Could someone point me in the right direction so I can investigate further?

 If TypeOf ctrl is LiteralControl  then
   Dim contact as LiteralControl  = ctrl
   html.appendFormat(("<div class=""col-sm-6 col-md-6 col-lg-6"">{0}</div>" + Environment.NewLine), contact.Text)
 End If
1

There are 1 best solutions below

0
blecovich On

ASP Literal controls are typically just used to render text, and don't create spans or other HTML tags that can be returned or styled.

Here's an example of HTML markup generated by the following ASP literal:

<asp:Literal runat="server">Hi there!</asp:Literal>

enter image description here

The literal could also be defined as so: <asp:Literal runat="server text="Hi there!" />

To access the text of an ASP literal control, just use the .Text property.

MyLiteral.Text = "Hi there!"

You can actually pass HTML fragments to the literal text if you have to, and they will render as normal. If you're actually designing the server-side code however, and need to do this, consider using asp:Labels (which render spans) and asp:Panels (which render divs) instead.

MyLiteral.Text = "<div style='width: 100px; height: 100px; background-color: yellow;'></div>"

Here's Microsoft's documentation on ASP literals; it includes all the available properties for accessing.