views:

349

answers:

4

I'm trying to use the selected value of a ListBox control to populate a TextBox with its Text property, and a HiddenField with its value property. It sounds simple enough, and I went with this:

currentGroupTextBox.Text = currentSiteGroupList.SelectedItem.Text;
currentGroupHiddenField.Value = currentSiteGroupList.SelectedValue;

But upon execution, ASP.NET returns an error:

Object reference not set to an instance of an object.

And highlights the first line. currentGroupTextBox and currentGroupHiddenField are two controls which are enabled in the ASPX file so I'm not too sure why ASP.NET would complain about instancing them.

+1  A: 

I'd be willing to bet that your first line is choking on referencing currentSiteGroupList.SelectedItem, as that seems the most likely candidate for being a null reference. Make sure your code is executing in the right place within the ASP.NET page lifecycle, such that the SelectedItem is set properly behind the scenes.

Adam Maras
Wouldn't SelectedItem be set by selecting an item in the ListBox? At the moment the code is executed by clicking a button and the list is populated in Page_Load().
Whistle
+1  A: 

SelectedItem from your currentSiteGroupList.SelectedItem is likely to be null (what represents no selection). You need to test it before assigning it to currentGroupTextBox.Text

Ciwee
How to make currentSiteGroupList.SelectedItem not null, if not by selecting something in the list? The SelectionMode is set to Single, so I would think that even in the case of not actively clicking in the ListBox the default (first) item would at least set the property initially?
Whistle
Also, trying to test currentSiteGroupList.SelectedItem returns the same error because the object is still not instanced.
Whistle
@Furism: SelectedItem property will be null unless you manually select an item on the list or programatically set the SelectedItem/SelectedItem property. You can't set these properties at design-time because they are marked as [Browsable(false)]
Ciwee
Sorry, this is really what I meant. Even if an item is manually selected, the problem will occur.
Whistle
A: 

Is it currentGroupTextBox or currentGroupTextBox that is null? In debug if stop on that line..is it one or both that are not existing? A common issue I find is that controls are placed on an asp formview or similar and so the reference to that control is not actually its id/name, but more likely you need to do;

TextBox myTextBoxReference = (TextBox) formName.FindControl("currentGroupTextBox")
string theValue = myTextBoxReference.Text

Another often issue is the page life cycle. So if your object is not on a form but maybe you are referencing it before it exists in the postback. hth

wortho
+1  A: 

I'm going to try and pull together answers to all of your questions, including those in the comments.

  1. Even if SelectionMode="Single", the listbox starts out without anything being selected, unless you specify which item should be selected in your code.

  2. To test if the SelectedItem is null, use the following code:

if (currentGroupSiteList.SelectedItem != null) {
    currentGroupTextBox.Text = currentSiteGroupList.SelectedItem.Text;
    currentGroupHiddenField.Value = currentSiteGroupList.SelectedValue;
}
  1. What does your Page_Load code that loads the listbox look like? Is it wrapped with a if (!Page.IsPostBack) check? If not, pressing the button and initiating a postback will reload the listbox, thus losing the SelectedItem that the user selected.
Jason Berkan
That was is. The list was "reloaded" in each Page_Load. What would be the most elegant way to do? I still need the page to be loaded when the page is displayed, but I need to make sure that the SelectedItem is not "reset" at the same time.
Whistle
Assuming the view state is active, you don't need to load the listbox on every postback. All you need to do is put if (!Page.IsPostPack) prior to your listbox loading code. (Actually, it is a good practice to put all your Page_Load code inside the postback check.
Jason Berkan