ListItems and formatting

N

Nathan Sokalski

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.
 
G

Guest

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
{
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
 
G

Guest

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 said:
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
{
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 said:
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.
 

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

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,838
Latest member
KandiceChi

Latest Threads

Top