views:

893

answers:

4

I have the need for a dropdownlist with the U.S. states in it. I could need this dropdownlist on various pages. Instead of always having to create a listitem for each state, I want a simple control. I don't want a UserControl - I want to extend the existing dropdownlist control. I know I have done this before at a previous employer, but for the life of me I can't remember how to do it!

Can't I just do something like this for each state? If so, where?

MyBase.Items.Add(New ListItem("Illinois","IL"))

Any ideas out there?

Thanks

+2  A: 

All you have to do is create a new class and inherit from the appropriate control:

/// <summary>
/// </summary>
[DefaultProperty("DataTable"),
     ToolboxData("<{0}:ExtendedDropDownlist runat=server></{0}:ExtendedDropDownlist>")]
public class ExtendedDropDownList : DropDownList


    /// <summary>
    /// Render this control to the output 
    /// parameter specified.
    /// </summary>
    /// <param name="output"> The HTML writer to 
    /// write out to </param>
    protected override void Render(HtmlTextWriter output)
    {
        //output.Write(Text);
        base.Render(output);
    }

In the constructor just add the appropriate listbox items like you have.

You may need to put it in a different project and reference the dll. I think I remember something about that, but it's been a while since I have had to do it.

Kevin
I have actually tried that, but no such luck.
Dan Appleyard
Try putting it in a different project and refencing the dll.
Kevin
Already done sir.
Dan Appleyard
You may need to add the render override too. I found that in the code I know works, and that may have been the trick.
Kevin
Never mind, I don't think that's it. I didn't think it made sense that it would be. Can you post the code you have so we can see?
Kevin
A: 

Extend DropDownList control and override OnLoad event and add your items like that :

protected override void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
     this.Items.Add(new ListItem("Illinois","IL"));
    }
}
Canavar
A: 
<ToolboxData("<{0}:StateDropDownList runat=server></{0}:StateDropDownList>")> _
Public Class StateDropDownList
    Inherits DropDownList

    Protected Overrides Sub RenderContents(ByVal output As HtmlTextWriter)
        output.Write(Text)
    End Sub

    Protected Overrides Sub RenderChildren(ByVal writer As System.Web.UI.HtmlTextWriter)
        MyBase.RenderChildren(writer)
    End Sub


    Private Sub LoadStates()
        MyBase.Items.Add(New ListItem("Alabama", "AL"))
        'Additional states removed for size
        MyBase.Items.Add(New ListItem("Wyoming", "WY"))
    End Sub

    Public Sub New()
        'tried does not work
        ' LoadStates()
    End Sub

    Private Sub StateDropDownList_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        'tried does not work
        ' LoadStates()
    End Sub


    Private Sub StateDropDownList_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'tried does not work
        'If Not Page.IsPostBack Then
        '    LoadStates()
        'End If
    End Sub

    Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
        MyBase.Render(writer)
    End Sub
End Class
Dan Appleyard
I don't know VB.net at all, but could it have something to do with using MyBase on the items, or on rendering?
Kevin
Kevin, I just replaced MyBase with Me (VB's this equivalent), and the same thing...
Dan Appleyard
A: 
public class DropDownListStates : System.Web.UI.WebControls.DropDownList
{
    protected override void CreateChildControls()
    {
        if (Items.Count > 0) return;

        Items.Add(new System.Web.UI.WebControls.ListItem("AL"));

        // other states here...

        Items.Add(new System.Web.UI.WebControls.ListItem("WY"));

    }
}
Andrew Robinson
I have tried this - and all I get is an empty dropdown but in the source view I get this: <select name="StateDropDownList1" id="StateDropDownList1"> AL</select>
Dan Appleyard

related questions