odd list box behaviour

  • Thread starter Thread starter KavvY
  • Start date Start date
K

KavvY

Hi,

In the code below the contents of the "TextField" column is correctly
displayed (DataTextField) in the List Box.

However on firing a SelectedIndexChanged event to get the value, the
DataValueField references a null object.

lbxResults.DataSource = sel.getDataTable();
lbxResults.DataTextField = "TextField";
lbxResults.DataValueField = "TextField"; //"Complaint_number";
lbxResults.DataBind();

Is there some hidden property of lbxResults (a list box) which I am suppose
to set to activate the value field? I have exactly the same code in another
page which works correctly.

for arguments sake here is the event code,

private void lbxResults_SelectedIndexChanged(object sender, System.EventArgs
e)
{
lblOutput.Text = lbxResults.SelectedItem.Value;
}

Thanks!
Rich.
 
KavvY said:
Hi,

In the code below the contents of the "TextField" column is correctly
displayed (DataTextField) in the List Box.

However on firing a SelectedIndexChanged event to get the value, the
DataValueField references a null object.

lbxResults.DataSource = sel.getDataTable();
lbxResults.DataTextField = "TextField";
lbxResults.DataValueField = "TextField"; //"Complaint_number";
lbxResults.DataBind();

Is there some hidden property of lbxResults (a list box) which I am suppose
to set to activate the value field? I have exactly the same code in another
page which works correctly.

for arguments sake here is the event code,

private void lbxResults_SelectedIndexChanged(object sender, System.EventArgs
e)
{
lblOutput.Text = lbxResults.SelectedItem.Value;
}

Thanks!
Rich.

For the record, if anyone else falls over on this kind of thing.

Make sure any code to populate list boxes etc is inside an "if
(!isPostBack)" condition, else it just resets after you click an item in the
list and resets the DataValueField before processing it. (i think)
 
Back
Top