本文介绍了列表框onindexchanged事件触发但是selecteditem属性为null ASP.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当ListBox1中选择了某些内容时,selectedindexchanged事件会触发,但listbox1.selecteditem / index / value为null。如何获取SelectedItem?



When something is selected in the ListBox1 the selectedindexchanged event fires however listbox1.selecteditem/index/value is null. How can I get the SelectedItem ?

   protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
   {
       var row = ListBox1.SelectedItem.Text;
       var date = DateTime.ParseExact(row,"yyyy-MMMM", CultureInfo.InvariantCulture);
       ChangeMonthViewOnGridView1(date.Year, date.Month);
   }


<asp:ListBox ID="ListBox1" runat="server"
OnSelectedIndexChanged="ListBox1_SelectedIndexChanged" Width="258px"
AutoPostBack="True" ></asp:ListBox>





我的尝试:



使用断点进行调试。我看不出有任何变化迹象。尝试使用下拉列表,选择项目仍然是第一个。



What I have tried:

using breaking point for debugging. I can't see no trace of signs of the change. tried with dropdownlist too there the selecteditem remains the first one.

推荐答案

<asp:ListBox ID="ListBox1" runat="server"
OnSelectedIndexChanged="ListBox1_SelectedIndexChanged" Width="258px"
AutoPostBack="True" ></asp:ListBox>





with





with

<asp:ListBox ID="ListBox1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ListBox1_SelectedIndexChanged">
                <asp:ListItem>2017-January</asp:ListItem>
                <asp:ListItem>2017-February</asp:ListItem>
                </asp:ListBox>







并尝试。这适用于你的代码




and try. this is working for your code behind


这篇关于列表框onindexchanged事件触发但是selecteditem属性为null ASP.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 01:29