简体   繁体   English

如何从asp.net的列表框中获取所选项目?

[英]How to get selected item from listbox in asp.net?

I have to 2 listboxes in my application and i cant get selected item from the first listbox. 我在应用程序中必须有2个列表框,但我无法从第一个列表框中选择项目。 It says null refference error. 它说空引用错误。

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);
    }
}

Problem with your code is you are re-binding the ListBox on every postback. 您的代码存在问题是您在每次回发时都重新绑定了ListBox So when you click button btnPoisci a postback happends which rebinds the listbox and when btnPoisci_Click method is called there is no selected item thus the below line will throw NRE:- 因此,当您单击按钮btnPoisci时, btnPoisci了回发,该重新绑定了列表框,并且当btnPoisci_Click方法时,没有选定的项,因此下面的行将引发NRE:

 string a = ListBox1.SelectedItem.ToString();

Don't add items in ListBox after every postback. 每次回发后不要在ListBox中添加项目。 Check if it is initial get request on PageLoad using IsPostBack property and only add it once:- 使用IsPostBack属性检查它是否是对PageLoad的初始获取请求,并且仅将其添加一次:

protected void Page_Load(object sender, EventArgs e)
{
  if(!IsPostBack)
  {
    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);
    }
  }
}

You are clearing the items in the list on each page load which means you are clearing out the list before your event fires. 您正在清除每次页面加载时列表中的项目,这意味着您将在事件触发之前清除列表。 When you do that you clear the state of the list. 当您这样做时,您将清除列表的状态。 Try loading Items on Page init or check to see if it IsPostBack. 尝试在Page init上加载项目,或检查它是否为IsPostBack。

You must only load the values in your ListBox1 if it is not a postback. 如果不是回发,则只能在ListBox1中加载值。 Right now your first line of code in your page load event is clearing everything and refreshing. 现在,页面加载事件中的第一行代码正在清除所有内容并刷新。 Do this: 做这个:

if (!IsPostBack) {
    ListBox1.Items.Clear();
    List<Lekarna> lekarne = service.pridobiLekarne().ToList();
}

This will only load the ListBox1 on the first time to the page, keeping the value of any selected item on subsequent postbacks. 这只会在第一次将ListBox1加载到页面时,将任何选定项的值保留在后续的回发中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM