views:

778

answers:

4

Hi, I want to subclass the built-in DropDownList in ASP.NET so that I can add functionality to it and use it in my pages. I tried doing this with a UserControl but found that it doesn't expose the internal DropDownList (logically, I guess). I've googled for the answer but can't find anything.

I've come as far as writing the actual class, and it's possible to subclass from DropDownList but I'm unable to register the file in my ASP.NET page and use it in the source view. Maybe I'm missing some properties in my class?

Any ideas?

+1  A: 

Do you really need to extend it? or can you just have a user control with the DDL in it and then hook up the various events (OnSelectedIndexChanged, OnDataBinding etc) inside your user control to add your own functionality when one of those events happen?

What kind of additional functionality are you trying to add?

Si Keep
The only thing I want to do with it is add an InitialValue property that defines the first value that is always present in the DDL.
deadtime
Would it be sensible to just expose the DDL as innerDropDownList or something?
deadtime
Could you use ddlxxx.Items.Insert(); somewhere in the user control maybe straight after binding to your data source?
Si Keep
Another option would be to take the original source and create a new source (collection or whatever) that includes the first value and the source data and then bind to your new composite source.
Si Keep
+7  A: 

You want to extend DropDownList in a Custom Control... not in a usercontrol.

Create a new Class Library Project called MyLibrary.

Add a class called MyDropDownList.cs

namespace My.Namespace.Controls
{
[ToolboxData("<{0}:MyDropDownList runat=\"server\"></{0}:MyDropDownList>")]
public class MyDropDownList: DropDownList
{
    // your custom code goes here
    // e.g.
    protected override void  RenderContents(HtmlTextWriter writer)
    {
        //Your own render code
    }
}
}

Once you compile your library, you can add a reference to it in your web application.

And a tagprefix in your web.config

    <add tagPrefix="my" namespace="My.Namespace.Controls" assembly="MyLibrary" />

That should allow you to add this to your aspx/ascx's

<my:MyDropDownList ID="myDDl" runat="server">
    ...
</my:MyDropDownList>
Eoin Campbell
+1 This is a great description of how to create custom controls which inherit their functionality from the base ASP.NET objects. Very clear and concise!
kdmurray
A: 

In a comment, you clarify your goal: "The only thing I want to do with it is add an InitialValue property that defines the first value that is always present in the DDL."

I don't think you need to create a special user control or custom control to accomplish that.

I often use this code throughout my web apps to populate list controls. You pass in a boolean indicating whether an additional ListItem is to be added at the top of the list, and the text for that item.

    public static void BindListControl (ListControl ctl, SqlDataReader dr,
  String textColumn, String valueColumn, bool addBlankRow, string blankRowText)
 {
  ctl.Items.Clear();
  ctl.DataSource = dr;
  ctl.DataTextField = textColumn;
  ctl.DataValueField = valueColumn;
  ctl.DataBind();

  if (addBlankRow == true) ctl.Items.Insert(0, blankRowText);
 }

This is useful, for example, if you want every DropDownList to have as its initial value a blank, or text such as "Select a city".

DOK
A: 

Your example is really good. Can u send me the full code of implementation that solves a great problem of mine. Please do send me the code.

John oliveson