Forum Moderators: open

Message Too Old, No Replies

Dropdownlist - Default to value passed from a datalist

         

ASPhopeful

3:39 pm on Nov 8, 2004 (gmt 0)

10+ Year Member



Hi All,

I have a datalist (BookingInfoDL). Within that datalist I have created a DDL (BookingStatus) which has the following code:

<asp:DropDownList runat="server" id="BookingStatusDD">

<asp:ListItem Value="Booked" Text="Booked" />

<asp:ListItem Value="Cancelled" Text="Cancelled" />

<asp:ListItem Value="NoShow" Text="NoShow" />

</asp:DropDownList>

I am looking to extract the value from the BookingStatus field in the Datalist and have the DDL default to that value. Can someone please help. For all other fields I am using:

<%# Container.DataItem("SomeField") %>

to extract the data from the DataList.

Many Thanks.
Helen

TheNige

12:57 am on Nov 9, 2004 (gmt 0)

10+ Year Member



Try something like this in your OnPageLoad sub or after the datalist is filled:

BookingStatusDD.Items.FindByValue(Container.DataItem("SomeField").ToString).Selected = True

ASPhopeful

8:14 am on Nov 9, 2004 (gmt 0)

10+ Year Member



Thanks but I'm getting an "Object reference not set to an instance of an object." error message. I have changed ("SomeField") to the value in the datalist which is ("BookingStatus").

Thanks
Helen.

tomasz

4:03 pm on Nov 10, 2004 (gmt 0)

10+ Year Member



i use this function

Public Sub SetComboByValue(ByVal ctl As DropDownList, ByVal sValue As String)
If sValue <> "" Then

Dim i As Integer
For i = 0 To ctl.Items.Count - 1
If ctl.Items(i).Value.ToLower = sValue.ToLower Then
ctl.SelectedIndex = i
Exit Sub
End If
Next
End If
End Sub