How to prevent BulletedList control from encoding HTML?

Discussion in 'ASP .Net Web Controls' started by John Doh, Feb 14, 2008.

  1. John Doh

    John Doh Guest

    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.
     
    John Doh, Feb 14, 2008
    #1
    1. Advertising

  2. John Doh

    Phil H Guest

    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 += "<li><em>" +
    (string)drvMyth["myth_text"] + "</em><br /
    >" +

    (string)drvMyth["reality_text"] + "</
    li>";
    }
    Literal1.Text += "</ul>";


    HTH
     
    Phil H, Feb 14, 2008
    #2
    1. Advertising

  3. John Doh

    John Doh Guest

    Thanks Phil.


    "Phil H" <> wrote in message
    news:...
    > 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 += "<li><em>" +
    > (string)drvMyth["myth_text"] + "</em><br /
    >>" +

    > (string)drvMyth["reality_text"] + "</
    > li>";
    > }
    > Literal1.Text += "</ul>";
    >
    >
    > HTH
     
    John Doh, Feb 15, 2008
    #3
  4. John Doh

    Lars Guest

    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" <> skrev i meddelandet
    news:...
    > Thanks Phil.
    >
    >
    > "Phil H" <> wrote in message
    > news:...
    >> 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 += "<li><em>" +
    >> (string)drvMyth["myth_text"] + "</em><br /
    >>>" +

    >> (string)drvMyth["reality_text"] + "</
    >> li>";
    >> }
    >> Literal1.Text += "</ul>";
    >>
    >>
    >> HTH

    >
     
    Lars, Feb 15, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stan SR

    BulletedList inside BulletedList

    Stan SR, Aug 31, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    2,908
    Stan SR
    Aug 31, 2006
  2. Nathan Sokalski

    Nesting the BulletedList control

    Nathan Sokalski, Mar 13, 2007, in forum: ASP .Net
    Replies:
    0
    Views:
    619
    Nathan Sokalski
    Mar 13, 2007
  3. Nathan Sokalski

    Problem with the BulletedList control

    Nathan Sokalski, Mar 15, 2007, in forum: ASP .Net
    Replies:
    0
    Views:
    367
    Nathan Sokalski
    Mar 15, 2007
  4. Rob R. Ainscough

    BulletedList control not showing bullets?

    Rob R. Ainscough, May 23, 2006, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    303
    Rob R. Ainscough
    May 23, 2006
  5. Nathan Sokalski

    Nesting the BulletedList control

    Nathan Sokalski, Mar 13, 2007, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    166
    Nathan Sokalski
    Mar 13, 2007
Loading...

Share This Page