asp.net 에서 Listbox 바 인 딩 여러 옵션 을 선택 하고 삭제 하 는 방법 입 니 다.
 
<%@ Page Language="C#" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<script runat="server"> 
protected void Page_Load(object sender, EventArgs e) 
{ 
for (int i = 0; i < ListBox1.Items.Count; i++) 
{ 
if (ListBox1.Items[i].Value == "A" || ListBox1.Items[i].Value == "C") 
{ 
ListBox1.Items[i].Selected = true; 
} 
} 
} 
</script> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
<title></title> 
</head> 
<body> 
<form id="form1" runat="server"> 
<asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple"> 
<asp:ListItem>A</asp:ListItem> 
<asp:ListItem>B</asp:ListItem> 
<asp:ListItem>C</asp:ListItem> 
<asp:ListItem>D</asp:ListItem> 
<asp:ListItem>E</asp:ListItem> 
</asp:ListBox> 
</form> 
</body> 
</html> 
을 살 펴 보 겠 습 니 다.위 에는 여러 개의 바 인 딩 만 있 을 뿐 입 니 다.하지만 여러 개의 옵션 을 어떻게 바 인 딩 하 는 동시에 여러 개의 옵션 을 선택 하 는 지 보 겠 습 니 다.
.aspx: 
<asp:TextBox ID="TextBox1" runat="server" Width="300"></asp:TextBox> 
<br /> 
<asp:Button ID="Button1" runat="server" Text="Binding" OnClick="Button1_Click" /> 
<br /> 
<br /> 
<asp:ListBox ID="ListBox1" runat="server" Height="100" SelectionMode="Multiple" ></asp:ListBox> . 
aspx.cs 에서 먼저 Listbox 를 위 한 데 이 터 를 준비 한 다음 에 Listbox 컨트롤 에 데 이 터 를 연결 합 니 다. 
protected void Page_Load(object sender, EventArgs e) 
{ 
if (!IsPostBack) 
{ 
Data_Binding(); 
} 
} 
private void Data_Binding() 
{ 
this.ListBox1.DataSource = Site(); 
this.ListBox1.DataTextField = "key"; 
this.ListBox1.DataValueField = "value"; 
this.ListBox1.DataBind(); 
} 
private Dictionary<string, string> Site() 
{ 
Dictionary<string, string> site = new Dictionary<string, string>(); 
site.Add("Insus.NET cnblogs", https://www.jb51.net); 
site.Add("Microsoft", "http://www.microsoft.com"); 
site.Add("Google", "http://www.google.com"); 
site.Add("Yahoo", "http://www.yahoo.com.cn"); 
site.Add("Ifeng", "http://www.ifeng.com"); 
site.Add("sina", http://www.baidu.com); 
site.Add("163", "http://www.163.com"); 
site.Add("QQ", "http://www.qq.com"); 
return site; 
} 
TextBox 의 문자열 을";"여러 값 으로 나 누 어 네 임 스페이스 using System.collections 를 참조 하 였 습 니 다.다음은 button 을 쓰 는 click 이벤트 입 니 다.코드 가 상당히 간단 합 니 다.Insus.NET 은 여기에 많은 설명 을 하지 않 습 니 다. 
protected void Button1_Click(object sender, EventArgs e) 
{ 
string[] s = this.TextBox1.Text.Split(';'); 
foreach (ListItem li in this.ListBox1.Items) 
{ 
li.Selected = ((IList)s).Contains(li.Text) ? true : false; 
} 
} 
마지막 으로 listbox 의 여러 옵션 을 삭제 하 는 방법 을 살 펴 보 겠 습 니 다.여러 옵션 을 삭제 하 는 방법 
int coun =listBox2.SelectedItems.Count; 
for(;coun> =1;coun--) 
{ 
listBox2.Items.RemoveAt(listBox2.SelectedIndices[coun-1]); 
} 
listBox2.ClearSelected(); 
} 
한 네티즌 이 foreach(object d in listbox 2.Selected Items)라 는 줄 에 문제 가 있 습 니 다.그 중 하 나 를 삭제 할 때,listbox 의 옵션 이 변경 되 었 습 니 다.foreach 를 다시 호출 하면 당연히 문제 가 있 을 것 입 니 다!해결 방법 은 listBox 2.Selected Items 를 배열 변 수 를 먼저 넣 고 호출 하 는 것 입 니 다!하지만 더 쉬 운 방법 은 listBox 2.ClearSelected()를 직접 호출 하 는 것 이다.그래,이 한 마디 로 모든 문 제 를 해결 했다!모든 선택 한 항목 이 삭 제 됩 니 다.이 글 은 listbox 가 여러 옵션 과 listbox 를 연결 하 는 동시에 여러 개의 선택 상 태 를 설정 하 는 옵션 을 설명 합 니 다.마지막 으로 여러 개의 바 인 딩 옵션 을 삭제 하 는 방법 을 설명 합 니 다.
                이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
WPF에서 내용이 Class 항목의 ListBox.SelectedItem을 가져옵니다.이미 나올지도 모르지만 비망록으로. SelectedItems(복수 선택)의 꺼내는 방법도 썼습니다 목록과 내용을 준비합니다. MainWindow.xaml<TextBlock Text="{Binding Id, Strin...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.