Asp.net使用c#Listbox [英] Asp.net using c# Listbox
本文介绍了Asp.net使用c#Listbox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嘿伙计们,我试图从列表框中获取项目的ID并将其添加到数据库中。有谁知道我怎么能去做,我不想要文本。我已经尝试过selectedItem和selectedValue,但它们没有用。我从数据库中获取列表框项目
Hey guys, im trying to get the ID of an item from the listbox and add it on to the database. does anyone know how can i go about it, i dont want the text. Ive tried selectedItem, and selectedValue but they not working. I get my listbox items from the database
推荐答案
您好,
首先确保您的绑定列表以这种方式框。
Hi,
First make sure your are binding list box in this way.
DataTable dtName = new DataTable();
dtName.Columns.Add(new DataColumn("DisplayMember"));
dtName.Columns.Add(new DataColumn("ValueMember"));
dtName.Rows.Add("Suits", "0");
dtName.Rows.Add("Harvey Spector", "1");
dtName.Rows.Add("Jessica Pearson", "2");
dtName.Rows.Add("Mike Ross", "3");
dtName.Rows.Add("Donna Paulson", "4");
dtName.Rows.Add("Rachel", "5");
Listbox1.DataSource = dtName;
Listbox1.DataTextField = "DisplayMember";//Name of Column
Listbox1.DataValueField = "ValueMember";//Name of Column
Listbox1.DataBind();
和页面加载你没有重新绑定列表框控件,即将数据绑定代码放在if(!ispostback)块中。
,只需致电
and on page load you are not rebinding the listbox control i.e place databinding code in if(!ispostback) block.
and simply call
ListItem item = Listbox1.SelectedItem;
string selectedText = item.Text;
string selectedValue = item.Value;
希望这会有所帮助!
Hope this helps!.
只需选择即可价值
Simply You can do to get selected value
foreach (ListItem item in ListBox1.Items)
{
if (item.Selected)
{
string value = item.Value.ToString();
}
}
Asp.Net列表框操作....... Asp.Net ListBox
Patter
Asp.Net listbox operations ....... Asp.Net ListBox
Patter
这篇关于Asp.net使用c#Listbox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文