Results 1 to 2 of 2

Thread: ListBox

  1. #1
    Join Date
    Dec 1969

    Default ListBox

    I have a listbox that I&#039;m populating programatically with Items from my db on page load. I have a button which onclick, references the selected item in this listbox. <BR><BR>I started having the following problem:<BR><BR>Every time I click on the button, the list of items in the lisbox grows by the number of items I&#039;m pulling from the db. This is because for every value I pull from the db, I create a new list item to add to the list.<BR><BR>I&#039;ve tried to solve this problem by executing the following code before populating the list on page load:<BR><BR>ListBox1.Items.Clear();<BR><BR>This unfortunately causes the following error:<BR><BR>&#039;Object reference not set to an instance of an object.&#039;<BR><BR>This happens in the button click event in the line of code that&#039;s trying to get the selected value from the listbox.<BR><BR>I don&#039;t understand why this is happening. Right after I call ListBox1.Items.Clear();, I run the code that populates the list with values from the db. Any takers?<BR>

  2. #2
    Join Date
    Dec 1969

    Default RE: ListBox

    It sounds like you may want to only populate your listbox on the page&#039;s initial load. For example in C#:<BR><BR>if(!Page.IsPostBack)<BR>{<BR> // populate your listbox<BR>}<BR><BR>It sounds like the control is getting populated with new data and the data saved in ViewState as well.<BR><BR>Jeff Widgren<BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts