Binding String to DropdownList/Using items as Properties

This example shows three things –
1. having a long list of items in a string, and binding them to a DropDownList
2. Using the items as Properties in code
3. Accessing the items in the DropDownlist programmatically

This example uses the long list of colors you can use as properties.
The string is bound to the DropDownList, and then, choosing the color, using the SelectedIndex property from the DropDownList, programmatically, with the Button click events (Next and Previous).
All you need to do is copy the code below into a blank page and run it from your DotNet enabled web server.

<html>
<%@ Import Namespace=”System.Drawing” %>
<head>
<meta name=”GENERATOR” Content=”ASP Express 2.0″>
<title>Binding String to DropdownList/Using items as Properties</title>
<script language=”VB” runat=”server”>
Protected strBColor as string
Sub Page_Load(Source as Object, E as EventArgs)

if not Page.IsPostBack then
Dim LongList() As String ={“AliceBlue”, “AntiqueWhite”, “Aqua”, “Aquamarine”, “Azure”, “Beige”, “Bisque”, “Black”,”BlanchedAlmond”,”Blue”,”BlueViolet”,”Brown”,”BurlyWood”,”CadetBlue”,”Chartreuse”,”Chocolate”,”Coral”,”CornflowerBlue”,”Cornsilk”,”Crimson”,”Cyan”,”DarkBlue”,”DarkCyan”,”DarkGoldenrod”,”DarkGray”,”DarkGreen”,”DarkKhaki”,”DarkMagenta”,”DarkOliveGreen”,”DarkOrange”,”DarkRed”,”DarkSalmon”,”DarkSeaGreen”,”DarkSlateBlue”,”DarkSlateGray”,”DarkTurquoise”,”DarkViolet”,”DeepPink”,”DeepSkyBlue”,”DimGray”,”DodgerBlue”,”FireBrick”,”FloralWhite”,”ForestGreen”,”Fuchsia”,”Gainsboro”,”GhostWhite”,”Gold”,”Goldenrod”,”Gray”,”Green”,”GreenYellow”,”HoneyDew”,”HotPink”,”IndianRed”,”Indigo”,”Ivory”,”Khaki”,”Lavender”,”LavenderBlush”,”LawnGreen”,”LemonChiffon”,”LightBlue”,”LightCoral”,”LightCyan”,”LightGoldenrodYellow”,”LightGray”,”LightGreen”,”LightPink”,”LightSalmon”,”LightSkyBlue”,”LightSteelBlue”,”LightYellow”,”Lime”,”LimeGreen”,”Linen”,”Magenta”,”Maroon”,”MediumAquamarine”,”MediumBlue”,”MediumOrchid”,”MediumPurple”,”MediumSeaGreen”,”MediumSlateBlue”,”MediumSpringGreen”,”MediumTurquoise”,”MediumVioletRed”,”MidnightBlue”,”MintCream”,”MistyRose”,”Moccasin”,”NavajoWhite”,”Navy”,”OldLace”,”Olive”,”OliveDrab”,”Orange”,”OrangeRed”,”Orchid”,”PaleGoldenrod”,”PaleGreen”,”PaleTurquoise”,”PaleVioletRed”,”PapayaWhip”,”PeachPuff”,”Peru”,”Pink”,”Plum”,”PowderBlue”,”Purple”,”Red”,”RosyBrown”,”RoyalBlue”,”SaddleBrown”,”Salmon”,”SandyBrown”,”SeaGreen”,”SeaShell”,”Sienna”,”Silver”,”SkyBlue”,”SlateGray”,”Snow”,”SpringGreen”,”SteelBlue”,”Tan”,”Teal”,”Thistle”,”Tomato”,”Turquoise”,”Violet”,”Wheat”,”White”,”WhiteSmoke”,”Yellow”,”YellowGreen”}
dd1.datasource=LongList
dd1.databind()
doLabels
end if
End Sub

Sub doLabels()
Label1.Text = “You selected ” & dd1.SelectedItem.Text
Label1.BackColor = Color.FromName(dd1.SelectedItem.Text)
dd1.BackColor = Color.FromName(dd1.SelectedItem.Text)
Label2.Text = “Selected Index = ” & dd1.SelectedIndex & _
” of (Item Count) ” & dd1.Items.Count – 1
End Sub

Sub dd1_SelectedIndexChanged(Source as Object, E as EventArgs)
doLabels
End Sub

Sub Button1_Click(Source as Object, E as EventArgs)
strBColor = dd1.SelectedItem.Text
If dd1.SelectedIndex <> dd1.Items.Count – 1 Then
dd1.SelectedIndex = dd1.SelectedIndex + 1
Else
dd1.SelectedIndex = dd1.SelectedIndex
End If
doLabels
End Sub

Sub Button2_Click(Source as Object, E as EventArgs)
If dd1.SelectedIndex <> 0 Then
dd1.SelectedIndex = dd1.SelectedIndex – 1
dolabels
End If
End Sub
</script>
</head>
<body>

<Form id=”form1″ runat=”server”>
<asp:DropDownList id=”dd1″ autopostback=”true” OnSelectedIndexChanged=”dd1_SelectedIndexChanged” runat=”server” />
<asp:Button id=”button1″ Text=”Next Color” onclick=”button1_click” tabindex=”0″ runat=”server” />
<asp:Button onclick=”button2_click” id=Button2 runat=”server” Width=”92″ Height=”25px” Text=”Previous Color”></asp:Button><p>
<asp:Label id=Label1 runat=”server” Width=”449px” Height=”75px” BorderStyle=”Groove” Font-Size=”Large” Font-Names=”Verdana” Font-Italic=”True” Font-Bold=”True”></asp:Label><br>
<asp:Label ID=”label2″ runat=”server” />
</Form>

</body>
</html>

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>