<html>
<head>
<title>Adding Items to a List Control Programmatically</title>
<link rel="stylesheet" href="/shared/netdemos.css">
<style type="text/css">
<!--
#msg {padding:10}
-->
</style>
<script language="C#" runat="server">
void applyColor ( object src, EventArgs e ) {
msg.Style [ "background-color" ] = colorSelect.SelectedItem.Value;
}
void addToList ( object src, EventArgs e ) {
colorSelect.Items.Add ( txtColor.Value );
}
</script>
</head>
<body>
<!-- #include virtual="~/shared/top.inc -->
<div class="header"><h2>Adding Items to a List Control Programmatically</h2></div>
<!-- #include virtual="~/shared/viewsrc_top.inc" -->
<hr size=1 width=92%>
<div align="center">
<form runat="server">
<p><span id="msg" runat="server">Click the button to apply a background color to this span.</span>
<p>Select a color: <asp:ListBox id="colorSelect" runat="server">
<asp:ListItem>SkyBlue</asp:ListItem>
<asp:ListItem>LightGreen</asp:ListItem>
<asp:ListItem>Gainsboro</asp:ListItem>
<asp:ListItem>LemonChiffon</asp:ListItem>
</asp:ListBox>
<input type=button runat="server" Value="Apply" onServerClick="applyColor">
<p>Don’t see your color in the list above? You can add it here:<br>
<input type=text id="txtColor" runat="server">
<input type=button runat="server" Value="Add to List" onServerClick="addToList">
</form>
</div>
<hr size=1 width=92%>
<!-- #include virtual="~/shared/viewsrc.inc" -->
</body>
</html>