我必须在我的应用程序中的2个列表框,我不能从第一个列表框中选择项目。它说null refference错误。如何从asp.net中的列表框中获取选定的项目?
protected void Page_Load(object sender, EventArgs e)
{
ListBox1.Items.Clear();
List<Lekarna> lekarne = service.pridobiLekarne().ToList();
foreach (Lekarna a in lekarne)
{
ListBox1.Items.Add(a.ID + " | " + a.imeLekarne + " | " + a.Kraj + " | " + a.Država + Environment.NewLine);
}
}
protected void btnPoisci_Click(object sender, EventArgs e)
{
string a = ListBox1.SelectedItem.ToString();
int c1 = int.Parse(a[0].ToString());
List<Zdravilo> zdravila = service.vrniVsaZdravilaGledeNaLekarno(c1).ToList();
ListBox2.Items.Clear();
foreach (Zdravilo b in zdravila)
{
ListBox2.Items.Add(b.ID + " | " + b.imeZdravila + " | " + b.letoIzdaje + " | " + b.proizvajalec + Environment.NewLine);
}
}
你确定service.pridobiLekarne()。ToList();至少包含1个项目? –
因为'SelectedItem'是'null'。你确定有一个项目被选中?您发布的代码中的哪一行是抛出异常。 –