<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="RepeatedValueBinding" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table width="100%">
<tr>
<td>
<select runat="server" ID="Select1" size="3" DataTextField="Key" DataValueField="Value"
NAME="Select1" />
</td>
<td>
<select runat="server" ID="Select2" DataTextField="Key" DataValueField="Value" NAME="Select2" />
</td>
<td>
<asp:ListBox runat="server" ID="Listbox1" Size="3" DataTextField="Key" DataValueField="Value" />
</td>
<td>
<asp:DropDownList runat="server" ID="DropdownList1" DataTextField="Key" DataValueField="Value" />
</td>
<td>
<asp:RadioButtonList runat="server" ID="OptionList1" DataTextField="Key" DataValueField="Value" />
</td>
<td>
<asp:CheckBoxList runat="server" ID="CheckList1" DataTextField="Key" DataValueField="Value" />
</td>
</tr>
</table>
<asp:Button runat="server" Text="Get Selection" ID="cmdGetSelection" OnClick="cmdGetSelection_Click" />
<br>
<br>
<asp:Literal runat="server" ID="Result" EnableViewState="False" />
</div>
</form>
</body>
</html>
File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class RepeatedValueBinding : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Hashtable ht = new Hashtable(3);
ht.Add("value1", "Key1");
ht.Add("value2", "Key2");
ht.Add("value3", "Key3");
Select1.DataSource = ht;
Select2.DataSource = ht;
Listbox1.DataSource = ht;
DropdownList1.DataSource = ht;
CheckList1.DataSource = ht;
OptionList1.DataSource = ht;
Page.DataBind();
}
}
protected void cmdGetSelection_Click(object sender, EventArgs e)
{
if (Select1.SelectedIndex != -1)
Result.Text += "- Item selected in Select1: " + Select1.Items[Select1.SelectedIndex].Text + " - " + Select1.Value + "<br>";
if (Select2.SelectedIndex != -1)
Result.Text += "- Item selected in Select2: " + Select2.Items[Select2.SelectedIndex].Text + " - " + Select2.Value + "<br>";
if (Listbox1.SelectedIndex != -1)
Result.Text += "- Item selected in Listbox1: " + Listbox1.SelectedItem.Text + " - " + Listbox1.SelectedItem.Value + "<br>";
if (DropdownList1.SelectedIndex != -1)
Result.Text += "- Item selected in DropdownList1: " + DropdownList1.SelectedItem.Text + " - " + DropdownList1.SelectedItem.Value + "<br>";
if (OptionList1.SelectedIndex != -1)
Result.Text += "- Item selected in OptionList1: " + OptionList1.SelectedItem.Text + " - " + OptionList1.SelectedItem.Value + "<br>";
if (CheckList1.SelectedIndex != -1)
{
Result.Text += "- Items selected in CheckList1: ";
foreach (ListItem li in CheckList1.Items)
{
if (li.Selected)
Result.Text += li.Text + " - " + li.Value + " ";
}
}
}
}