2个 listbox 互相调换数据(人员)
来源:互联网 发布:30w买车还是买房知乎 编辑:程序博客网 时间:2024/05/16 05:17
实现功能,对人员的预览调换。
点左,右按钮时不更改人员,点修改时做最后更改!
源视图代码:
02
<table>
03
<tr>
04
<td>
05
<asp:ListBox ID=
"ListBox1"
runat=
"server"
Height=
"200px"
Width=
"150px"
06
SelectionMode=
"Multiple"
></asp:ListBox></td>
07
<td width=
'1'
>
08
<asp:Button ID=
"Button1"
runat=
"server"
Text=
">>"
OnClick=
"Button1_Click"
/>
09
<asp:Button ID=
"Button2"
runat=
"server"
Text=
"<<"
OnClick=
"Button2_Click"
/>
10
</td>
11
<td>
12
<asp:ListBox ID=
"ListBox2"
runat=
"server"
Height=
"200px"
Width=
"150px"
13
SelectionMode=
"Multiple"
></asp:ListBox>
14
</td>
15
</tr>
16
<tr>
17
<td colspan=
"3"
align=
"right"
><asp:Button ID=
"btn"
runat=
"server"
Text=
"修改"
18
onclick=
"btn_Click"
/></td>
19
</tr>
20
</table>
后台代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Left();
Right();
}
}
/// <summary>
/// 右侧
/// </summary>
private void Right()
{
DbHelper db = new DbHelper();
StringBuilder s = new StringBuilder();
s.Append("select * from account where gradeid in (2,3)");DataTable dt = db.GetDataTable(s.ToStr());
this.ListBox2.DataSource = dt;
this.ListBox2.DataTextField = "accountName";
this.ListBox2.DataValueField = "accountid";
this.ListBox2.DataBind();
ViewState["dtR"] = dt;//将右侧数据放到页面临时变量中}
/// <summary>
/// 左侧
/// </summary>
private void Left()
{
DbHelper db = new DbHelper();
StringBuilder s = new StringBuilder();
s.Append("select * from account where gradeid=1");DataTable dt = db.GetDataTable(s.ToStr());
this.ListBox1.DataSource = dt;
this.ListBox1.DataTextField = "accountName";
this.ListBox1.DataValueField = "accountid";
this.ListBox1.DataBind();
ViewState["dtL"] = dt;//将左侧数据放到页面临时变量中}
/// <summary>
/// 左向右移动
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button1_Click(object sender, EventArgs e)
{
//向右移动
if (this.ListBox1.SelectedIndex >= 0)
{
//选中的数据,用来让listbox做增加删除。(当遍历一个listbox集合时做删除的话会影响该items项的大小,会报索引值错误)
ArrayList ar = new ArrayList();
//选中的数据,最后需要用来与右侧数据判断是否是左侧数据
StringBuilder s = new StringBuilder();//将选中项放到临时变量
foreach (ListItem li in this.ListBox1.Items)
{
if (li.Selected)
{
s.Append(li.Value + ",");
ar.Add(li);
}
}//执行右添加,左删除选中的数据
foreach (ListItem li in ar)
{
this.ListBox2.Items.Add(li);//右添加
this.ListBox1.Items.Remove(li);//左删除
}if (ViewState["sR"].IsNotNullOrEmpty())
{
ViewState["sR"] = s.Append(ViewState["sR"]);
}
else
{
ViewState["sR"] = s;//右临时变量
}}
}/// <summary>
/// 右向左移动
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button2_Click(object sender, EventArgs e)
{
//保存在左临时变量中//向左移动
if (this.ListBox2.SelectedIndex > 0)
{
ArrayList al = new ArrayList();
StringBuilder s = new StringBuilder();
//移动到左侧
foreach (ListItem li in this.ListBox2.Items)
{
if (li.Selected)
{
s.Append(li.Value);
al.Add(li);
}
}//执行左添加,右删除选中的数据
foreach (ListItem li in al)
{
this.ListBox2.Items.Remove(li);//右删除
this.ListBox1.Items.Add(li);//左添加
}if (ViewState["sL"].IsNotNullOrEmpty())
{
ViewState["sL"] = s.Append(ViewState["sL"]);
}
else
{
ViewState["sL"] = s;//左临时变量
}}
}
/// <summary>
/// 修改組成員
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btn_Click(object sender, EventArgs e)
{
//处理左临时变量
GetLeftTemp();
//处理右临时变量
GetRightTemp();
}/// <summary>
/// 获取左临时变量与左侧数据比较
/// </summary>
private void GetLeftTemp()
{
if (ViewState["sL"].IsNotNullOrEmpty() && ViewState["dtL"].IsNotNullOrEmpty())
{
bool isOpen = true;//默认没有相同值
StringBuilder sEdit = new StringBuilder();//选中值的结果string sL = ViewState["sL"].ToString();
string[] sTemp = sL.Trim(',').Split(',');
DataTable dt = (DataTable)ViewState["dtL"];
foreach (string s in sTemp)
{
foreach (DataRow dr in dt.Rows)
{
int drTemp = dr["accountId"].ToInt();
int iTemp = s.ToInt();
if (iTemp == drTemp)//在这里判断如果选中的左临时变量 sL 与 dtL 相同那么..
{
isOpen = false;//有相同值
}
}
if (isOpen)//当没有相同值的时候存储要修改的ID
{
//执行这里的操作(与下面的获取右临时变量相同)
sEdit.Append(s + ",");
}
}
}
}/// <summary>
/// 获取右临时变量与右侧数据比较
/// </summary>
private void GetRightTemp()
{
if (ViewState["sR"].IsNotNullOrEmpty() && ViewState["dtR"].IsNotNullOrEmpty())
{
bool isOpen = true;
StringBuilder sEdit = new StringBuilder();string sR = ViewState["sR"].ToString();
string[] sTemp = sR.Trim(',').Split(',');
DataTable dt = (DataTable)ViewState["dtR"];
foreach (string s in sTemp)
{
foreach (DataRow dr in dt.Rows)
{
int iTemp = s.ToInt();
int drTemp = dr["accountid"].ToInt();
if (iTemp != drTemp)
{
string st = string.Empty;
}
else
{
isOpen = false;
}
}
if (isOpen)
{
sEdit.Append(s + ",");
}
}
}}
- 2个 listbox 互相调换数据(人员)
- java中List集合中任意两个位置的数据互相调换
- asp.net 2个ListBox之间转移数据的实现
- dataGridView数据调换位置
- ListBox的互相移动
- java编程之调换数据位置(从命令窗输入数据)
- java编程之调换数据位置(从对话窗体输入数据)
- WPF一个列表(ListBox)呈现多个数据集合
- 不用第三个参数调换整数a和b(算法)
- 快速排序(单边调换)
- 快速排序(双边调换)
- winform程序之人员选择listbox控件应用了一点arraylist(简单)
- sql语句(一)统计跟单位目录关联的人员信息表的多个数据
- ListBox的数据绑定
- C# ListBox数据互换
- ListBox绑定数据
- WPF ListBox数据绑定
- ComboBox/ListBox绑定数据
- 理解B树 B+树
- 小小车模,我儿子
- 如何继续
- ubuntu 下耳机插上后,扬声器仍有声音,耳机没有声音的情况。
- 纳兰容若的记忆
- 2个 listbox 互相调换数据(人员)
- jquery弹出层实例
- Python 类
- rpm Header V3 DSA signature: NOKEY 错误解决
- function trim() 问题
- 精确计算日期物理差(计算给定的两个日期间的物理年月日时分秒差)
- 新闻文本聚类
- 治疗室监控系统v1.0
- 关于ubuntu搜索不到无线网络。