listbox的问题
从一个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)
{
}
}