动态删除ListBox中的特定项 [英] Remove specific Items from ListBox dynamically

查看:80
本文介绍了动态删除ListBox中的特定项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

调试显示listA.Items.Count返回0;

在此处查看调试器信息



将我的代码附加到我试过的内容:



  public   void  Populate_ListBoxes( string  subj)
{
var elements = subj.Split( new [] {' ,'},System.StringSplitOptions.RemoveEmptyEntries);
string lname = null ;
foreach string items in 元素)
{
lname = Get_Subjects(items);
ListItem li = new ListItem();
li.Text = lname;
li.Value = items;
// ListItem la = listA.Items.FindByValue(items);
listB.Items.Add(LI);
// 将lname与listItem的每个文本值进行比较
// 如果比较成功 - >从列表中删除该项。
// 我不知道如何比较,因为listA.Items .Count返回0
// 如果sqlDataSource用一些填充listA,它怎么能返回0 listItems? ? ??
}
}



函数 Get_Subjects 获取与subjectID对应的主题名称主表。

我调试了代码,发现

 listA.Items.Count 

返回0

我不明白我哪里出错了。任何帮助表示赞赏!

aspx:

 <   td  >  
< asp:ListBox ID = listA runat = server DataSourceID = SqlDataSourceSubjects

DataTextFi字段 = subject DataValueField = subjectID = 6

SelectionMode = 多个 AutoPostBack = True > < / asp :ListBox >
< asp:SqlDataSource ID = SqlDataSourceSubjects runat < span class =code-keyword> = server

ConnectionString = <% $ ConnectionStrings:myconnectionstring%>

SelectCommand = SELECT subjectID,subject FROM mast_subject >
< / asp:SqlDataSource >
< / td >
< td >
< asp:按钮 ID = btnlistAtolistB runat = server

OnClick = btnlistAtolistB_Click 文本 = >> / >

< asp:按钮 ID = btnlistBtolistA runat = server < span class =code-attribute>
< span class =code-attribute> OnClick = btnlistBtolistA_Click 文字 = << / >
< / td >
< td >
< span class =code-keyword>< asp:ListBox ID = listB runat = server = < span class =code-keyword> 6 SelectionMode = 多个 >
< / asp:ListBox >
< / td >

解决方案

ConnectionStrings:myconnectionstring%>

SelectCommand = SELECT subjectID,subject FROM mast_subject >
< / asp:SqlDataSource >
< / td >
< td >
< asp:按钮 ID = btnlistAtolistB runat = server

OnClick = < span class =code-keyword> btnlistAtolistB_Click 文本 = >> / >

< asp:按钮 ID = btnlistBtolistA runat = server OnClick = btnlistBtolistA_Click 文本 = < < / >
< / td >
< td > ;
< asp:ListBox ID = listB runat = server = 6 SelectionMode = 多个 >
< / asp:ListBox > ;
< / td >


移动listA.Items.Clear();来自for循环,因为listA项目将在第一项添加到listB后清除。

因此,首先将listA中的所有项添加到listB,然后清除listA。它会工作。


ListBox.Items.Remove(index);



传递列表框中项目的索引

Debugging shows listA.Items.Count returns 0;
See the debugger information here

attaching my codebehind what I have tried :

public void Populate_ListBoxes(string subj)
{
  var elements = subj.Split(new[] { ',' }, System.StringSplitOptions.RemoveEmptyEntries);
  string lname = null;
  foreach (string items in elements)
  {
    lname = Get_Subjects(items);
    ListItem li = new ListItem();            
    li.Text = lname;
    li.Value = items;
    //ListItem la = listA.Items.FindByValue(items);
    listB.Items.Add(li);
    //compare lname to each of the listItem's text value
    //If comparison successful --> Remove that item from list.
    //I dont know how to compare because listA.Items.Count returns 0
    //How can it return 0 if sqlDataSource is populating listA with some listItems ? ? ??
  }
}


The function Get_Subjects gets the subject name corresponding to subjectID from a master table.
I debugged the code and found

listA.Items.Count

returns "0"
I fail to understand where I'm going wrong. Any help is appreciated!
aspx:

<td>
<asp:ListBox ID="listA" runat="server" DataSourceID="SqlDataSourceSubjects" 

  DataTextField="subject" DataValueField="subjectID" Rows="6" 

  SelectionMode="Multiple" AutoPostBack="True"></asp:ListBox>
<asp:SqlDataSource ID="SqlDataSourceSubjects" runat="server" 

 ConnectionString="<%$ ConnectionStrings:myconnectionstring %>" 

  SelectCommand="SELECT subjectID, subject FROM mast_subject">
</asp:SqlDataSource>
</td>
<td>
<asp:Button ID="btnlistAtolistB" runat="server" 

OnClick="btnlistAtolistB_Click" Text=">>" />
 
<asp:Button ID="btnlistBtolistA" runat="server"                                        OnClick="btnlistBtolistA_Click" Text="<<" />
</td>
<td>
<asp:ListBox ID="listB" runat="server" Rows="6" SelectionMode="Multiple">
</asp:ListBox>
</td>

解决方案

ConnectionStrings:myconnectionstring %>" SelectCommand="SELECT subjectID, subject FROM mast_subject"> </asp:SqlDataSource> </td> <td> <asp:Button ID="btnlistAtolistB" runat="server" OnClick="btnlistAtolistB_Click" Text=">>" />   <asp:Button ID="btnlistBtolistA" runat="server" OnClick="btnlistBtolistA_Click" Text="<<" /> </td> <td> <asp:ListBox ID="listB" runat="server" Rows="6" SelectionMode="Multiple"> </asp:ListBox> </td>


Move listA.Items.Clear(); from the for loop because your listA items will cleared after first item added to listB.
So, first add all items from listA to listB and then clear the listA. It will work.


ListBox.Items.Remove(index);

pass the index of item in the listbox


这篇关于动态删除ListBox中的特定项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆