点击listbox中的选项 直接在textbox中显示

当点击listbox中的一个选项时,在textbox中显示出来  这个需要改listbox的属性AutoPostBack为true

在listbox的Selectedindexchanged事件中添加代码

if(this.ListBox1 .SelectedItem !=null )
{

this.textbox.text=this.listbox1.selecteditem.text;

}

当有两个listbox,第一个为第二个的类型,第二个的内容显示到textbox

if (this.ListBox1.SelectedValue == "TeacherId")//teacherid为listbox1中选项的value
{
ListBox2.Items.Clear();   //先把listbox2clear一下  去除多余的内容

string strSQL = "SELECT TeacherId,TeacherName FROM teacher_infotable;";

DataSet ds = db.GetDataSet(strSQL);
DataTable dt = ds.Tables[0];
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
ListItem lt = new ListItem();
lt.Value = dt.Rows[i].ItemArray[0].ToString();
lt.Text = dt.Rows[i].ItemArray[1].ToString();

this.ListBox2.Items.Add(lt);
}
}
}

listbox2的事件代码

foreach (ListItem li in ListBox2.Items)
{
if (li.Selected)
{
this.txt_MessageClassIdentifier.Text += li.Value +",";
}
}

效果如下;这里显示是ID,不是文字,根据需要自己调

原文地址:https://www.cnblogs.com/wwr01/p/8974009.html