Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with ListBox Control

Status
Not open for further replies.

bunnyweb

MIS
Jan 13, 2003
42
IE
Hi. I have just started transitioning from ASP to ASP.NET and I have run into a problem I need help with.


I have a listbox control on my .aspx page:

<ASP:LISTBOX ID="Sites" RUNAT="server" DATAVALUEFIELD="SiteID" DATATEXTFIELD="SiteName" ROWS="1">

This works fine. My listbox populates from my database.


I want to get the value of the selected item in my listbox. So in my aspx.vb file in the submit subroutine I have the line:

Dim SiteIDStr As String = Sites.SelectedItem.Value


When I submit my form I keep getting this error:

System.NullReferenceException: Object reference not set to an instance of an object.


I have tried many variations of the line giving me errors, but nothing I tried has worked. Can someone please shed some light on this?

Many many thanks in advance!
 
Are you sure that you have an item selected in the listbox when you all the submit code?
 
Well, my list box is literally just a list of sites. If you were to look at the rendered HTML it would look like:

<select name="Sites" size="1" id="Sites">
<option value="1">LONDON</option>
<option value="2">DUBLIN</option>
<option value="3">MADRID</option>
<option value="4">PARIS</option>
</select>

The value I'm trying to capture is the "value=".

If all my options have a value then isn't one always going to be selected?
 
If all my options have a value then isn't one always going to be selected?
No. You have to actually select an entry from the ListBox for one to be selected (or you could default one of the entries to be selected).


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Ok, how do I default one one my entries?

I currently have this:

adapter.SelectCommand = cmd
adapter.Fill(dataSet)

' Bind the data to the site drop down list
Sites.DataSource = dataSet
Sites.DataBind()
 
Try using the SelectedIndex method.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top