我页面上有两个listbox。我想实现的功能是,如果数据库有符合条的记录就将两个listbox进行填充。否则将某个表的数据全部填充到一个,然后实现两者之间双击互选。一个增加另一个必须减少。
我的代码:
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;
using DbService;public partial class AdminManage_test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string strTable = "Duties";
string[] strColumn = new String[2];
strColumn[0] = "DutyId";
strColumn[1] = "DutyName";
DataSet myDS = new DataSet();
try
{
myDS = DbAccess.SelectAll(strTable, strColumn);
ListBox2.DataSource = myDS.Tables[0].DefaultView;
ListBox2.DataValueField = myDS.Tables[0].Columns["DutyId"].ToString();
ListBox2.DataTextField = myDS.Tables[0].Columns["DutyName"].ToString();
ListBox2.DataBind();
}
catch (Exception k)
{
k.ToString();
}
}
}
protected void ListBox2_SelectedIndexChanged(object sender, EventArgs e)
{
if (ListBox2.SelectedIndex != -1)
{ ListBox1.Items.Add(new ListItem(ListBox2.SelectedItem.Value));
ListBox1.Items.Add(new ListItem(ListBox2.SelectedItem.Text));
ListBox2.Items.Remove(ListBox2.SelectedItem.Value);
ListBox2.Items.Remove(ListBox2.SelectedItem.Text);
}
}
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (ListBox1.SelectedIndex != -1)
{
ListBox2.Items.Add(new ListItem(ListBox1.SelectedItem.Value));
ListBox1.Items.Remove(ListBox1.SelectedItem.Value);
} }
}
问题是,怎么将value加进去却不显示(要使用valuez),两一个问题,双击后一个增加另一个由于刷新重载如数据库提取的数据却不减少。请大家帮忙看如何实现
我的代码:
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;
using DbService;public partial class AdminManage_test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string strTable = "Duties";
string[] strColumn = new String[2];
strColumn[0] = "DutyId";
strColumn[1] = "DutyName";
DataSet myDS = new DataSet();
try
{
myDS = DbAccess.SelectAll(strTable, strColumn);
ListBox2.DataSource = myDS.Tables[0].DefaultView;
ListBox2.DataValueField = myDS.Tables[0].Columns["DutyId"].ToString();
ListBox2.DataTextField = myDS.Tables[0].Columns["DutyName"].ToString();
ListBox2.DataBind();
}
catch (Exception k)
{
k.ToString();
}
}
}
protected void ListBox2_SelectedIndexChanged(object sender, EventArgs e)
{
if (ListBox2.SelectedIndex != -1)
{ ListBox1.Items.Add(new ListItem(ListBox2.SelectedItem.Value));
ListBox1.Items.Add(new ListItem(ListBox2.SelectedItem.Text));
ListBox2.Items.Remove(ListBox2.SelectedItem.Value);
ListBox2.Items.Remove(ListBox2.SelectedItem.Text);
}
}
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (ListBox1.SelectedIndex != -1)
{
ListBox2.Items.Add(new ListItem(ListBox1.SelectedItem.Value));
ListBox1.Items.Remove(ListBox1.SelectedItem.Value);
} }
}
问题是,怎么将value加进去却不显示(要使用valuez),两一个问题,双击后一个增加另一个由于刷新重载如数据库提取的数据却不减少。请大家帮忙看如何实现
关键是怎么让value不显示,另一格问题是,因为我在if(!ispostback)中帮定了数据。在邦定了数据的控件内双击后,页面有载入了邦定的数据,所以此控件内容并没有被remove掉
{
if (ListBox2.SelectedIndex != -1)
{ ListBox1.Items.Add(new ListItem(ListBox2.SelectedItem.Text,ListBox2.SelectedItem.Value));
ListBox2.Items.Remove(ListBox2.SelectedItem.Value);
ListBox2.Items.Remove(ListBox2.SelectedItem.Text);
}
}
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (ListBox1.SelectedIndex != -1)
{
ListBox2.Items.Add(new ListItem( ListBox1.SelectedItem.Text, ListBox1.SelectedItem.Value));
ListBox1.Items.Remove(ListBox1.SelectedItem.Value);
ListBox1.Items.Remove(ListBox1.SelectedItem.Text);
} }这样还是不对,添加成功,但是不能同时移出选择的listbox的项