listbox的问题

dengkz1 2006-10-26 11:46:10
从一个listbox1选中一条,点击一按钮就将listbox1的选中那条添加到listbox2中去

如listbox1里有三条记录,不管我选中哪条,添加后总是加到第一条的,也就是不是我选哪条就添加哪条

代码如下,
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class oa_my_share : System.Web.UI.Page
{
CnDB.DBConnection DB = new CnDB.DBConnection();
CnDB.Func Fun = new CnDB.Func();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind();
}
}
public void Bind()
{
DropDownList1.DataSource = DB.go("select * from dept1");
DropDownList1.DataTextField = "dept";
DropDownList1.DataValueField = "dept";
DropDownList1.DataBind();
ListBox1.DataSource = DB.go("select cname,dept from hr where dept='" + DropDownList1.SelectedValue + "'").Tables[0].DefaultView;
ListBox1.DataTextField = "cname";
ListBox1.DataValueField = "ename";
ListBox1.DataBind();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
ListBox1.Items.Clear();
ListBox1.DataSource = DB.go("select * from hr where dept='" + DropDownList1.SelectedValue + "'");
ListBox1.DataTextField = "cname";
ListBox1.DataValueField = "ename";
ListBox1.DataBind();
}
protected void Button2_Click(object sender, EventArgs e)
{
if (ListBox1.SelectedIndex > -1)
{
ListBox2.Items.Add(ListBox1.SelectedItem.Text);
}
}
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
...全文
128 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
dengkz1 2006-10-26
  • 打赏
  • 举报
回复
?
dengkz1 2006-10-26
  • 打赏
  • 举报
回复
现在知道

如果datalist1是从数据库读出来的就会不行

如果datalist1是从页面上手动加items进去的就OK

快晕了
冷月孤峰 2006-10-26
  • 打赏
  • 举报
回复
这样:
private void butGoOne_Click(object sender, System.EventArgs e)
{
if(ltboxAll.SelectedIndex>-1)
{
do
{
ltboxNow.Items.Add(ltboxAll.SelectedItem );
ltboxAll.Items .Remove(ltboxAll.SelectedItem);
}while(ltboxAll.SelectedIndex !=-1);
}
}

private void butBackOne_Click(object sender, System.EventArgs e)
{
if(ltboxNow.SelectedIndex>-1)
{
do
{
ltboxAll.Items.Add(ltboxNow.SelectedItem);
ltboxNow.Items.Remove(ltboxNow.SelectedItem);
}
while(ltboxNow.SelectedIndex!=-1);
}
}
dengkz1 2006-10-26
  • 打赏
  • 举报
回复
都不行
xingyaohua 2006-10-26
  • 打赏
  • 举报
回复
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++;
}
}
jc15271149 2006-10-26
  • 打赏
  • 举报
回复
js的
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<script language="JavaScript">
<!--
function addSrcToDestList() {
destList = window.document.forms[0].destList;
srcList = window.document.forms[0].srcList;
var len = destList.length;
for(var i = 0; i < srcList.length; i++) {
if ((srcList.options[i] != null) && (srcList.options[i].selected)) {

var found = false;
for(var count = 0; count < len; count++) {
if (destList.options[count] != null) {
if (srcList.options[i].text == destList.options[count].text) {
found = true;
break;
}
}
}
if (found != true) {
destList.options[len] = new Option(srcList.options[i].text);
len++;
}
}
}
}

function deleteFromDestList() {
var destList = window.document.forms[0].destList;
var len = destList.options.length;
for(var i = (len-1); i >= 0; i--) {
if ((destList.options[i] != null) && (destList.options[i].selected == true)) {
destList.options[i] = null;
}
}
}
// -->
</SCRIPT>
</head>
<body>
<center>
<form method="POST">
<table bgcolor="#FFFFCC">

<tr>
<td bgcolor="#FFFFCC" width="85">
<select size="6" name="srcList" multiple>
<option value="1">Item 1
<option value="2">Item 2
<option value="3">Item 3
<option value="4">Item 4
<option value="5">Item 5
<option value="6">Item 6
</select>
</td>
<td bgcolor="#FFFFCC" width="74" align="center">
<input type="button" value=" 增加到右边 " onClick="javascript:addSrcToDestList()">
<br><br>
<input type="button" value=" 从右边删除 " onclick="javascript:deleteFromDestList();">
</td>
<td bgcolor="#FFFFCC" width="69">
<select size="6" name="destList" multiple>
</select>
</td>
</tr>
</table>
</form>
</body>
</html>

62,266

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧