StatesDropDownList Control
Life With Nate
Nate's Poetry Page
Resume (PDF)
Nate's Code
ASP.NET 2.0/3.5
ButtonExtensions
ConditionalRequiredTextValidator
CSSImageMap
CustomStyle
DateTimePicker
DateTimePickerTemplated
LengthValidator
RestrictInputTextBox
RestrictInputValidator
Rollovers
RotatedTextHandler
RotatedTextImage
Rotators
SelectedCountValidator
SlideIntoView
StatesDropDownList
UniqueValueValidatorBase
VB.NET 2.0/3.5
Just For Fun
Windows Phone 7
Description
The StatesDropDownList is simply a convenience for all those times when you need to create forms allowing the user to select a state. It inherits from the standard DropDownList and is for the most part used in the same way (other than the fact that you will probably rarely want to add any ListItems!).
Example
AL
AK
AZ
AR
CA
CO
CT
DE
FL
GA
HI
ID
IL
IN
IA
KS
KY
LA
ME
MD
MA
MI
MN
MS
MO
MT
NE
NV
NH
NJ
NM
NY
NC
ND
OH
OK
OR
PA
RI
SC
SD
TN
TX
UT
VT
VA
WA
WV
WI
WY
AL - Alabama
AK - Alaska
AZ - Arizona
AR - Arkansas
CA - California
CO - Colorado
CT - Connecticut
DC - District of Columbia
DE - Delaware
FL - Florida
GA - Georgia
HI - Hawaii
ID - Idaho
IL - Illinois
IN - Indiana
IA - Iowa
KS - Kansas
KY - Kentucky
LA - Louisiana
ME - Maine
MD - Maryland
MA - Massachusetts
MI - Michigan
MN - Minnesota
MS - Mississippi
MO - Missouri
MT - Montana
NE - Nebraska
NV - Nevada
NH - New Hampshire
NJ - New Jersey
NM - New Mexico
NY - New York
NC - North Carolina
ND - North Dakota
OH - Ohio
OK - Oklahoma
OR - Oregon
PA - Pennsylvania
RI - Rhode Island
SC - South Carolina
SD - South Dakota
TN - Tennessee
TX - Texas
UT - Utah
VT - Vermont
VA - Virginia
WA - Washington
WV - West Virginia
WI - Wisconsin
WY - Wyoming
--Select A State--
Alabama
Alaska
Arizona
Arkansas
California
Colorado
Connecticut
District of Columbia
Delaware
Florida
Georgia
Hawaii
Idaho
Illinois
Indiana
Iowa
Kansas
Kentucky
Louisiana
Maine
Maryland
Massachusetts
Michigan
Minnesota
Mississippi
Missouri
Montana
Nebraska
Nevada
New Hampshire
New Jersey
New Mexico
New York
North Carolina
North Dakota
Ohio
Oklahoma
Oregon
Pennsylvania
Rhode Island
South Carolina
South Dakota
Tennessee
Texas
Utah
Vermont
Virginia
Washington
West Virginia
Wisconsin
Wyoming
Alabama (AL)
Alaska (AK)
Arizona (AZ)
Arkansas (AR)
California (CA)
Colorado (CO)
Connecticut (CT)
District of Columbia (DC)
Delaware (DE)
Florida (FL)
Georgia (GA)
Hawaii (HI)
Idaho (ID)
Illinois (IL)
Indiana (IN)
Iowa (IA)
Kansas (KS)
Kentucky (KY)
Louisiana (LA)
Maine (ME)
Maryland (MD)
Massachusetts (MA)
Michigan (MI)
Minnesota (MN)
Mississippi (MS)
Missouri (MO)
Montana (MT)
Nebraska (NE)
Nevada (NV)
New Hampshire (NH)
New Jersey (NJ)
New Mexico (NM)
New York (NY)
North Carolina (NC)
North Dakota (ND)
Ohio (OH)
Oklahoma (OK)
Oregon (OR)
Pennsylvania (PA)
Rhode Island (RI)
South Carolina (SC)
South Dakota (SD)
Tennessee (TN)
Texas (TX)
Utah (UT)
Vermont (VT)
Virginia (VA)
Washington (WA)
West Virginia (WV)
Wisconsin (WI)
Wyoming (WY)
Alabama = AL
Alaska = AK
Arizona = AZ
Arkansas = AR
California = CA
Colorado = CO
Connecticut = CT
District of Columbia = DC
Delaware = DE
Florida = FL
Georgia = GA
Hawaii = HI
Idaho = ID
Illinois = IL
Indiana = IN
Iowa = IA
Kansas = KS
Kentucky = KY
Louisiana = LA
Maine = ME
Maryland = MD
Massachusetts = MA
Michigan = MI
Minnesota = MN
Mississippi = MS
Missouri = MO
Montana = MT
Nebraska = NE
Nevada = NV
New Hampshire = NH
New Jersey = NJ
New Mexico = NM
New York = NY
North Carolina = NC
North Dakota = ND
Ohio = OH
Oklahoma = OK
Oregon = OR
Pennsylvania = PA
Rhode Island = RI
South Carolina = SC
South Dakota = SD
Tennessee = TN
Texas = TX
Utah = UT
Vermont = VT
Virginia = VA
Washington = WA
West Virginia = WV
Wisconsin = WI
Wyoming = WY
AL is Alabama
AK is Alaska
AZ is Arizona
AR is Arkansas
CA is California
CO is Colorado
CT is Connecticut
DC is District of Columbia
DE is Delaware
FL is Florida
GA is Georgia
HI is Hawaii
ID is Idaho
IL is Illinois
IN is Indiana
IA is Iowa
KS is Kansas
KY is Kentucky
LA is Louisiana
ME is Maine
MD is Maryland
MA is Massachusetts
MI is Michigan
MN is Minnesota
MS is Mississippi
MO is Missouri
MT is Montana
NE is Nebraska
NV is Nevada
NH is New Hampshire
NJ is New Jersey
NM is New Mexico
NY is New York
NC is North Carolina
ND is North Dakota
OH is Ohio
OK is Oklahoma
OR is Oregon
PA is Pennsylvania
RI is Rhode Island
SC is South Carolina
SD is South Dakota
TN is Tennessee
TX is Texas
UT is Utah
VT is Vermont
VA is Virginia
WA is Washington
WV is West Virginia
WI is Wisconsin
WY is Wyoming
The following tags were used to create the examples above:
<NJS:StatesDropDownList runat="server" StatesFormat="Abbreviation" DisplayDC="false"/> <NJS:StatesDropDownList runat="server" StatesFormat="AbbreviationName" DisplayBlank="false"/> <NJS:StatesDropDownList runat="server" StatesFormat="Name" BlankText="--Select A State--"/> <NJS:StatesDropDownList runat="server" StatesFormat="NameAbbreviation"/> <NJS:StatesDropDownList runat="server" StatesFormat="Custom" StatesFormatString="{1} = {0}"/> <NJS:StatesDropDownList runat="server" StatesFormat="Custom" StatesFormatString="{0} is {1}"/>
Properties & Methods
StatesDropDownList
The StatesDropDownList is a simple convenience control that inherits from the standard DropDownList and is used for selecting a state. Most properties inherited from System.Web.UI.WebControls.DropDownList are not listed here, but are available.
StatesFormatString
- The format used to list the states in the generated DropDownList. The format string uses standard formatstring syntax, with {0} representing the abbreviated state name and {1} representing the full state name
StatesFormat
- One of the predefined formatstrings from the StatesDisplayStyle enumeration, default is Abbreviation
DisplayDC
- Specifies whether or not District of Columbia/DC should be included in the list of states, default is True
DisplayBlank
- Specifies whether or not to include an extra ListItem at the beginning of the list, default is True
BlankText
- The text to be used for the first ListItem if DisplayBlank is set to True, default is String.Empty
SelectedValue (inherited from DropDownList)
- The value of the selected item. This will be the state abbreviation in capital letters or String.Empty if DisplayBlank is set to True and is selected
StatesDisplayStyle As Byte
An enumeration used to specify the formatstring used to select a predefined formatstring for the StatesFormat property. If you wish to specify a custom formatstring you must select StatesDisplayStyle.Custom.
Abbreviation
- Displays only the state abbreviation
AbbreviationName
- Displays the state abbreviation followed by a dash and the state name, equivelant to using StatesFormatString="{0} - {1}"
Custom
- Uses the formatstring specified in StatesFormatString, which uses {0} to represent the state abbreviation and {1} to represent the state name
Name
- Displays only the state name
NameAbbreviation
- Displays the state name followed by a space and the state abbreviation in parentheses, equivelant to using StatesFormatString="{1} ({0})"
Source Code
StatesDropDownList.vb:
Namespace NathanSokalski <System.Drawing.ToolboxBitmap(GetType(ToolboxIcons), "StatesDropDownList.bmp"), _ System.Web.UI.ToolboxData("<{0}:StatesDropDownList runat=""server""/>")> _ Public Class StatesDropDownList : Inherits System.Web.UI.WebControls.DropDownList Private _statesformatstring As String = "{0}" Private _displaydc As Boolean = True Private _displayblank As Boolean = True Private _blanktext As String = String.Empty <Description("The format used to list the states in the generated DropDownList")> _ Public WriteOnly Property StatesFormatString() As String Set(ByVal value As String) Me._statesformatstring = value End Set End Property <DefaultValue("Abbreviation"), _ Description("One of the predefined formats from the StatesDisplayStyle enumeration")> _ Public WriteOnly Property StatesFormat() As StatesDisplayStyle Set(ByVal value As StatesDisplayStyle) Select Case value Case StatesDisplayStyle.Abbreviation : Me._statesformatstring = "{0}" Case StatesDisplayStyle.AbbreviationName : Me._statesformatstring = "{0} - {1}" Case StatesDisplayStyle.Name : Me._statesformatstring = "{1}" Case StatesDisplayStyle.NameAbbreviation : Me._statesformatstring = "{1} ({0})" End Select End Set End Property <DefaultValue(True), _ Description("Specifies whether or not District of Columbia/DC should be included in the list of states")> _ Public WriteOnly Property DisplayDC() As Boolean Set(ByVal value As Boolean) Me._displaydc = value End Set End Property <DefaultValue(True), _ Description("Specifies whether or not to include an extra ListItem at the beginning of the DropDownList")> _ Public WriteOnly Property DisplayBlank() As Boolean Set(ByVal value As Boolean) Me._displayblank = value End Set End Property <Description("The text to be used for the first ListItem if DisplayBlank is set to True")> _ Public WriteOnly Property BlankText() As String Set(ByVal value As String) Me._blanktext = value End Set End Property Private Sub StatesDropDownList_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init If Me._displayblank Then Me.Items.Add(New ListItem(Me._blanktext, String.Empty)) Me.Items.Add(New ListItem(String.Format(Me._statesformatstring, "AL", "Alabama"), "AL")) Me.Items.Add(New ListItem(String.Format(Me._statesformatstring, "AK", "Alaska"), "AK")) Me.Items.Add(New ListItem(String.Format(Me._statesformatstring, "AZ", "Arizona"), "AZ")) Me.Items.Add(New ListItem(String.Format(Me._statesformatstring, "AR", "Arkansas"), "AR")) Me.Items.Add(New ListItem(String.Format(Me._statesformatstring, "CA", "California"), "CA")) Me.Items.Add(New ListItem(String.Format(Me._statesformatstring, "CO", "Colorado"), "CO")) Me.Items.Add(New ListItem(String.Format(Me._statesformatstring, "CT", "Connecticut"), "CT")) If Me._displaydc Then Me.Items.Add(New ListItem(String.Format(Me._statesformatstring, "DC", "District of Columbia"), "DC")) Me.Items.Add(New ListItem(String.Format(Me._statesformatstring, "DE", "Delaware"), "DE")) Me.Items.Add(New ListItem(String.Format(Me._statesformatstring, "FL", "Florida"), "FL")) Me.Items.Add(New ListItem(String.Format(Me._statesformatstring, "GA", "Georgia"), "GA")) Me.Items.Add(New ListItem(String.Format(Me._statesformatstring, "HI", "Hawaii"), "HI")) Me.Items.Add(New ListItem(String.Format(Me._statesformatstring, "ID", "Idaho"), "ID")) Me.Items.Add(New ListItem(String.Format(Me._statesformatstring, "IL", "Illinois"), "IL")) Me.Items.Add(New ListItem(String.Format(Me._statesformatstring, "IN", "Indiana"), "IN")) Me.Items.Add(New ListItem(String.Format(Me._statesformatstring, "IA", "Iowa"), "IA")) Me.Items.Add(New ListItem(String.Format(Me._statesformatstring, "KS", "Kansas"), "KS")) Me.Items.Add(New ListItem(String.Format(Me._statesformatstring, "KY", "Kentucky"), "KY")) Me.Items.Add(New ListItem(String.Format(Me._statesformatstring, "LA", "Louisiana"), "LA")) Me.Items.Add(New ListItem(String.Format(Me._statesformatstring, "ME", "Maine"), "ME")) Me.Items.Add(New ListItem(String.Format(Me._statesformatstring, "MD", "Maryland"), "MD")) Me.Items.Add(New ListItem(String.Format(Me._statesformatstring, "MA", "Massachusetts"), "MA")) Me.Items.Add(New ListItem(String.Format(Me._statesformatstring, "MI", "Michigan"), "MI")) Me.Items.Add(New ListItem(String.Format(Me._statesformatstring, "MN", "Minnesota"), "MN")) Me.Items.Add(New ListItem(String.Format(Me._statesformatstring, "MS", "Mississippi"), "MS")) Me.Items.Add(New ListItem(String.Format(Me._statesformatstring, "MO", "Missouri"), "MO")) Me.Items.Add(New ListItem(String.Format(Me._statesformatstring, "MT", "Montana"), "MT")) Me.Items.Add(New ListItem(String.Format(Me._statesformatstring, "NE", "Nebraska"), "NE")) Me.Items.Add(New ListItem(String.Format(Me._statesformatstring, "NV", "Nevada"), "NV")) Me.Items.Add(New ListItem(String.Format(Me._statesformatstring, "NH", "New Hampshire"), "NH")) Me.Items.Add(New ListItem(String.Format(Me._statesformatstring, "NJ", "New Jersey"), "NJ")) Me.Items.Add(New ListItem(String.Format(Me._statesformatstring, "NM", "New Mexico"), "NM")) Me.Items.Add(New ListItem(String.Format(Me._statesformatstring, "NY", "New York"), "NY")) Me.Items.Add(New ListItem(String.Format(Me._statesformatstring, "NC", "North Carolina"), "NC")) Me.Items.Add(New ListItem(String.Format(Me._statesformatstring, "ND", "North Dakota"), "ND")) Me.Items.Add(New ListItem(String.Format(Me._statesformatstring, "OH", "Ohio"), "OH")) Me.Items.Add(New ListItem(String.Format(Me._statesformatstring, "OK", "Oklahoma"), "OK")) Me.Items.Add(New ListItem(String.Format(Me._statesformatstring, "OR", "Oregon"), "OR")) Me.Items.Add(New ListItem(String.Format(Me._statesformatstring, "PA", "Pennsylvania"), "PA")) Me.Items.Add(New ListItem(String.Format(Me._statesformatstring, "RI", "Rhode Island"), "RI")) Me.Items.Add(New ListItem(String.Format(Me._statesformatstring, "SC", "South Carolina"), "SC")) Me.Items.Add(New ListItem(String.Format(Me._statesformatstring, "SD", "South Dakota"), "SD")) Me.Items.Add(New ListItem(String.Format(Me._statesformatstring, "TN", "Tennessee"), "TN")) Me.Items.Add(New ListItem(String.Format(Me._statesformatstring, "TX", "Texas"), "TX")) Me.Items.Add(New ListItem(String.Format(Me._statesformatstring, "UT", "Utah"), "UT")) Me.Items.Add(New ListItem(String.Format(Me._statesformatstring, "VT", "Vermont"), "VT")) Me.Items.Add(New ListItem(String.Format(Me._statesformatstring, "VA", "Virginia"), "VA")) Me.Items.Add(New ListItem(String.Format(Me._statesformatstring, "WA", "Washington"), "WA")) Me.Items.Add(New ListItem(String.Format(Me._statesformatstring, "WV", "West Virginia"), "WV")) Me.Items.Add(New ListItem(String.Format(Me._statesformatstring, "WI", "Wisconsin"), "WI")) Me.Items.Add(New ListItem(String.Format(Me._statesformatstring, "WY", "Wyoming"), "WY")) End Sub Protected Overrides Function LoadPostData(ByVal postDataKey As String, ByVal postCollection As System.Collections.Specialized.NameValueCollection) As Boolean Me.SelectedValue = postCollection(postDataKey) Return False End Function Public Enum StatesDisplayStyle As Byte Abbreviation AbbreviationName 'AA - Name Custom 'Must specify format string, {0}=Abbreviation, {1}=Name Name NameAbbreviation 'Name (AA) End Enum End Class End Namespace
Remarks
Because StatesDropDownList overrides the RenderContents method, it may not function as expected if you declaratively add ListItems using the <asp:ListItem/> tag. If you wish to add any ListItems other than the ones I have made available, I suggest that you either modify my code (which you are more than welcome to do) or write your own control. If there is an option you think should be included, feel free to send it to me and I will take it into consideration.