ListBox控件綁定數據庫的兩種方法
實現方法1:用綁定數據源的方法
using System.Data;
using System.Data.SqlClient;
..................
//建立連接,連接字符串的內容要視具體情況而定,這裏只給出連接到本機的範例
SqlConnection con=new SqlConnection("server=(local);database=department;uid=sa;pwd=");
con.Open( );
SqlCommand cm=new SqlCommand("select empID,empName from emp",con);
SqlDataReader dr=cm.ExecuteReader( );
//綁定
this.lbxEmp.DataSource=dr; //lbxEmp爲ListBox對象
this.lbxEmp.DataTextField="empName";
this.lbxEmp.DataValueField="empID";
this.lbxEmp.DataBind( );
dr.Close( );
con.Close( );
實現方法2:使用集合添加
using System.Data;
using System.Data.SqlClient;
..................
//建立連接,連接字符串的內容要視具體情況而定,這裏只給出連接到本機的範例
SqlConnection con=new SqlConnection("server=(local);database=department;uid=sa;pwd=");
con.Open( );
SqlCommand cm=new SqlCommand("select empID,empName from emp",con);
SqlDataReader dr=cm.ExecuteReader( );
while(dr.Read( ))
{
this.lbxEmp.Items.add(new listItem(dr.GetString(1),dr.GetInt32(0).ToString( )));
}
dr.Close( );
con.Close( );
***********************************-----------------
. 屬性列表:
SelectionMode 組件中條目的選擇類型,即多選(Multiple)、單選(Single)
Rows 列表框中顯示總共多少行
Selected 檢測條目是否被選中
SelectedItem 返回的類型是ListItem,獲得列表框中被選擇的條目
Count 列表框中條目的總數
SelectedIndex 列表框中被選擇項的索引值
Items 泛指列表框中的所有項,每一項的類型都是ListItem
2. 取列表框中被選中的值
ListBox.SelectedValue
3. 動態的添加列表框中的項:
ListBox.Items.Add("所要添加的項");
4. 移出指定項:
//首先判斷列表框中的項是否大於0
If(ListBox.Items.Count > 0 )
{
//移出選擇的項
ListBox.Items.Remove(ListBox.SelectedItem);
}
5. 清空所有項:
//首先判斷列表框中的項是否大於0
If(ListBox.Items.Count > 0 )
{
//清空所有項
ListBox.Items.Clear();
}
6. 列表框可以一次選擇多項:
只需設置列表框的屬性 SelectionMode="Multiple",按Ctrl可以多選
7. 兩個列表框聯動,即兩級聯動菜單
//判斷第一個列表框中被選中的值
switch(ListBox1.SelectValue)
{
//如果是"A",第二個列表框中就添加這些:
case "A"
ListBox2.Items.Clear();
ListBox2.Items.Add("A1");
ListBox2.Items.Add("A2");
ListBox2.Items.Add("A3");
//如果是"B",第二個列表框中就添加這些:
case "B"
ListBox2.Items.Clear();
ListBox2.Items.Add("B1");
ListBox2.Items.Add("B2");
ListBox2.Items.Add("B3");
}
8. 實現列表框中項的移位
即:向上移位、向下移位
具體的思路爲:創建一個ListBox對象,並把要移位的項先暫放在這個對象中。
如果是向上移位,就是把當前選定項的的上一項的值賦給當前選定的項,然後
把剛纔新加入的對象的值,再附給當前選定項的前一項。
具體代碼爲:
//定義一個變量,作移位用
index = -1;
//將當前條目的文本以及值都保存到一個臨時變量裏面
ListItem lt=new ListItem (ListBox.SelectedItem.Text,ListBox.SelectedValue);
//被選中的項的值等於上一條或下一條的值
ListBox.Items[ListBox.SelectedIndex].Text=ListBox.Items[ListBox.SelectedIndex + index].Text;
//被選中的項的值等於上一條或下一條的值
ListBox.Items[ListBox.SelectedIndex].Value=ListBox.Items[ListBox.SelectedIndex + index].Value;
//把被選中項的前一條或下一條的值用臨時變量中的取代
ListBox.Items[ListBox.SelectedIndex].Test=lt.Test;
//把被選中項的前一條或下一條的值用臨時變量中的取代
ListBox.Items[ListBox.SelectedIndex].Value=lt.Value;
//把鼠標指針放到移動後的那項上
ListBox.Items[ListBox.SelectedIndex].Value=lt.Value;
9. 移動指針到指定位置:
(1).移至首條
//將被選中項的索引設置爲0就OK了
ListBox.SelectIndex=0;
(2).移至尾條
//將被選中項的索引設置爲ListBox.Items.Count-1就OK了
ListBox.SelectIndex=ListBox.Items.Count-1;
(3).上一條
//用當前被選中的索引去減 1
ListBox.SelectIndex=ListBox.SelectIndex - 1;
(4).下一條
//用當前被選中的索引去加 1
ListBox.SelectIndex=ListBox.SelectIndex + 1;
出自 51CTO.COM博客
this.ListBox1.Items.Insertat(3,new ListItem("插入在第3行之後項",""));
this.ListBox1.Items.Insertat(index,ListItem)
ListBox1.Items.Insert(0,new ListItem("text","value"));
ASP.NET中添加控件ListBox , 屬性設爲 Multiple , 則可進行多選.
就以兩個listbox之間多選添加項目爲例.
兩個控件爲listboxleft , listboxright 定義了一個動態數組用於中間存儲 arrRight .具體代碼如下:
//讀取右邊選中項目
ArrayList arrRight = new ArrayList();
foreach(ListItem item in this.ListBoxRight.Items) //按類型listitem讀取listbox中選定項
{
if(item.Selected) //判斷是否選中
{
arrRight.Add(item);
}
}
//右邊移除選定項目 左邊添加
foreach(ListItem item in arrRight)
{
this.ListBoxLeft.Items.Add(item);
this.ListBoxRight.Items.Remove(item);
}
不能將item的添加刪除直接寫在if(item.Selected){}內,因爲項目remove後會出現錯誤
Move the Item of ListBox
.aspx
--------------------------------
<asp:ListBox id="ListBox1" style="Z-INDEX: 105; LEFT: 96px; POSITION: absolute; TOP: 344px" runat="server">
<asp:ListItem Value="a1">a1</asp:ListItem>
<asp:ListItem Value="a2">a2</asp:ListItem>
<asp:ListItem Value="a3">a3</asp:ListItem>
</asp:ListBox>
----------------------------------
.cs
//for 個
private void Button1_Click(object sender, System.EventArgs e){
if (this.ListBox1.SelectedIndex>=0)
{
int i = ListBox1.SelectedIndex;
if(i>0)
{
string values = this.ListBox1.Items[i].Text ;
this.ListBox1.Items[i].Text = this.ListBox1.Items[i-1].Text ;
this.ListBox1.Items[i-1].Text = values;
}
}
}
ASP.NET中ListBox實現Double Click事件
2007-06-24 13:18
在ASP.NET中的ListBox控件的使用中很多人都發現沒有了WINFORM中ListBox的鼠標雙擊事件
這樣就給我們開發帶來很多不方便的地方
也看了很多CSDN上用JS來實現雙擊事件的方法,都是不完整的,最後發現以下方法是最有效的
首先在WEB頁面上加入JS腳本和存放ListBox事件的隱藏輸入框
再將ASP.NET控件ListBox中加入雙擊事件聲明
<html>
<head>
<script language="javascript">
function ListBox1_DoubleClick() {
/* we will change value of this hidden field so
that in
page load event we can identify event.
*/
document.forms[0].ListBox1Hidden.value = "doubleclicked";
document.forms[0].submit();
}
</script>
</head>
<body>
<form runat="server">
<div>Double click on Listbox
<br />
<asp:ListBox id="ListBox1"
οndblclick="ListBox1_DoubleClick()" runat="server">
<asp:ListItem Value="1">One</asp:ListItem>
<asp:ListItem Value="2">Two</asp:ListItem>
<asp:ListItem Value="3">Three</asp:ListItem>
<asp:ListItem Value="4">Four</asp:ListItem>
</asp:ListBox>
<input type="hidden" name="ListBox1Hidden" />
</div>
<div>click on button
<br />
<asp:Button id="Button1" οnclick="Button1_Click"
runat="server" Text="Button"/>
</div>
</form>
</body>
</html>
最後在WEB窗體加載時候執行下列代碼就能實現雙擊ListBox中Item執行一些操作
void Page_Load(Object sender, EventArgs e){
if(Request.Params["ListBox1Hidden"] != null
&& (string)Request.Params["ListBox1Hidden"] == "doubleclicked" {
//This means It was double click
Response.Write("Double Click was fired selected item is "
+ ListBox1.SelectedItem.Text);
//可以在這裏加要運行的代碼
}
}
希望這篇文章能對大家有幫助,不要再受CSDN上帖子的誤導了
ListBox基本功能首先是列表項的添加,客戶端實現代碼添加在listbox實例化代碼中間,例如:
<asp:ListItem Value="value" Selected=True>Text</asp:ListItem>
若在服務器端實現,爲避免每次加載時執行添加列表項,上述代碼包含在下面代碼中:
if(!IsPostBack)
{
}
WebForm頁面上須添加2個listbox(listbox1和lixtbox2)和2個命令按鈕,listbox1不爲空。列表項從listbox1添加到listbox2須在Button1單擊事件中調用Add方法:
ListBox2.Items.Add(ListBox1.SelectedValue);
若要從listbox2中刪除列表項的話須在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;
}
這樣只能實現單項添加,想要實現多項添加,首先設置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();
若列表項已經添加,不允許二次添加,Button1單擊事件中的代碼包含在:
if(ListBox2.Items.FindByValue(ListBox1.SelectedValue)==null)
{
}
ListBox與數據庫綁定就是指定他的DataSource和DataTextField屬性,
ListBox2.DataSource=數據源;
ListBox2.DataTextField="字段名";
ListBox2.DataBind();
如果是在後臺做的話
AutoPostBack =true
然後給第一個listbox加事件(SelectedIndexChanged)
然後一個遍歷就可以出來了