.net中ListBox控件的使用,谢谢各位大侠啦
"gxdangjia"这位仁兄,精神可嘉,其实第一楼简单明了地把我的问题给解决了,但是现在又出现一个新的问题,如果我把第一个ListBox中的selecttiondmode属性从single改为multiple,这个时候第一个ListBox就允许多选,但是如何把每次多选的值都传给第二个ListBox呢?谢谢~~! 展开
using System.Collections;
//下面是程序实现部分
//先定义一个数组变量
ArrayList al=new ArrayList();
//把listbox1中选中的项存入数组变量al
for(int i=0;i<this.ListBox1.Items.count;i++)
{
if(this.ListBox1.Items[i].Selected){al.Add(this.ListBox1.Items[i].Text);}
}
//把al绑定到listbox2
this.ListBox2.DataSource=al;
this.ListBox2.DataBind();
listBox2.Items.Clear();
listBox2.Items.Add(listBox1.SelectedItem);
过年高兴失眠睡不着
看到了你的问题,想到了百度知道提交答案时的选择分类也是类似的这种情况
于是写了个c#代码 供你参考,用了半个多小时。自己也练练提高一下吧
百度用的什么技术不清楚
代码如下:
我用的是三层
数据库层:
public static DataTable dt( string sqlstr)
{
SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["listboxConnectionString"].ConnectionString);
SqlDataAdapter sqlada = new SqlDataAdapter(sqlstr,sqlcon);
DataTable dt = new DataTable();
try
{
sqlada.Fill(dt);
}
catch (Exception ex)
{
throw (ex);
}
finally
{
sqlada.Dispose();
sqlcon.Close;
}
return dt;
}
逻辑层代码
public static void yiji(ListBox lb)
{
DataTable dt=datahelp.dt("select distinct yiji from menu ");
lb.DataSource=dt;
lb.DataTextField=dt.Columns["yiji"].ToString();
lb.DataValueField=dt.Columns["yiji"].ToString();
lb.DataBind();
}
public static void erji(string stvalue,ListBox lb)
{
DataTable dt = datahelp.dt("select min(id) as id, erji from menu where yiji='"+stvalue+"'group by erji order by id ");
lb.DataSource = dt;
lb.DataTextField = dt.Columns["erji"].ToString();
lb.DataValueField = dt.Columns["erji"].ToString();
lb.DataBind();
}
public static void sanji(string stvalue, ListBox lb)
{
DataTable dt = datahelp.dt("select id, sanji from menu where erji='" + stvalue + "' order by id ");
if (dt.Rows[0]["sanji"].ToString() !="")
{
lb.Visible = true;
lb.DataSource = dt;
lb.DataTextField = dt.Columns["sanji"].ToString();
lb.DataValueField = dt.Columns["sanji"].ToString();
lb.DataBind();
}
else
{
lb.Visible = false;
}
}
网页后台代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dealhelp.yiji(ListBox1);
dealhelp.erji("电脑网络",ListBox2);
ListBox3.Visible = false;
}
}
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
ListBox3.Visible = false;
string st = ListBox1.SelectedValue;
dealhelp.erji(st,ListBox2);
}
protected void ListBox2_SelectedIndexChanged(object sender, EventArgs e)
{
string st = ListBox2.SelectedValue;
dealhelp.sanji(st,ListBox3);
}
网页里脱了三个listbox他们的id依次为listbox1,2,3.
数据库建表思想如图 你的问题加一个foreach循环就搞定了,再循环中判断选中项,选中就加,二楼的代码就可以实现。