ListBox基本功能使用方法
ListBox基本功能首先是列表項的添加,客戶(hù)端實(shí)現代碼添加在listbox實(shí)例化代碼中間,例如:
<asp:ListItem Value="value" Selected=True>Text</asp:ListItem>
若在服務(wù)器端實(shí)現,為避免每次加載時(shí)執行添加列表項,上述代碼包含在下面代碼中:
if(!IsPostBack)
{
}
WebForm頁(yè)面上須添加2個(gè)listbox(listbox1和lixtbox2)和2個(gè)命令按鈕,listbox1不為空。列表項從listbox1添加到listbox2須在Button1單擊事件中調用Add方法:
ListBox2.Items.Add(ListBox1.SelectedValue);
若要從listbox2中刪除列表項的話(huà)須在Button2單擊事件中調用Remove方法:
ListBox2.Items.Remove(ListBox2.SelectedValue);
列表項從listbox1添加到listbox2后,列表項從listbox1中刪除:
int i=0;
while(i<ListBox1.Items.Count)
{
if(ListBox1.Items[i].Selected==true)
{
ListBox2.Items.Add(ListBox1.Items[i]);
ListBox1.Items.Remove(ListBox1.Items[i]);
}
else
i+=1;
}
這樣只能實(shí)現單項添加,想要實(shí)現多項添加,首先設置ListBox1的SelectionMode屬性值Multiple,ListBox1允許多項選中。
在Button1單擊事件中添加
foreach(ListItem MyItem in ListBox1.Items)
if(MyItem.Selected==true)
ListBox2.Items.Add(MyItem);
想要一次清空ListBox2中所有選項可在Button2單擊事件中調用clear方法,
ListBox2.Items.Clear();
若列表項已經(jīng)添加,不允許二次添加,Button1單擊事件中的代碼包含在:
if(ListBox2.Items.FindByValue(ListBox1.SelectedValue)==null)
{
}
ListBox與數據庫綁定就是指定他的DataSource和DataTextField屬性,
ListBox2.DataSource=數據源;
ListBox2.DataTextField="字段名";
ListBox2.DataBind();
<script type="text/javascript">
function SelectAll()
{
var lst1=window.document.getElementById("<%=lb_Sourse.ClientID %>");
var length = lst1.options.length;
var string = window.document.getElementById("<%=hf_NewName.ClientID %>")
for(var i=0;i<length;i++)
{
var v = lst1.options[i].value;
var t = lst1.options[i].text;
var lst2=window.document.getElementById("<%=lb_NewName.ClientID %>");
lst2.options[i] = new Option(t,v,true,true);
string.value+=v;
}
}
function DelAll()
{
var lst2=window.document.getElementById("<%=lb_NewName.ClientID %>");
var length = lst2.options.length;
for(var i=length;i>0;i--)
{
lst2.options[i-1].parentNode.removeChild(lst2.options[i-1]);
}
}
function SelectOne()
{
var string = window.document.getElementById("<%=hf_NewName.ClientID %>")
var lst1=window.document.getElementById("<%=lb_Sourse.ClientID %>");
var lst2=window.document.getElementById("<%=lb_NewName.ClientID %>");
var lstindex=lst1.selectedIndex;
var length = lst2.options.length;
var isExists = false;
if(lstindex<0)
return;
else if(length != null)
{
for(var i=0;i < length; i++)
{
if(lst2.options[i].text == lst1[lstindex].text&&lst2.options[i].value == lst1[lstindex].value)
{
isExists = true;
}
}
}
else
{
return;
}
if (isExists == false)
{
var v = lst1.options[lstindex].value;
var t = lst1.options[lstindex].text;
lst2.options[lst2.options.length] = new Option(t,v,true,true);
string.value+=v;
}
else
{
alert("所選條目已經(jīng)存在");
return false;
}
}
function DelOne()
{
var lst2=window.document.getElementById("<%=lb_NewName.ClientID %>");
var lstindex=lst2.selectedIndex;
if(lstindex>=0)
{
var v = lst2.options[lstindex].value+";";
lst2.options[lstindex].parentNode.removeChild(lst2.options[lstindex]);
}
}
</script>

希望對大家有所幫助!
ASP.NET中添加控件ListBox , 屬性設為 Multiple , 則可進(jìn)行多選.
就以?xún)蓚€(gè)listbox之間多選添加項目為例.
兩個(gè)控件為listboxleft , listboxright 定義了一個(gè)動(dòng)態(tài)數組用于中間存儲 arrRight .具體代碼如下:
//讀取右邊選中項目
ArrayList arrRight = new ArrayList();
foreach(ListItem item in this.ListBoxRight.Items) //按類(lèi)型listitem讀取listbox中選定項
{
if(item.Selected) //判斷是否選中
{
arrRight.Add(item);
}
}
//右邊移除選定項目 左邊添加
foreach(ListItem item in arrRight)
{
this.ListBoxLeft.Items.Add(item);
this.ListBoxRight.Items.Remove(item);
}
不能將item的添加刪除直接寫(xiě)在if(item.Selected){}內,因為項目remove后會(huì )出現錯誤
添加兩個(gè)listbox (ListBoxAll , ListBoxUser) 兩個(gè)按鈕( ButtonListDel >> , ButtonListAdd <<)
按鈕的代碼為:
private void ButtonListDel_Click(object sender, System.EventArgs e)
{
//listbox >> 刪除listboxuser選中項目 將其添加入listboxall
if(this.ListBoxUser.SelectedIndex != -1)
{
this.ListBoxAll.Items.Add(this.ListBoxUser.SelectedItem.Value);
this.ListBoxUser.Items.Remove(this.ListBoxUser.SelectedItem.Value);
}
}
private void ButtonListAdd_Click(object sender, System.EventArgs e)
{
//listbox <<
if(this.ListBoxAll.SelectedIndex != -1)
{
this.ListBoxUser.Items.Add(this.ListBoxAll.SelectedItem.Value);
this.ListBoxAll.Items.Remove(this.ListBoxAll.SelectedItem.Value);
}
}
1 為了確保添加不會(huì )重復 填充listbox時(shí)使兩邊無(wú)重復項目.
完成listbox里項目的添加、刪除的關(guān)鍵代碼:
1.通過(guò)AddRange方法添加項目:this.lbyx.Items.AddRange(new object[] {"北京","上海","天津","成都","廣州","深圳","武漢"});
2.添加items:this.lbbx.Items.Add(this.lbyx.Text);
3.清空列表內的所有items:this.lbbx.Items.Clear();
4.當前所選項的編號獲取:this.lbbx.SelectedIndex
5.刪除某項:this.lbbx.Items.RemoveAt(this.lbbx.SelectedIndex);
在從一個(gè)ListBox選擇內容copy到另外一個(gè)ListBox時(shí)候用下面的方法:
if (ListBox2.Items.IndexOf(ListBox1.SelectedItem) == -1)
{
ListBox2.Items.Add(new ListItem(ListBox1.SelectedValue));
//ListBox2.Items.Add(ListBox1.SelectedItem); <--用這個(gè)會(huì )記錄狀態(tài),ListBox2不支持Multiple就出錯了
}
微軟quickstart提供的
<asp:listbox width="100px" runat=server>
<asp:listitem>Roman</asp:listitem>
<asp:listitem>Arial Black</asp:listitem>
<asp:listitem>Garamond</asp:listitem>
<asp:listitem>Somona</asp:listitem>
<asp:listitem>Symbol</asp:listitem>
</asp:listbox>
void RemoveAllBtn_Click(Object Src, EventArgs E) {
while (InstalledFonts.Items.Count != 0) {
AvailableFonts.Items.Add(new ListItem(InstalledFonts.Items[0].Value));
InstalledFonts.Items.Remove(InstalledFonts.Items[0].Value);
}
}
當數據源改為
<asp:listbox width="100px" runat=server>
<asp:listitem value="1">Roman</asp:listitem>
<asp:listitem value="bbb">Arial Black</asp:listitem>
<asp:listitem value="333">Garamond</asp:listitem>
<asp:listitem value="4">Somona</asp:listitem>
<asp:listitem value="5">Symbol</asp:listitem>
</asp:listbox>
listbox.items.count會(huì )一直為總數,不會(huì )順while循環(huán)的變化,可以修改為如果方法:
#region button
private void btnRemoveAll_Click(object sender, System.EventArgs e)
{
while (lstSelDpt.Items.Count != 0)
{
lstAllDpt.Items.Add(lstSelDpt.Items[lstSelDpt.Items.Count-1]);
lstSelDpt.Items.RemoveAt(lstSelDpt.Items.Count-1);
}
}
private void btnRemove_Click(object sender, System.EventArgs e)
{
while(lstSelDpt.SelectedIndex != -1)
{
lstAllDpt.Items.Add(lstSelDpt.Items[lstSelDpt.SelectedIndex]);
lstSelDpt.Items.Remove(lstSelDpt.Items[lstSelDpt.SelectedIndex]);
}
}
private void btnAdd_Click(object sender, System.EventArgs e)
{
while (lstAllDpt.SelectedIndex != -1)
{
lstSelDpt.Items.Add(lstAllDpt.Items[lstAllDpt.SelectedIndex]);
lstAllDpt.Items.Remove(lstAllDpt.Items[lstAllDpt.SelectedIndex]);
}
}
private void btnAddAll_Click(object sender, System.EventArgs e)
{
while (lstAllDpt.Items.Count != 0)
{
lstSelDpt.Items.Add(lstAllDpt.Items[lstAllDpt.Items.Count-1]);
lstAllDpt.Items.Remove(lstAllDpt.Items[lstAllDpt.Items.Count-1]);
}
}
#endregion
我在用dotnet做一個(gè)項目的過(guò)程中,遇到了一個(gè)ListBox的問(wèn)題:通過(guò)在一個(gè)ListBox中雙擊,把選中的項添加到另一個(gè)ListBox中,但ListBox控件本身并沒(méi)有該事件,那么如何實(shí)現呢?我就想到了客戶(hù)端腳本javascrit,通過(guò)查閱相關(guān)資料,終于把這個(gè)問(wèn)題解決了,現在寫(xiě)出來(lái)與大家分享,希望能對大家有所幫助。
這里有三個(gè)問(wèn)題:
第一:雙擊所要執行的javascript代碼是什么?
注意:javascript代碼的語(yǔ)法要正確,即每一行都要以“;”結尾;
function change()
{
var addOption=document.createElement("option");
var index1;
if(document.Form1.ListBox1.length==0)return(false);
index1=document.Form1.ListBox1.selectedIndex;
if(index1<0)return(false);
addOption.text=document.Form1.ListBox1.options(index1).text;
addOption.value=document.Form1.ListBox1.value;
document.Form1.ListBox2.add(addOption);
document.Form1.ListBox1.remove (index1);
}
第二:如何將 javascript 代碼轉換為C#代碼?
public static void ListBox_DblClick(Page page,System.Web.UI.WebControls.WebControl webcontrol,string SourceControlName,string TargetControlName)
{
SourceControlName = "document.Form1." + SourceControlName;
TargetControlName = "document.Form1." + TargetControlName;
string js = "<script language=javascript> function change(SourceControlName,TargetControlName)";
js += "{";
js += "var addOption=document.createElement('option'); \n";
js += " var index1; \n";
js += "if(SourceControlName.length==0)return(false);\n";
js += " index1=SourceControlName.selectedIndex; \n ";
js += " if(index1<0)return(false);\n";
聯(lián)系客服