Click here to Skip to main content
Licence CPOL
First Posted 6 May 2009
Views 20,325
Downloads 1,234
Bookmarked 44 times

DropDown and MultiselectDropDown Controls for ASP.NET

By | 6 May 2009 | Article
Custom DropDown and MultiselectDropDown controls with data binding support
dropdown-screen-0.PNG

Description

This article discusses about two user controls written in ASP.NET. Working on a project recently, I needed to develop a custom DropDown control with multiselect options in the form of CheckBox. I searched around for something simple, yet useful for my needs, and I wasn't able to find anything, so I developed my own control. Later, I reused the code to create a regular DropDown control (no multiselect checkbox options). The two controls can be merged into one, but I might do that at a later stage. On to the controls. 

DropDown

This control can be bound to a IListSource to fetch data to be populated, in addition to the regular Items collection that can be populated from the asp. Here are a few examples of how this control can be used in ASPX.

<%@ Register Src="Controls/DropDown.ascx" TagName="DropDown" TagPrefix="thp" %>

<thp:DropDown ID="DropDown1" runat="server" Width="200">
     <Items>
          <asp:ListItem Text="Some text" Value="0" Selected="True" />
          <asp:ListItem Text="Another option" Value="1" />
     </Items>
</thp:DropDown>
<thp:DropDown ID="DropDown2" runat="server" Width="200" DataSourceID="DataSource1"
            DataTextField="Name" DataValueField="Id" />
<thp:DropDown ID="DropDown3" runat="server" Width="200" 
	OnNeedDataSource="DropDown3_NeedDataSource"
         DataTextField="Name" DataValueField="Id" OnClientChange="onDropDownChange" />

Here's a screenshot of what the above code would render:

dropdown-screen-1.PNG

Both controls support the following data events:

  • ItemDataBound - Fired after the object has been bound to an Item. The argument passes both the bound object (DataObject) and the ListItem.
  • NeedDataSource - Fired when there's no DataSourceID specified. This event is helpful when you want to manually populate the controls with your data. See the source code provided for specifics on this, you can use either Items collection or create custom list source (or both).

MultiselectDropDown

The code and usage is exactly as the DropDown control. Here's a screenshot of all of the above scenarios:

multidropdown-screen-1.PNG

The styling in the attached project uses .skin files in the default theme. You can easily modify these to fit your needs. Everything can be styled.

I hope you find this code useful. I recommend this to anyone that wants to start developing custom controls as it has very important features implemented, such as the IPostBackDataHandler interface to pass data between postbacks in a custom manner. It also features some nice DataBinding examples and how you can use and modify these to perform specific tasks.

History

  • 6th May, 2009: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Tomislav Markovski

Web Developer

Macedonia, The Republic Of Macedonia, The Republic Of

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionCreate Control Dynamically PinmemberFrank A M8:15 20 Apr '12  
AnswerRe: Create Control Dynamically PinmemberFrank A M10:42 1 May '12  
QuestionHow to connect to sql datasource. Pinmembervishalmetkar21:37 30 Sep '11  
QuestionBug Pinmemberig88sir8:53 18 Jul '11  
AnswerRe: Bug PinmemberTomislav Markovski8:57 18 Jul '11  
GeneralRe: Bug Pinmemberig88sir9:25 18 Jul '11  
GeneralAssign datatable to it Pinmemberashu218816:32 2 May '11  
GeneralRe: Assign datatable to it PinmemberTomislav Markovski21:14 2 May '11  
QuestionWant to change data at runtime? PinmemberMansi20100:05 9 Nov '10  
GeneralRequest Pinmemberabhinav8647:36 20 Oct '10  
GeneralRe: Request Pinmemberashu218816:43 2 May '11  
GeneralJust what i needed PinmemberDavevandeven1:46 5 Jan '10  
GeneralLooks great PinmemberJeffCirceo16:17 11 May '09  
Generalcool article PinmemberJuraj Suchan6:56 6 May '09  
GeneralGreat idea. Pinmembergstolarov6:01 6 May '09  
GeneralRe: Great idea. PinmemberTomislav Markovski22:29 6 May '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 6 May 2009
Article Copyright 2009 by Tomislav Markovski
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid