點擊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,不是文字,根據需要自己調

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM