Sampson-Li
Sampson.Li学习经验总结博客 学历代表过去,能力代表现在.学习力代表未来!

首先js判断listbox不为空

var lst1=window.document.getElementById("SourceListBox").length;

if(lst1==0)

{

alert("不能为空!");

}

然后两个listbox移植

 1 function MoveListBoxToRight(leftname, rightname) {
2 var size = $("#" + leftname + " option").size();
3 var selsize = $("#" + leftname + " option:selected").size();
4 if (size > 0 && selsize > 0) {
5 $.each($("#" + leftname + " option:selected"), function(id, own) {
6 var text = $(own).text();
7 var tag = $(own).attr("tag");
8 $("#" + rightname).prepend("<option tag=\"" + tag + "\">" + text + "</option>");
9 $(own).remove();
10 $("#" + leftname + "").children("option:first").attr("selected", true);
11 });
12 }
13 $.each($("#" + rightname + " option"), function(id, own) {
14 orderrole(rightname);
15 });
16 }//将ListBox中的数据移动到另外一个ListBox中,leftname为ListBox的name,rightname为另外一个ListBox的name

js操作页面listbox的选中值

 1 <script type="text/javascript">
2 function SelectAll()//选择源全部插入到目标
3 {
4 var lst1=window.document.getElementById("SourceListBox");
5 var length = lst1.options.length;
6 for(var i=0;i<length;i++)
7 {
8 var v = lst1.options[i].value;
9 var t = lst1.options[i].text;
10 var lst2=window.document.getElementById("DestinationListBox");
11 lst2.options[i] = new Option(t,v,true,true);
12 }
13 }
14 function DelAll()//删除全部
15 {
16 var lst2=window.document.getElementById("DestinationListBox");
17 var length = lst2.options.length;
18 for(var i=length;i>0;i--)
19 {
20 lst2.options[i-1].parentNode.removeChild(lst2.options[i-1]);
21 }
22 }
23 function SelectOne()//选择获得焦点的项目
24 {
25 var lst1=window.document.getElementById("SourceListBox");
26 var lstindex=lst1.selectedIndex;
27 if(lstindex<0)
28 return;
29 var v = lst1.options[lstindex].value;
30 var t = lst1.options[lstindex].text;
31 var lst2=window.document.getElementById("DestinationListBox");
32 lst2.options[lst2.options.length] = new Option(t,v,true,true);
33 }
34 function DelOne()//删除获得焦点的项目
35 {
36 var lst2=window.document.getElementById("DestinationListBox");
37 var lstindex=lst2.selectedIndex;
38 if(lstindex>=0)
39 {
40 var v = lst2.options[lstindex].value+";";
41 lst2.options[lstindex].parentNode.removeChild(lst2.options[lstindex]);
42 }
43 }
44 </script>

一个刷新的小事例.随意写的,有时间在改成无刷新的

View Code
前台代码(ApplyListBox.aspx):
< %@ Page language="c#" Codebehind="ApplyListBox.aspx.cs" AutoEventWireup="false" Inherits="ApplyListBox.WebForm1" %>
< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
< HTML>
< HEAD>
< title>WebForm1< /title>
< meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
< meta name="CODE_LANGUAGE" Content="C#">
< meta name="vs_defaultClientScript" content="JavaScript">
< meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
< /HEAD>
< body MS_POSITIONING="GridLayout">
< form id="Form1" method="post" runat="server">
< asp:ListBox id="ListBox1" style="Z-INDEX: 100; LEFT: 256px; POSITION: absolute; TOP: 120px" runat="server" SelectionMode="Multiple" Width="73px" Height="134px">
< asp:ListItem Value="1">1< /asp:ListItem>
< asp:ListItem Value="2">2< /asp:ListItem>
< asp:ListItem Value="3">3< /asp:ListItem>
< asp:ListItem Value="4">4< /asp:ListItem>
< asp:ListItem Value="5">5< /asp:ListItem>
< asp:ListItem Value="6">6< /asp:ListItem>
< /asp:ListBox>
< asp:Label id="Label2" style="Z-INDEX: 107; LEFT: 448px; POSITION: absolute; TOP: 96px" runat="server">列表框2< /asp:Label>
< asp:ListBox id="ListBox2" style="Z-INDEX: 101; LEFT: 440px; POSITION: absolute; TOP: 120px" runat="server" SelectionMode="Multiple" Width="72px" Height="134px">< /asp:ListBox>
< asp:Button id="Upbtn" style="Z-INDEX: 102; LEFT: 360px; POSITION: absolute; TOP: 136px" runat="server" Text="上移">< /asp:Button>
< asp:Button id="Movebtn" style="Z-INDEX: 103; LEFT: 360px; POSITION: absolute; TOP: 176px" runat="server" Text="转移">< /asp:Button>
< asp:Button id="Downbtn" style="Z-INDEX: 104; LEFT: 360px; POSITION: absolute; TOP: 216px" runat="server" Text="下移">< /asp:Button>
< asp:Label id="Label1" style="Z-INDEX: 106; LEFT: 264px; POSITION: absolute; TOP: 96px" runat="server">列表框1< /asp:Label>
< /form>
< /body>
< /HTML>
后台代码(ApplyListBox.aspx.cs):
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace ApplyListBox
{
/// < summary>
/// WebForm1 的摘要说明。
/// < /summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.ListBox ListBox1;
protected System.Web.UI.WebControls.ListBox ListBox2;
protected System.Web.UI.WebControls.Button Upbtn;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Button Movebtn;
protected System.Web.UI.WebControls.Button Downbtn;

private void Page_Load(object sender, System.EventArgs e)
{

}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// < summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// < /summary>
private void InitializeComponent()
{
this.Upbtn.Click += new System.EventHandler(this.Upbtn_Click);
this.Movebtn.Click += new System.EventHandler(this.Movebtn_Click);
this.Downbtn.Click += new System.EventHandler(this.Downbtn_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void Movebtn_Click(object sender, System.EventArgs e)
{
int Count = ListBox1.Items.Count;
int Index = 0;
for(int i=0;i< Count-1;i++)
{
ListItem Item = ListBox1.Items[Index];
if(ListBox1.Items[Index].Selected == true)
{
ListBox1.Items.Remove(Item);
ListBox2.Items.Add(Item);
Index--;
}
Index++;
}
}

private void Upbtn_Click(object sender, System.EventArgs e)
{
//若不是第一行则上移
if( ListBox1.SelectedIndex > 0 )
{
string name = ListBox1.SelectedItem.Text;
string ID = ListBox1.SelectedItem.Value;
int index = ListBox1.SelectedIndex;
ListBox1.SelectedItem.Text = ListBox1.Items[index-1].Text;
ListBox1.SelectedItem.Value = ListBox1.Items[index-1].Value;
ListBox1.Items[index-1].Text = name;
ListBox1.Items[index-1].Value = ID;
ListBox1.SelectedIndex --;
}
}

private void Downbtn_Click(object sender, System.EventArgs e)
{
//若不是最后一行则下移
if( ListBox1.SelectedIndex >= 0 && ListBox1.SelectedIndex < ListBox1.Items.Count-1 )
{
string name = ListBox1.SelectedItem.Text;
string ID = ListBox1.SelectedItem.Value;
int index = ListBox1.SelectedIndex;
ListBox1.SelectedItem.Text = ListBox1.Items[index+1].Text;
ListBox1.SelectedItem.Value = ListBox1.Items[index+1].Value;
ListBox1.Items[index+1].Text = name;
ListBox1.Items[index+1].Value = ID;
ListBox1.SelectedIndex ++;
}
}
}
}





posted on 2011-12-06 09:35  Sampson  阅读(1534)  评论(0编辑  收藏  举报