我不确定你是如何填充你的盒子的,我的意思是手动或从数据库表。您需要类似值的 ID,假设我们有 lstBox1 和 lstBox2,我将在页面加载时填充数据,并为 selectedindexchanged 编写代码:
protected void Page_Load(object sender, EventArgs e)
{
//disable code on postback
if (IsPostBack) return;
//creating first list items
ListItem lst11 = new ListItem("Bob","1");
ListItem lst12 = new ListItem("Jack", "2");
ListItem lst13 = new ListItem("Fred", "3");
//populating first list items
lstBox1.Items.Add(lst11);
lstBox1.Items.Add(lst12);
lstBox1.Items.Add(lst13);
//creating second list items
ListItem lst21 = new ListItem("Dave", "1");
ListItem lst22 = new ListItem("Brian", "2");
ListItem lst23 = new ListItem("Lee", "3");
//populating second list items
lstBox2.Items.Add(lst21);
lstBox2.Items.Add(lst22);
lstBox2.Items.Add(lst23);
}
protected void lstBox1_SelectedIndexChanged(object sender, EventArgs e)
{
lstBox2.SelectedValue = lstBox1.SelectedValue;
}
protected void lstBox2_SelectedIndexChanged(object sender, EventArgs e)
{
lstBox1.SelectedValue = lstBox2.SelectedValue;
}