ListItems and formatting

Discussion in 'ASP .Net Web Controls' started by Nathan Sokalski, Aug 16, 2007.

  1. I have become very frustrated with the ListItem control lately due to what
    happens when the Text property contains HTML tags. When the Text property
    contains HTML tags, or character codes such as  , they are converted to
    HTML (for example, <br/> would be rendered as &lt;br/&gt;). This can become
    very frustrating when you want to bold or underline part or all of a
    ListItem, which is not that uncommon of a task. Also, because this would be
    extremely trivial to do in an HTML file or classic ASP (and I am guessing
    most other server-side technologies), most clients will complain about it.
    Is there a reason that the ListItem does this? Why doesn't the ListItem have
    a Mode property that can be set to PassThrough, similar to what the Literal
    control has. I think this would solve the problem, and still allow the
    people that do want their HTML converted to have that option. But since the
    Mode property doesn't currently exist (I haven't looked at .NET 3.0, but
    since I still use VS2005 and .NET 2.0, that really doesn't matter yet), can
    someone tell me a way to use the ListItem and still do a little simple
    formatting like bolding and underlining (using simple stuff like the <b></b>
    and <u></u> tags) in my code? Thanks.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/
    Nathan Sokalski, Aug 16, 2007
    #1
    1. Advertising

  2. Hi Nathan,
    all list controls inherits from class ListControl where you can find
    RenderContents method. I used Reflector to get code of this method and change
    it little bit to get new one in my own list control:

    public class MyListBox : BulletedList
    {
    public MyListBox()
    {
    //
    // TODO: Add constructor logic here
    //
    }

    protected override void RenderContents(HtmlTextWriter writer)
    {
    ListItemCollection items = this.Items;
    int count = items.Count;
    if (count > 0)
    {
    bool flag = false;
    for (int i = 0; i < count; i++)
    {
    ListItem item = items;
    if (item.Enabled)
    {
    writer.WriteBeginTag("option");
    if (item.Selected)
    {
    if (flag)
    {
    this.VerifyMultiSelect();
    }
    flag = true;
    writer.WriteAttribute("selected", "selected");
    }
    writer.WriteAttribute("value", item.Value, true);
    if (item.Attributes != null && item.Attributes.Count
    > 0)

    {
    item.Attributes.Render(writer);
    }
    if (this.Page != null)
    {

    this.Page.ClientScript.RegisterForEventValidation(this.UniqueID, item.Value);
    }
    writer.Write('>');
    writer.Write(item.Text);
    writer.WriteEndTag("option");
    writer.WriteLine();
    }
    }
    }
    }
    }

    This is only skelet of new class but it will solve your main problem. I
    think ListItem does not allow to set Mode because it is also used in ListBox
    and DropDownList where html tags are not rendered.

    Regards,
    Ladislav

    "Nathan Sokalski" wrote:

    > I have become very frustrated with the ListItem control lately due to what
    > happens when the Text property contains HTML tags. When the Text property
    > contains HTML tags, or character codes such as , they are converted to
    > HTML (for example, <br/> would be rendered as <br/>). This can become
    > very frustrating when you want to bold or underline part or all of a
    > ListItem, which is not that uncommon of a task. Also, because this would be
    > extremely trivial to do in an HTML file or classic ASP (and I am guessing
    > most other server-side technologies), most clients will complain about it.
    > Is there a reason that the ListItem does this? Why doesn't the ListItem have
    > a Mode property that can be set to PassThrough, similar to what the Literal
    > control has. I think this would solve the problem, and still allow the
    > people that do want their HTML converted to have that option. But since the
    > Mode property doesn't currently exist (I haven't looked at .NET 3.0, but
    > since I still use VS2005 and .NET 2.0, that really doesn't matter yet), can
    > someone tell me a way to use the ListItem and still do a little simple
    > formatting like bolding and underlining (using simple stuff like the <b></b>
    > and <u></u> tags) in my code? Thanks.
    > --
    > Nathan Sokalski
    >
    > http://www.nathansokalski.com/
    >
    >
    >
    Ladislav Mrnka, Aug 17, 2007
    #2
    1. Advertising

  3. One more note:
    As you can see my class derives from BulletedList (I used some of my
    previous code) but RenderContents is from ListControl. BulletedList has its
    own overriden version which is much more complicated to allow rendering items
    as hyperlinks, link buttons etc.

    Ladislav Mrnka

    "Ladislav Mrnka" wrote:

    > Hi Nathan,
    > all list controls inherits from class ListControl where you can find
    > RenderContents method. I used Reflector to get code of this method and change
    > it little bit to get new one in my own list control:
    >
    > public class MyListBox : BulletedList
    > {
    > public MyListBox()
    > {
    > //
    > // TODO: Add constructor logic here
    > //
    > }
    >
    > protected override void RenderContents(HtmlTextWriter writer)
    > {
    > ListItemCollection items = this.Items;
    > int count = items.Count;
    > if (count > 0)
    > {
    > bool flag = false;
    > for (int i = 0; i < count; i++)
    > {
    > ListItem item = items;
    > if (item.Enabled)
    > {
    > writer.WriteBeginTag("option");
    > if (item.Selected)
    > {
    > if (flag)
    > {
    > this.VerifyMultiSelect();
    > }
    > flag = true;
    > writer.WriteAttribute("selected", "selected");
    > }
    > writer.WriteAttribute("value", item.Value, true);
    > if (item.Attributes != null && item.Attributes.Count
    > > 0)

    > {
    > item.Attributes.Render(writer);
    > }
    > if (this.Page != null)
    > {
    >
    > this.Page.ClientScript.RegisterForEventValidation(this.UniqueID, item.Value);
    > }
    > writer.Write('>');
    > writer.Write(item.Text);
    > writer.WriteEndTag("option");
    > writer.WriteLine();
    > }
    > }
    > }
    > }
    > }
    >
    > This is only skelet of new class but it will solve your main problem. I
    > think ListItem does not allow to set Mode because it is also used in ListBox
    > and DropDownList where html tags are not rendered.
    >
    > Regards,
    > Ladislav
    >
    > "Nathan Sokalski" wrote:
    >
    > > I have become very frustrated with the ListItem control lately due to what
    > > happens when the Text property contains HTML tags. When the Text property
    > > contains HTML tags, or character codes such as , they are converted to
    > > HTML (for example, <br/> would be rendered as <br/>). This can become
    > > very frustrating when you want to bold or underline part or all of a
    > > ListItem, which is not that uncommon of a task. Also, because this would be
    > > extremely trivial to do in an HTML file or classic ASP (and I am guessing
    > > most other server-side technologies), most clients will complain about it.
    > > Is there a reason that the ListItem does this? Why doesn't the ListItem have
    > > a Mode property that can be set to PassThrough, similar to what the Literal
    > > control has. I think this would solve the problem, and still allow the
    > > people that do want their HTML converted to have that option. But since the
    > > Mode property doesn't currently exist (I haven't looked at .NET 3.0, but
    > > since I still use VS2005 and .NET 2.0, that really doesn't matter yet), can
    > > someone tell me a way to use the ListItem and still do a little simple
    > > formatting like bolding and underlining (using simple stuff like the <b></b>
    > > and <u></u> tags) in my code? Thanks.
    > > --
    > > Nathan Sokalski
    > >
    > > http://www.nathansokalski.com/
    > >
    > >
    > >
    Ladislav Mrnka, Aug 17, 2007
    #3
    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. Guest
    Replies:
    1
    Views:
    402
    William F. Robertson, Jr.
    Oct 28, 2003
  2. Eric Robishaw
    Replies:
    1
    Views:
    4,318
    Victor Garcia Aprea [MVP]
    Dec 27, 2003
  3. Arthur Dent

    Inherited DropDownList and asp:listitems

    Arthur Dent, Aug 15, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    510
    Arthur Dent
    Aug 16, 2006
  4. Nathan Sokalski

    ListItems and formatting

    Nathan Sokalski, Aug 16, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    334
    =?Utf-8?B?TGFkaXNsYXYgTXJua2E=?=
    Aug 17, 2007
  5. Arthur Dent

    Inherited DropDownList and asp:listitems

    Arthur Dent, Aug 15, 2006, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    128
    Arthur Dent
    Aug 16, 2006
Loading...

Share This Page