Andy Smith's Blog

Page.RegisterStartupScript('Andy', 'MetaBuilders_WebControls_GainKnowledge();');

A fix for attributes on DDL ListItems

The built in DropDownList doesn't actually render any attributes you set for list items. This stops you from being able to set background colors and such, which people seem to like to do. Well, here's some code that will fix that.

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace MetaBuilders.WebControls {
 public class DDL : System.Web.UI.WebControls.DropDownList {
  protected override void RenderContents(HtmlTextWriter writer) {
   Boolean selectedItemRendered = false;
   for (Int32 i = 0; (i < Items.Count); i++ ) {
    ListItem litem = Items[i];
    
    writer.WriteBeginTag("option");
    if (litem.Selected) {
     if (selectedItemRendered) {
      throw new HttpException("DropDownList does not support multiple selection.");
     }
     selectedItemRendered = true;
     writer.WriteAttribute("selected", "selected", false);
 
    }
    writer.WriteAttribute("value", litem.Value, true);
   
    litem.Attributes.Render(writer);
    writer.Write(">");
    HttpUtility.HtmlEncode(litem.Text, writer);
    writer.WriteEndTag("option");
    writer.WriteLine();
 
   }
 
 
  } 
 }
}

Comments

Fabrice said:

Faster that you on that one Andy ;-)
http://weblogs.asp.net/fmarguerie/posts/3103.aspx
# June 5, 2003 1:47 PM

Andre Colbiornsen // Sweden said:

Here you are, a VB.Net version:

Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Public Class DDL
Inherits System.Web.UI.WebControls.DropDownList

Protected Shadows Sub RenderContents(ByVal writer As Html32TextWriter)
Dim selectedItemRendered As Boolean = False

For i As Int32 = 0 To Items.Count - 1
Dim litem As ListItem = Items(i)

writer.WriteBeginTag("option")

If litem.Selected Then

If selectedItemRendered Then
Throw New HttpException("DropDownList does not support multiple selections.")
End If

selectedItemRendered = True
writer.WriteAttribute("selected", "selected", False)

End If

writer.WriteAttribute("value", litem.Value, True)
litem.Attributes.Render(writer)
writer.Write(">")
HttpUtility.HtmlEncode(litem.Text, writer)
writer.WriteEndTag("option")
writer.WriteLine()
Next

End Sub

End Class
# July 23, 2003 4:26 PM

David 'Satch' Kennedy said:

The VB version of RenderControl is incorrectly posted here and will not replace the RenderControl called from the base class Render (you will note the signature did not match the base class). The corrected VB code is as follows:

Protected Overrides Sub RenderContents(ByVal writer As HtmlTextWriter)
Dim selectedItemRendered As Boolean = False
For i As Int32 = 0 To Items.Count - 1
Dim litem As ListItem = Items(i)
writer.WriteBeginTag("option")
If litem.Selected Then
If selectedItemRendered Then
Throw New HttpException("DropDownList does not support multiple selections.")
End If
selectedItemRendered = True
writer.WriteAttribute("selected", "selected", False)
End If
writer.WriteAttribute("value", litem.Value, True)
litem.Attributes.Render(writer) ' MS left this minor call out :P
writer.Write(">")
HttpUtility.HtmlEncode(litem.Text, writer)
writer.WriteEndTag("option")
writer.WriteLine()
Next
End Sub

Hope this helps someone, I lost several hours on it :P
# July 13, 2004 7:28 PM

Tzion said:

Greeting. What counts is not necessarily the size of the dog in the fight - it's the size of the fight in the dog. Help me! I can not find sites on the: Alavert cause. I found only this - <a href="genericalavert.info/">fee virus protection alavert</a>. Possible books, purchasing planes and men, may be lived by one knowledge on any many location, alavert. Family: learn at exciting of the business faced to the way to result an mouth of the reality of virtues named, alavert. Best regards :eek:, Tzion from Comoros.

# March 27, 2010 7:42 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)