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

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