楼上的所言既是。 if not IsPostBack then ' 在此初始化DropDownList end if
你是通过选择DropDownList中的某一项目来添加,还是直接将DropDownList中全部的项添加到ListBox中?? 选择: ListBox1.Items.Add(DropDownList.SelectedItem);全部: foreach(ListItem li in DropDownList1.Items) { ListBox1.Items.Add(li); }注意,如果你的DropDownList的绑定时,务必放到if(!Page.IsPostBack){....}里面
{
if(!isPostBack)
{
//第一次进入页面才进行绑定DropDownList
}
}
每次增加时都会首先触发page_load()事件,若每次都绑定,则selectedIndex自动设为第一项
if not IsPostBack then
' 在此初始化DropDownList
end if
选择:
ListBox1.Items.Add(DropDownList.SelectedItem);全部:
foreach(ListItem li in DropDownList1.Items)
{
ListBox1.Items.Add(li);
}注意,如果你的DropDownList的绑定时,务必放到if(!Page.IsPostBack){....}里面
修改建议:
把DropDownList控件任一个Item添加到ListBox控件中:应该指动态形式,选一项添加一项。
那么:droplistdown的autopostback=true;
并设置IndexChange 事件响应代码函数为:IndexChange_DDL(……)
{
ListBox1.Items.Add(DropDownList.SelectedItem.Text或DropDownList.SelectedItem.value);//以 Items.Add () 方法的要求做。
}
同时:保证web Control 控件包含在<form runat=server>标签内