DropDownList with EmptyDataTemplate

This is a low-impact extension of the standard .NET DropDownList control in order to support an EmptyDataTemplate.  The EmptyDataTemplate concept is used in other places to display some content when the bound datasource returns no results. It’s unfortunately not natively provided in the DropDownList, but I’ve found it to be very helpful.

I confess that this (at present) does not function fully like the normal EmptyDataTemplate usage, which is written as a child control. Rather, it’s simply an additional parameter.  As such, it will really only support text.

This class (like others I’ve documented) use the resources namespace.  The syntax is:

<res:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="name" DataValueField="id" EmptyDataTemplate="No data available." />


And the source is:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Project.resources {
public class DropDownList : System.Web.UI.WebControls.DropDownList {

private String _emptyDataTemplate;

public String EmptyDataTemplate {
get { return this._emptyDataTemplate; }
set { this._emptyDataTemplate = value; }

protected override void OnPagePreLoad(object sender, EventArgs e) {
base.OnPagePreLoad(sender, e);
//Text = "DataTextField";
ListItem li = new ListItem("Text", "Value");

protected override void RenderContents(HtmlTextWriter writer) {
if (this.Items.Count <= 0) { writer.WriteBeginTag("option>");


Leave a Reply

Your email address will not be published. Required fields are marked *