Home > Enterprise >  Display string html content in aspx page
Display string html content in aspx page

Time:10-18

I have a string HTML text saved in a string variable. for example it looks like this:

sample.aspx.vb

Dim htmlStr As String = "<h1> Hi </h1>"

I want the htmlStr variable's content formatted as a HTML tag and displayed in the aspx page. How can I do this ?

It should look like this:

Hi

CodePudding user response:

Simple drop in a control on the form you want to use. A plaine jane "div" is fine, or even a label works. (a text box will not).

So, say I drop in this div (and a button)

        <asp:Button ID="Button1" runat="server" Text="test button" />
        <br />

        <div id="mycooldiv" runat="server">

        </div>

Now, code behind can be this:

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim htmlStr As String = "<h1> Hi </h1>"

    mycooldiv.InnerHtml = htmlStr

End Sub

and now we get/see this:

enter image description here

CodePudding user response:

A LiteralControl can contain HTML.

    Dim htmlStr As String = "<h1> Hi </h1>"

    Me.Controls.Add(New LiteralControl(htmlStr))

https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.literalcontrol?view=netframework-4.8

  • Related