Dropdownlist Control with 39 Counties of England

The dropdownlist 'ddlCounties' in the code below contains the 39 counties of England. When the page is loaded for the first time a dropdownlist of all 39 counties is displayed and the label 'Label1' displays the text 'Please select a County'. If you select an option the autopostback property of the dropdownlist which in this case is set to true forces a postback to the server with the dropdownlist's SelectedIndexChanged event being raised and the sub ddlCounties_SelectedIndexChanged run.

The value rather than the text of the selected item is assigned to a string variable 'County', even though in this case they are the same. (If you wanted the text you could use 'ddlCounties.SelectedItem.Text'). The variable is then checked to see if it's empty and if it is our label 'Label1' will display the text 'Please select a County' otherwise the text 'You selected' followed by the name of the selected county will be displayed.

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
Protected Sub ddlCounties_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Dim County As String = ddlCounties.SelectedItem.Value
If County = "" Then
Label1.Text = "Please select a County"
Else
Label1.Text = "You selected " & County
End If
End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
If Not Page.IsPostBack Then
Label1.Text = "Please select a County"
End If
End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Counties of England</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="ddlCounties" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlCounties_SelectedIndexChanged">
<asp:ListItem Value="" Selected="True">Select County</asp:ListItem>
<asp:ListItem Value="Bedfordshire">Bedfordshire</asp:ListItem>
<asp:ListItem Value="Berkshire">Berkshire</asp:ListItem>
<asp:ListItem Value="Buckinghamshire">Buckinghamshire</asp:ListItem>
<asp:ListItem Value="Cambridgeshire">Cambridgeshire</asp:ListItem>
<asp:ListItem Value="Cheshire">Cheshire</asp:ListItem>
<asp:ListItem Value="Cornwall">Cornwall</asp:ListItem>
<asp:ListItem Value="Cumberland">Cumberland</asp:ListItem>
<asp:ListItem Value="Derbyshire">Derbyshire</asp:ListItem>
<asp:ListItem Value="Devon">Devon</asp:ListItem>
<asp:ListItem Value="Dorset">Dorset</asp:ListItem>
<asp:ListItem Value="Durham">Durham</asp:ListItem>
<asp:ListItem Value="Essex">Essex</asp:ListItem>
<asp:ListItem Value="Gloucestershire">Gloucestershire</asp:ListItem>
<asp:ListItem Value="Hampshire">Hampshire</asp:ListItem>
<asp:ListItem Value="Herefordshire">Herefordshire</asp:ListItem>
<asp:ListItem Value="Hertfordshire">Hertfordshire</asp:ListItem>
<asp:ListItem Value="Huntingdonshire">Huntingdonshire</asp:ListItem>
<asp:ListItem Value="Kent">Kent</asp:ListItem>
<asp:ListItem Value="Lancashire">Lancashire</asp:ListItem>
<asp:ListItem Value="Leicestershire">Leicestershire</asp:ListItem>
<asp:ListItem Value="Lincolnshire">Lincolnshire</asp:ListItem>
<asp:ListItem Value="Middlesex">Middlesex</asp:ListItem>
<asp:ListItem Value="Norfolk">Norfolk</asp:ListItem>
<asp:ListItem Value="Northamptonshire">Northamptonshire</asp:ListItem>
<asp:ListItem Value="Northumberland">Northumberland</asp:ListItem>
<asp:ListItem Value="Nottinghamshire">Nottinghamshire</asp:ListItem>
<asp:ListItem Value="Oxfordshire">Oxfordshire</asp:ListItem>
<asp:ListItem Value="Rutland">Rutland</asp:ListItem>
<asp:ListItem Value="Shropshire">Shropshire</asp:ListItem>
<asp:ListItem Value="Somerset">Somerset</asp:ListItem>
<asp:ListItem Value="Staffordshire">Staffordshire</asp:ListItem>
<asp:ListItem Value="Suffolk">Suffolk</asp:ListItem>
<asp:ListItem Value="Surrey">Surrey</asp:ListItem>
<asp:ListItem Value="Sussex">Sussex</asp:ListItem>
<asp:ListItem Value="Warwickshire">Warwickshire</asp:ListItem>
<asp:ListItem Value="Westmorland">Westmorland</asp:ListItem>
<asp:ListItem Value="Wiltshire">Wiltshire</asp:ListItem>
<asp:ListItem Value="Worcestershire">Worcestershire</asp:ListItem>
<asp:ListItem Value="Yorkshire">Yorkshire</asp:ListItem>
</asp:DropDownList><br />

<asp:Label ID="Label1" runat="server"></asp:Label>
</div>
</form>
</body>
</html>


Get the best asp web hosting provider now and save 30%

Advertisements



MembersPro

MembersPro PayPal - ASP Membership software

Plug and play ASP membership script that integrates with PayPal to let you charge recurring membership fees.