首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

(急)一个关于listbox的简单有关问题!为什么移除不掉listbox中的项

2012-01-10 
(急)一个关于listbox的简单问题!为什么移除不掉listbox中的项!有两个listbox两个按钮,其中一个是绑定到数

(急)一个关于listbox的简单问题!为什么移除不掉listbox中的项!
有两个listbox两个按钮,其中一个是绑定到数据库获取Items,选择一项后点添加就是在选定的listbox中移除该项,在另外一个listbox中添加项,现在的问题是能够添加,但移除不不了,代码如下:

protected   void   Page_Load(object   sender,   EventArgs   e)
        {
               
//绑定listbox
if   (!IsPostBack)
                {
                        listBoxBind();
                }                        
        }
//添加一项并移除listall中的选定项,这个有问题
        protected   void   btnAdd_Click(object   sender,   EventArgs   e)
        {
                if   (ListAll.SelectedIndex   !=   -1)
                {
                        ListHas.Items.Add(new   ListItem(ListAll.SelectedItem.Text));
                        ListAll.Items.Remove(ListAll.SelectedItem.Text);
                        ListAll.DataBind();
                }
        }
//这个没问题,添加和移除都好使!
        protected   void   btnRemov_Click(object   sender,   EventArgs   e)
        {
                if   (ListHas.SelectedIndex   !=   -1)
                {
                        ListAll.Items.Add(new   ListItem(ListHas.SelectedItem.Text));
                        ListHas.Items.Remove(ListAll.SelectedItem.Text);
                }
        }
//绑定list
        public   void   listBoxBind()
        {
                comm.CommandText   =   "select   *   from   model ";
                comm.Connection   =   conn;
                da.SelectCommand   =   comm;
                try
                {
                        da.Fill(dt);
                        this.ListAll.DataSource   =   dt;
                        this.ListAll.DataTextField   =   "name ";
                        this.ListAll.DataValueField   =   "id ";
                        this.ListAll.DataBind();
                }
                catch   {   }


                finally
                {
                        da.Dispose();
                        dt.Clear();
                        comm.Dispose();
                        conn.Close();
                }
        }
谢谢各位!

[解决办法]
沙发沙发,帮忙顶

[解决办法]
给你一个方法

protected void Add_Item(ListBox sourceLstBox, ListBox targetLstBox)
{
if (sourceLstBox.SelectedItem != null)
{
targetLstBox.Items.Add(sourceLstBox.Items[sourceLstBox.SelectedIndex]);
sourceLstBox.Items.Remove(sourceLstBox.Items[sourceLstBox.SelectedIndex]);
}
}


[解决办法]
因为你又重新 ListAll.DataBind(); 了

可以试试加ispostback
[解决办法]
try:

protected void btnAdd_Click(object sender, EventArgs e)
{
if (ListAll.SelectedIndex != -1)
{
ListHas.Items.Add(new ListItem(ListAll.SelectedItem.Text));
ListAll.Items.Remove(ListAll.SelectedItem);
ListAll.DataBind();
}
}
//这个没问题,添加和移除都好使!
protected void btnRemov_Click(object sender, EventArgs e)
{
if (ListHas.SelectedIndex != -1)
{
ListAll.Items.Add(new ListItem(ListHas.SelectedItem.Text));
ListHas.Items.Remove(ListAll.SelectedItem);
}
}
[解决办法]
你移除之后又重新绑定了
[解决办法]
ListAll.DataBind();
多余

[解决办法]
//添加
function AddItem(lst1,lst2)
{
var elemAll = document.getElementById(lst1);
var elemSel = document.getElementById(lst2);
var len=elemSel.length;
if(len <3)
{
for(var i=0;i <elemAll.options.length;i++)
{
if(elemAll.options[i].selected)
{
var SelText=elemAll.options[i].innerText;
var SelValue = elemAll.options[i].value;
var Num = elemSel.options.length;
elemSel.options[Num]=new Option(SelText,SelValue);
elemAll.options.remove(i);
i--;
}
}
}
}
//删除
function RemoveItem(lst1,lst2)
{
var elemAll = document.getElementById(lst1);
var elemSel = document.getElementById(lst2);
for(var i=0;i <elemSel.options.length;i++)
{
if(elemSel.options[i].selected)
{
var SelText = elemSel.options[i].innerText;
var SelValue = elemSel.options[i].value;
var Num = elemAll.options.length;
elemAll.options[Num]= new Option(SelText,SelValue);
elemSel.options.remove(i);


i--;
}
}
}
[解决办法]
ListHas.Items.Remove(ListAll.SelectedItem.Text);
这句话有问题,好好看看,修改如下
ListHas.Items.Remove(ListHas.SelectedItem);

热点排行