Friday, April 8, 2011

Custom dropdownlist with items autopopulated

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

From stackoverflow
  • 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.

    Dan Appleyard : I have actually tried that, but no such luck.
    Kevin : Try putting it in a different project and refencing the dll.
    Dan Appleyard : Already done sir.
    Kevin : 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?
  • 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"));
        }
    }
    
  • <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
    
    Kevin : I don't know VB.net at all, but could it have something to do with using MyBase on the items, or on rendering?
    Dan Appleyard : Kevin, I just replaced MyBase with Me (VB's this equivalent), and the same thing...
  • 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"));
    
        }
    }
    
    Dan Appleyard : I have tried this - and all I get is an empty dropdown but in the source view I get this:

0 comments:

Post a Comment