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();
 
   }
 
 
  } 
 }
}

2 Comments

  • 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(&quot;option&quot;)





    If litem.Selected Then





    If selectedItemRendered Then


    Throw New HttpException(&quot;DropDownList does not support multiple selections.&quot;)


    End If





    selectedItemRendered = True


    writer.WriteAttribute(&quot;selected&quot;, &quot;selected&quot;, False)





    End If





    writer.WriteAttribute(&quot;value&quot;, litem.Value, True)


    litem.Attributes.Render(writer)


    writer.Write(&quot;&gt;&quot;)


    HttpUtility.HtmlEncode(litem.Text, writer)


    writer.WriteEndTag(&quot;option&quot;)


    writer.WriteLine()


    Next





    End Sub





    End Class

  • 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(&quot;option&quot;)

    If litem.Selected Then

    If selectedItemRendered Then

    Throw New HttpException(&quot;DropDownList does not support multiple selections.&quot;)

    End If

    selectedItemRendered = True

    writer.WriteAttribute(&quot;selected&quot;, &quot;selected&quot;, False)

    End If

    writer.WriteAttribute(&quot;value&quot;, litem.Value, True)

    litem.Attributes.Render(writer) ' MS left this minor call out :P

    writer.Write(&quot;&gt;&quot;)

    HttpUtility.HtmlEncode(litem.Text, writer)

    writer.WriteEndTag(&quot;option&quot;)

    writer.WriteLine()

    Next

    End Sub



    Hope this helps someone, I lost several hours on it :P

Comments have been disabled for this content.