T
Tony Z
I am trying to bind a DropDownList to a dataset and (basically at the same
time) set the selected index of the list base on the value of a column from
another dataset.
The first problem I have had to overcome was getting the index of the item I
want. I wrote following routine to do that:
Function findIndex(ByRef droplist As DropDownList, ByVal value As String) As
Integer
Dim li As ListItem
Dim i As Integer
li = droplist.Items.FindByText(value)
For i = 0 To droplist.Items.Count - 1
If li.Equals(droplist.Items(i)) Then
Return i
End If
Next i
Return -1
End Function
I use it in the ASP code like this:
<asp:dropdownlist id=PaymentTypeDrop runat="server" Width="128px"
EnableViewState="False" DataSource="<%# PaymentTypeDS1 %>"
DataTextField="Payment Type" SelectedIndex='<%# findIndex(PaymentTypeDrop,
DataBinder.Eval(objpayDS, "Tables[Payments].DefaultView.[0].PaymentType"))
%>' DataMember="Payment Type">
</asp:dropdownlist>
Now when I set a breakpoint at the Return -1 line, I discover that the
DropDownList has yet to be populated. I therefore have to assume that the
binding to fill the DropDownList has not yet taken place.
How can I accomplish my goal?
time) set the selected index of the list base on the value of a column from
another dataset.
The first problem I have had to overcome was getting the index of the item I
want. I wrote following routine to do that:
Function findIndex(ByRef droplist As DropDownList, ByVal value As String) As
Integer
Dim li As ListItem
Dim i As Integer
li = droplist.Items.FindByText(value)
For i = 0 To droplist.Items.Count - 1
If li.Equals(droplist.Items(i)) Then
Return i
End If
Next i
Return -1
End Function
I use it in the ASP code like this:
<asp:dropdownlist id=PaymentTypeDrop runat="server" Width="128px"
EnableViewState="False" DataSource="<%# PaymentTypeDS1 %>"
DataTextField="Payment Type" SelectedIndex='<%# findIndex(PaymentTypeDrop,
DataBinder.Eval(objpayDS, "Tables[Payments].DefaultView.[0].PaymentType"))
%>' DataMember="Payment Type">
</asp:dropdownlist>
Now when I set a breakpoint at the Return -1 line, I discover that the
DropDownList has yet to be populated. I therefore have to assume that the
binding to fill the DropDownList has not yet taken place.
How can I accomplish my goal?