Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ListItems and formatting

Reply
Thread Tools

ListItems and formatting

 
 
Nathan Sokalski
Guest
Posts: n/a
 
      08-16-2007
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.velocityreviews.com/forums/(E-Mail Removed)
http://www.nathansokalski.com/


 
Reply With Quote
 
 
 
 
=?Utf-8?B?TGFkaXNsYXYgTXJua2E=?=
Guest
Posts: n/a
 
      08-17-2007
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[i];
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
> (E-Mail Removed)
> http://www.nathansokalski.com/
>
>
>

 
Reply With Quote
 
 
 
 
=?Utf-8?B?TGFkaXNsYXYgTXJua2E=?=
Guest
Posts: n/a
 
      08-17-2007
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[i];
> 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
> > (E-Mail Removed)
> > http://www.nathansokalski.com/
> >
> >
> >

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ListItems and formatting Nathan Sokalski ASP .Net Web Controls 2 08-17-2007 01:23 AM
Inherited DropDownList and asp:listitems Arthur Dent ASP .Net 2 08-16-2006 03:07 PM
Inherited DropDownList and asp:listitems Arthur Dent ASP .Net Building Controls 1 08-16-2006 03:07 PM
Change forecolor for individual listitems in CheckBoxList Eric Robishaw ASP .Net 1 12-27-2003 07:50 PM
ListItems are added in reverse order ASP .Net 1 10-28-2003 07:10 PM



Advertisments