ListBox 问题,得不到选中值,求高手帮忙...

pconline 2010-11-19 01:53:41
if (!Page.IsPostBack)
{
SqlDataAdapter sda = new SqlDataAdapter();
DataSql li = new DataSql();
System.Data.DataTable dl = new System.Data.DataTable();
DataSet ds = new DataSet();
sqlli = "select class2_name,class1_name from class2";
sda = li.CreateDa(sqlli);
sda.Fill(ds, "class2");
this.ListBox3.DataTextField = "class2_name";
this.ListBox3.DataValueField = "class1_name";
this.ListBox3.DataSource = ds.Tables["class2"];
this.ListBox3.DataBind();
}

protected void Button3_Click(object sender, EventArgs e)
{
this.ListBox4.Items.Add(new ListItem(this.ListBox3.SelectedItem.Text.ToString(), this.ListBox3.SelectedItem.Value.ToString()));
}

我是想把ListBox3 中被选中的的项动态添加到 ListBox4 中.但是我次添加按钮.都只添加的是listbox3中的第一项.而不是我选中的项...请问.为什么呢......

...全文
220 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
pconline 2010-11-19
  • 打赏
  • 举报
回复
我再研究研究
kevin_520_kevin 2010-11-19
  • 打赏
  • 举报
回复
实在不行你把ADD方法换成insert方法试试看
kevin_520_kevin 2010-11-19
  • 打赏
  • 举报
回复
这你跟踪调试下看看吧,这代码感觉就是那样么
pconline 2010-11-19
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 bin_520_yan 的回复:]
C# code

private void Form1_Load(object sender, EventArgs e)
{
this.listBox1.Items.Add("111111111");
this.listBox1.Items.Add("22222222");
this.listBox……
[/Quote]



这个方法是可以的.但是我少得到一个值.. 我的目的就是只知道名字和ID..呵呵.
pconline 2010-11-19
  • 打赏
  • 举报
回复
唉...郁闷啊...

this.ListBox3.DataValueField = "class1_name"; 把他注释掉就没问题. 释放出来就不行.

我就奇了怪了.... 注释掉我就少得到一个值..下面的程序不好继续下去了..

郁闷.....
hookyzlr 2010-11-19
  • 打赏
  • 举报
回复
是在找不到问题所在
我本地测试没有问题。
你把绑定的代码
this.ListBox3.DataTextField = "class2_name";
this.ListBox3.DataValueField = "class1_name";
this.ListBox3.DataSource = ds.Tables["class2"];
this.ListBox3.DataBind();
改成手动循环添加试试?
DataBox-MDX 2010-11-19
  • 打赏
  • 举报
回复
上面是CS下的,
下面是BS下的:

protected void Page_Load(object sender, EventArgs e)
{
this.ListBox1.Items.Add("aa");
this.ListBox1.Items.Add("bb");
this.ListBox1.Items.Add("cc");
this.ListBox1.Items.Add("dd");
this.ListBox1.Items.Add("ee");
}
protected void Button1_Click(object sender, EventArgs e)
{
if (this.ListBox2.Items.IndexOf(this.ListBox1.SelectedItem) == -1)
{
this.ListBox2.Items.Add(this.ListBox1.SelectedItem);
}
}

注意要给每个listbox的属性SelectionMode设置为Multiple即可
pconline 2010-11-19
  • 打赏
  • 举报
回复
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="open_line.aspx.cs" Inherits="open_line" %>
<link rel="stylesheet" type="text/css" href="hncss.css" />
<html>

<head runat="server">
<title></title>
<style type="text/css">
body {
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
}


.tr1
{
background-color: #f7fbff;
}
.tr0
{
background-color: #FFFFFF;
}
</style>

<%--<script>
function radioClick(clientid)
{
var grd = document.getElementById( ' <=GridView1.ClientID%> ');
var arr = grd.getElementsByTagName( "RadioButton1");
for(var i = 0; i < arr.length; i++)
{
if(arr.type == "radio ")
{ arr.checked = false;}
}
document.getElementById(clientid).checked = true;
}

</script >--%>
<script language="javascript" type="text/javascript">
function CheckNum()
{
  var objRegex = /[\d\.]/ig; //数字的正则表达式
  if(String.fromCharCode(event.keyCode).match(objRegex) == null)
  {
    event.returnValue = 0;
  }
}
</script>
</head>
<body >
<form id="form1" runat="server">
<table width="100%" height="100%" border="0" cellspacing="0" cellpadding="0">
<tr>

<td width="93%" background="images/tiaob.jpg" class="style4" style="height: 52px">
<table width="123" border="0" cellspacing="0" cellpadding="0" background="images/tiaotou.jpg">
<tr>
<td height="40" class="style5" style="text-align: center">
<span style="font-size: 12pt; color: #ffffff">
<asp:Label ID="Label1" runat="server" Text="开放电路添加" Font-Size="10pt"></asp:Label></span></td>
</tr>
</table></td>
</tr>
<tr>
<td colspan="2" valign="top" class="style3" >
<br />
<table border="0" cellspacing="1" cellpadding="2" bgcolor="#00CCFF" align="center">
<tr>
<td bgcolor="#FFFFFF" style="height: 32px; text-align: center;" align="right" colspan="2" >
<span style="font-size: 14pt; color: #ff0000">开放电路添加</span></td>
</tr>
<tr>
<td bgcolor="#FFFFFF" style="width: 155px" align="right" >
用户名称: 
</td>
<td bgcolor="#FFFFFF" style="width: 418px"> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td bgcolor="#FFFFFF" style="width: 155px" align="right" >
电路名称: </td>
<td bgcolor="#FFFFFF" style="width: 418px"> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td bgcolor="#FFFFFF" style="width: 155px; height: 26px;" align="right" >
光缆名称: </td>
<td bgcolor="#FFFFFF" class="style11" style="width: 418px; height: 26px;"> <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox></td>
</tr>

<tr>
<td bgcolor="#FFFFFF" style="width: 155px; height: 28px;" align="right" >
局端名称:<td bgcolor="#FFFFFF" class="style11" style="height: 28px; width: 418px; text-align: center;">
<asp:ListBox ID="ListBox1" runat="server" Width="125px"></asp:ListBox>
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="添加>>" />
<asp:ListBox ID="ListBox2" runat="server" Width="125px"></asp:ListBox>
           
</td>
</tr>
<tr>
<td bgcolor="#FFFFFF" style="width: 155px; height: 29px;" align="right" >
配线架名称:</td>
<td bgcolor="#FFFFFF" class="style7" style="width: 418px; height: 29px; text-align: center;">
<asp:ListBox ID="ListBox3" runat="server" Width="125px"></asp:ListBox>
<asp:Button ID="Button3" runat="server" OnClick="Button3_Click" Text="添加>>" />
<asp:ListBox ID="ListBox4" runat="server" Width="125px"></asp:ListBox>
<asp:Button ID="Button9" runat="server" Text="关联>>" OnClick="Button9_Click" /></td>
</tr>
<tr>
<td bgcolor="#FFFFFF" style="width: 155px" align="right" >
交接箱名称:</td>
<td bgcolor="#FFFFFF" style="width: 418px; text-align: center;"><asp:ListBox ID="ListBox5" runat="server" Width="125px"></asp:ListBox>
<asp:Button ID="Button4" runat="server" OnClick="Button4_Click" Text="添加>>" />
<asp:ListBox ID="ListBox6" runat="server" Width="125px"></asp:ListBox>
<asp:Button ID="Button10" runat="server" Text="关联>>" OnClick="Button10_Click" /></td>
</tr>
<tr>
<td bgcolor="#FFFFFF" style="width: 155px" align="right">
接头盒名称:</td>
<td bgcolor="#FFFFFF" style="height: 20px; width: 418px; text-align: center;"><asp:ListBox ID="ListBox7" runat="server" Width="125px">
</asp:ListBox>
<asp:Button ID="Button5" runat="server" OnClick="Button5_Click" Text="添加>>" />
<asp:ListBox ID="ListBox8" runat="server" Width="125px"></asp:ListBox>
<asp:Button ID="Button11" runat="server" Text="关联>>" OnClick="Button11_Click" /></td>
</tr>
<tr>
<td bgcolor="#FFFFFF" style="width: 155px" align="right" > 终端盒名称:</td>
<td bgcolor="#FFFFFF" style="width: 418px; text-align: center;"><asp:ListBox ID="ListBox9" runat="server" Width="125px"></asp:ListBox>
<asp:Button ID="Button6" runat="server" OnClick="Button6_Click" Text="添加>>" />
<asp:ListBox ID="ListBox10" runat="server" Width="125px"></asp:ListBox>
<asp:Button ID="Button12" runat="server" Text="关联>>" OnClick="Button12_Click" /></td>
</tr>
<tr>
<td bgcolor="#FFFFFF" style="width: 155px" align="right" >
分光器名称:</td>
<td bgcolor="#FFFFFF" style="height: 22px; width: 418px; text-align: center;"><asp:ListBox ID="ListBox11" runat="server" Width="125px"></asp:ListBox>
<asp:Button ID="Button7" runat="server" OnClick="Button7_Click" Text="添加>>" />
<asp:ListBox ID="ListBox12" runat="server" Width="125px"></asp:ListBox>
<asp:Button ID="Button13" runat="server" Text="关联>>" OnClick="Button13_Click" /></td>
</tr>
<tr>
<td bgcolor="#FFFFFF" style="width: 155px" align="right" >
双纤/单纤: </td>
<td bgcolor="#FFFFFF" style="width: 418px">
 <asp:TextBox ID="TextBox10" runat="server"></asp:TextBox>
     <span style="color: #ff0000">注:请输入数字</span></td>
</tr>

<tr>
<td bgcolor="#FFFFFF" style="width: 155px" align="right" >
纤芯序号: </td>
<td bgcolor="#FFFFFF" style="width: 418px">
 <asp:TextBox ID="TextBox11" runat="server"></asp:TextBox>
     <span style="color: #ff0000">注:请输入数字</span></td>
</tr>
<tr>
<td bgcolor="#FFFFFF" style="width: 155px; height: 113px;" align="right" >备注:  </td>
<td bgcolor="#FFFFFF" style="width: 418px; height: 113px;" > <asp:TextBox ID="TextBox12" runat="server" Height="160px" Rows="5" TextMode="MultiLine"
Width="410px"></asp:TextBox></td>
</tr>
<tr>
<td colspan="2" bgcolor="#FFFFFF" class="style6" align="center">
 <asp:Button ID="Button1" runat="server" onclick="Button1_Click"
style="text-align: center" Text="添加信息" />
</td>
</tr>
</table></td>
</tr>
<tr>
<td height="30" colspan="2" background="images/buttom_back.jpg"><div align="center"><img src="images/bottom_z.jpg" width="252" height="40" /></div></td>
</tr>
</table>



</form>



</body>
</html>
DataBox-MDX 2010-11-19
  • 打赏
  • 举报
回复

private void Form1_Load(object sender, EventArgs e)
{
this.listBox1.Items.Add("111111111");
this.listBox1.Items.Add("22222222");
this.listBox1.Items.Add("333333333333");
this.listBox1.Items.Add("44444444444");
this.listBox1.Items.Add("55555555555");
this.listBox1.Items.Add("66666666666666");
}

private void button1_Click(object sender, EventArgs e)
{
if (this.listBox2.Items.IndexOf(this.listBox1.SelectedItem) == -1)//Listbox2中不存在添加
{
this.listBox2.Items.Add(this.listBox1.SelectedItem);
}

}

希望对Lz有用
hookyzlr 2010-11-19
  • 打赏
  • 举报
回复
我说的是所有的前台代码。
你的listbox外面有没有套updatepanel,如果套了的话,两个listbox是不是在同一个updatepanel里面。
还有有没有禁用viewstate?
koukoujiayi 2010-11-19
  • 打赏
  • 举报
回复
完全没问题哦!!
jiameixiawanli 2010-11-19
  • 打赏
  • 举报
回复
if (!Page.IsPostBack)
{
SqlDataAdapter sda = new SqlDataAdapter();
DataSql li = new DataSql();
System.Data.DataTable dl = new System.Data.DataTable();
DataSet ds = new DataSet();
sqlli = "select class2_name,class1_name from class2";
sda = li.CreateDa(sqlli);
sda.Fill(ds, "class2");
this.ListBox3.DataTextField = "class2_name";
this.ListBox3.DataValueField = "class1_name";
this.ListBox3.DataSource = ds.Tables["class2"];
this.ListBox3.DataBind();
}

protected void Button3_Click(object sender, EventArgs e)
{
this.ListBox4.Items.Add(new ListItem(this.ListBox3.SelectedValue.value.ToString(), this.ListBox3.SelectedItem.Value.ToString()));
}


楼主把this.ListBox3.SelectedValue.text ,改成this.ListBox3.SelectedValue.value
pconline 2010-11-19
  • 打赏
  • 举报
回复
<tr>
<td bgcolor="#FFFFFF" style="width: 155px; height: 29px;" align="right" >
配线架名称:</td>
<td bgcolor="#FFFFFF" class="style7" style="width: 418px; height: 29px; text-align: center;">
<asp:ListBox ID="ListBox3" runat="server" Width="125px"></asp:ListBox>
<asp:Button ID="Button3" runat="server" OnClick="Button3_Click" Text="添加>>" />
<asp:ListBox ID="ListBox4" runat="server" Width="125px"></asp:ListBox>
<asp:Button ID="Button9" runat="server" Text="关联>>" OnClick="Button9_Click" /></td>
</tr>




前台代码
guozhaoyou1 2010-11-19
  • 打赏
  • 举报
回复
对list4重新绑定一次
  • 打赏
  • 举报
回复

this.ListBox3.DataSource = ds.Tables["class2"];
this.ListBox3.DataTextField = "class2_name";
this.ListBox3.DataValueField = "class1_name";
this.ListBox3.DataBind();
hookyzlr 2010-11-19
  • 打赏
  • 举报
回复
纠结。没发现问题所在。
楼主吧前台代码全贴出来,给大伙看看
丰云 2010-11-19
  • 打赏
  • 举报
回复
是不是别的操作影响了这里的情况??
丰云 2010-11-19
  • 打赏
  • 举报
回复
貌似页面状态没有保留住......
pconline 2010-11-19
  • 打赏
  • 举报
回复
如果我把绑定的语句改一下. this.ListBox3.DataValueField = "class1_name"; 把这句注释掉..就可以得到...点添加的按钮就可以得到...只要加上这句就得不到.添加的时候就永远都是第一项而不是我选中的项......这个也太奇怪了啊...
Ny-6000 2010-11-19
  • 打赏
  • 举报
回复
事件执行顺序的问题了.

跟踪调试一下.
加载更多回复(1)

62,271

社区成员

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

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

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

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