How to prevent BulletedList control from encoding HTML?

J

John Doh

Is it possible to prevent the ListItem that I add to a BulletedList control
to be HTML encoded? I want to add text with <br> tags for example.

Thanks.
 
P

Phil H

Hi

It would appear that the Text property of the ListItem will only allow
a single line of plain text. It converts < > to &lt; and &gt;
respectively (so renders them visibly on the page as < > ) and will
ignore escaped control characters (e.g. \n in C#).

This is unlike the behaviour of the Text property of say a Label
control which does allow HTML tags in string literals and will render
them unfiltered. (The label control also puts line breaks in where
there are \n escape characters)

Without knowing more about the context in which you are trying to
achieve this I can only suggest you consider building your list in
code using a Literal control (designed for programmatic generation of
raw HTML). For example:

Assume you have placed a Literal control named ALiteral on the page
using the designer

ALiteral.Text = "<ul>";
ALiteral.Text += "<li>Line 1 - some text<br />continued on next line
with no bullet</li>";

//... other lines

ALiteral.Text += "</ul>";

In fact here is another example where the list is generated
programmatically from a DataTable using a DataSet
(It produces a bulleted list of 'Myths' surround a particular topic
taken from a project I am working on. Line breaks are put in after
each myth and the truth text on the line underneath. The bullets and
the italicised text help emphasise each fallacious statement on the
first line of each entry in the list:

DataSet1TableAdapters.mythsTableAdapter taMyths
= new DataSet1TableAdapters.mythsTableAdapter();
DataSet1.mythsDataTable dtMyths = taMyths.GetData();

Literal1.Text = "<ul>";
foreach (DataRowView drvMyth in dtMyths.DefaultView)
{
Literal1.Text += said:
(string)drvMyth["reality_text"] + "</
li>";
}
Literal1.Text += "</ul>";


HTH
 
J

John Doh

Thanks Phil.


Phil H said:
Hi

It would appear that the Text property of the ListItem will only allow
a single line of plain text. It converts < > to &lt; and &gt;
respectively (so renders them visibly on the page as < > ) and will
ignore escaped control characters (e.g. \n in C#).

This is unlike the behaviour of the Text property of say a Label
control which does allow HTML tags in string literals and will render
them unfiltered. (The label control also puts line breaks in where
there are \n escape characters)

Without knowing more about the context in which you are trying to
achieve this I can only suggest you consider building your list in
code using a Literal control (designed for programmatic generation of
raw HTML). For example:

Assume you have placed a Literal control named ALiteral on the page
using the designer

ALiteral.Text = "<ul>";
ALiteral.Text += "<li>Line 1 - some text<br />continued on next line
with no bullet</li>";

//... other lines

ALiteral.Text += "</ul>";

In fact here is another example where the list is generated
programmatically from a DataTable using a DataSet
(It produces a bulleted list of 'Myths' surround a particular topic
taken from a project I am working on. Line breaks are put in after
each myth and the truth text on the line underneath. The bullets and
the italicised text help emphasise each fallacious statement on the
first line of each entry in the list:

DataSet1TableAdapters.mythsTableAdapter taMyths
= new DataSet1TableAdapters.mythsTableAdapter();
DataSet1.mythsDataTable dtMyths = taMyths.GetData();

Literal1.Text = "<ul>";
foreach (DataRowView drvMyth in dtMyths.DefaultView)
{
Literal1.Text += said:
(string)drvMyth["reality_text"] + "</
li>";
}
Literal1.Text += "</ul>";


HTH
 
L

Lars

HI

I have the very same problem. My database stores html commands for links
that I show in a DataList. The DataList canhandle to show pure HTML code bit
not GridView or FormView. After I bought a instroduction book that I'm
reading I got some ideas how to solve part of the problem. You could use
Responce.Write( anHTMLString ); to write plain html to the responce. Just as
in old plain ASP.


I suggest you use a DataList in stead of a DataView. That is unless you have
to change the value in a database. Then you have som work to do.


<asp:DataList ID="DataList1" runat="server" BackColor="White"
BorderColor="White" BorderStyle="Ridge" BorderWidth="2px"
CellPadding="3"
CellSpacing="1" DataKeyField="HTML" DataSourceID="sdsMusicFiles">
<FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
<EditItemStyle BackColor="#DEDFDE" Font-Bold="False" Font-Italic="False"
Font-Overline="False" Font-Strikeout="False" Font-Underline="False"
ForeColor="Black" />
<AlternatingItemStyle BackColor="#DEDFDE" Font-Bold="False"
Font-Italic="False"
Font-Overline="False" Font-Strikeout="False" Font-Underline="False"
ForeColor="Black" />
<ItemStyle BackColor="#DEDFDE" ForeColor="Black" />
<EditItemTemplate>
HTML<asp:TextBox ID="tbEditHTML" runat="server"
ontextchanged="TextBox1_TextChanged"></asp:TextBox>
<br />
URL<asp:TextBox ID="tbEditURL" runat="server"
ontextchanged="TextBox1_TextChanged"></asp:TextBox>
<br />
Name<asp:TextBox ID="tbEditFilename" runat="server"
ontextchanged="TextBox1_TextChanged"></asp:TextBox>
<br />
Flagga<asp:TextBox ID="tbEditFlag" runat="server"
ontextchanged="TextBox1_TextChanged"></asp:TextBox>
<br />
Kompositör<asp:TextBox ID="tbEditAuthour" runat="server"
ontextchanged="TextBox1_TextChanged"></asp:TextBox>
<br />
</EditItemTemplate>
<SelectedItemStyle BackColor="#DEDFDE" Font-Bold="True"
Font-Italic="False"
Font-Overline="False" Font-Strikeout="False" Font-Underline="False"
ForeColor="Black" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
<ItemTemplate>
<asp:Label ID="HTMLLabel" runat="server" Text='<%# Eval("HTML",
"{0}") %>' />
<br />
</ItemTemplate>
</asp:DataList>





John Doh said:
Thanks Phil.


Phil H said:
Hi

It would appear that the Text property of the ListItem will only allow
a single line of plain text. It converts < > to &lt; and &gt;
respectively (so renders them visibly on the page as < > ) and will
ignore escaped control characters (e.g. \n in C#).

This is unlike the behaviour of the Text property of say a Label
control which does allow HTML tags in string literals and will render
them unfiltered. (The label control also puts line breaks in where
there are \n escape characters)

Without knowing more about the context in which you are trying to
achieve this I can only suggest you consider building your list in
code using a Literal control (designed for programmatic generation of
raw HTML). For example:

Assume you have placed a Literal control named ALiteral on the page
using the designer

ALiteral.Text = "<ul>";
ALiteral.Text += "<li>Line 1 - some text<br />continued on next line
with no bullet</li>";

//... other lines

ALiteral.Text += "</ul>";

In fact here is another example where the list is generated
programmatically from a DataTable using a DataSet
(It produces a bulleted list of 'Myths' surround a particular topic
taken from a project I am working on. Line breaks are put in after
each myth and the truth text on the line underneath. The bullets and
the italicised text help emphasise each fallacious statement on the
first line of each entry in the list:

DataSet1TableAdapters.mythsTableAdapter taMyths
= new DataSet1TableAdapters.mythsTableAdapter();
DataSet1.mythsDataTable dtMyths = taMyths.GetData();

Literal1.Text = "<ul>";
foreach (DataRowView drvMyth in dtMyths.DefaultView)
{
Literal1.Text += said:
(string)drvMyth["reality_text"] + "</
li>";
}
Literal1.Text += "</ul>";


HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top