本文介绍了对象引用未设置为列表框的selecteditem上的对象错误的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我在asp.net C#网站上第一次使用列表框。我希望用户能够从列表框中选择多个项目,稍后在按钮点击事件中,所选项目将显示在标签中。 我在google上搜索了很多..遇到了很多解决方案,但没有他们为我工作。 我不知道为什么mi无处不在得到这个奇怪的错误。 我的aspx代码: I am using listbox for the first time in my asp.net C# website. I want the user to be able to select multiple items from the listbox and later on the button click event the selected items will be displayed in the label.I have searched alot on google.. came across many solutions bt non of them worked for me.I dnt know why m i getting this weird error out of nowhere.my aspx code:<asp:ListBox ID="ListBox1" SelectionMode="Multiple" AutoPostBack="true" DataTextField="area" DataValueField="area" runat="server" Height="139px" Width="239px" onselectedindexchanged="ListBox1_SelectedIndexChanged"> <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" /> You Choosed<br /> <asp:Label ID="Label1" runat="server"> 我的代码在aspx.cs后面代码: my code behind aspx.cs code:protected void Page_Load(object sender, EventArgs e) { bindlist(); if (!IsPostBack) { bindlist(); } } protected void bindlist() { SqlConnection conn = new SqlConnection(); conn.ConnectionString = "Data Source=SARU-PC\\SQLEXPRESS;Initial Catalog=jd;Integrated Security=True"; SqlDataAdapter adp = new SqlDataAdapter("select area from area", conn); DataSet ds = new DataSet(); adp.Fill(ds); ListBox1.DataSource = ds; ListBox1.DataBind(); } protected void Button1_Click(object sender, EventArgs e) { ListItem item = ListBox1.SelectedItem; string a = item.Text; Label1.Text = a; //for (int i = ListBox1.Items.Count - 1; i >= 0; i--) //{ // if (ListBox1.Items[i].Selected == true) // { // Label1.Text += ListBox1.Items[i].ToString(); // } //} //ListItem item = ListBox1.SelectedItem; //string a = item.ToString(); //Label1.Text = a; //Label1.Text = ""; //foreach (ListItem li in ListBox1.Items) //{ // if (li.Selected) // { // Label1.Text += li.ToString() + "<br />"; // } //} } protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e) { Label1.Text = ""; string a = ListBox1.SelectedItem.ToString(); Label1.Text += a + "<br />"; } 1)我在页面加载事件中从数据库加载了我的列表框以后我用现有代码替换为了以防万一。 2)我尝试在按钮点击标签bt后获取所选项目我甚至尝试在selectedindexchanged事件上获取它。它仍然无法工作。 3)已经尝试了在标签bt中分配所选项目列表框的方法都是徒劳的。 我是在我将listbox1.selecteditem分配给字符串或标签本身的行上获取对象引用未设置为对象的实例错误。 请帮助!!!! 1) I loaded my listbox from database in page load event later i replaced with existing code just in case.2) I tried getting selected items on button click in label bt later i even tried getting it on selectedindexchanged event. it still doesnt work.3) have tried diff ways of assigning selected items of listbox in label bt all in vain.I am getting the"Object reference not set to an instance of an object" error on the line where i assign "listbox1.selecteditem" to a string or to the label itself.Please help!!!!推荐答案 <asp:listbox id="ListBox1" selectionmode="Multiple" autopostback="true" datatextfield="area" >DataValueField="area" runat="server" Height="139px" Width="239px"></asp:listbox> b $ b protected void Page_Load(object sender, EventArgs e){//dont call bindlist hereif (!IsPostBack){bindlist();}}protected void Button1_Click(object sender, EventArgs e){foreach (ListItem li in ListBox1.Items){ if (li.Selected) { Label1.Text += li.ToString() + "; }}} 这篇关于对象引用未设置为列表框的selecteditem上的对象错误的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-27 15:34