I have a dropdownlist (ddlState) that is bound by a table (tblState). A second table (tblPreferences) is queried to find what the selected value of the dropdownlist should be. This works fine.
However, Ive been getting a "Object reference not set to an instance of an object" error when trying to set the selected value of the dropdownlist. Heres a code snippet.
' dropdownlist has already been bound
' strState contains the selected value (i.e. Michigan)DataReader.Read
dim strState as string
strState = dataReader( "LandState" )
ddlState.SelectedItem.Value = strState
Any ideas?
Thanks,
DamonJust figured it out:
ddlState.Selectedindex = ddlState.Items.Indexof (ddlState.Items.FindByValue(strState))
[C#]
ListItem liSelect = ddlState.FindByValue(strState);// if not found, then leave as is
if ( liSelect == null )
{
ddlState.SelectedItem.Selected = false; // could be "Select"
liSelect.Selected = true;
}[VB.NET]
Dim liSelect As ListItem = ddlState.FindByValue(strState)' if not found, leave as is
If Not liSelect Is Nothing Then
ddlState.SelectedItem.Selected = False
liSelect.Selected = True
End If
Pardon the intrusion, but I just have to mention this :-D
WithEasyListBox, you select an item this way:
myELB.SelectedValue = strState
(For C#, just slap a semicolon on there)
It'sjust as easy for multiple-select lists...
0 comments:
Post a Comment