views:

158

answers:

2

I have a drop down list that is being used in an EditItemTemplate of a DetailsView, and it is being populated from a SqlDataSource, and I am binding the selected value as follows:

<EditItemTemplate>
    <asp:DropDownList ID="lstLocations" runat="server" 
         DataSourceID="sqlMDALocationsAll" DataTextField="loc_name" DataValueField="id" 
         SelectedValue='<%# Bind("staff_location_id") %>' AppendDataBoundItems="True" >
         <asp:ListItem Value="">(Unknown)</asp:ListItem>
    </asp:DropDownList>
</EditItemTemplate>

All works as expected. Now what I want to do is enable only those bound list items based on another column in the sqlDataSource - there is a "status" column that can have values of either active or inactive - and if the status of an entry is active, then I want the corresponding list item to be enabled, otherwise I want it to be disabled. The reason is that since this is an edit form, I don't want people to be able to select a value that is inactive, but I need to include those "inactive" entries in the drop down list, since the main entry that is being edited could well have a location id for a location that is now inactive.

What I tried to use was the following atted to the DropDownList definition:

Enabled='<%# Eval("status") = "active" %>'

But that didn't work - but there weren't any errors reported.

Any suggestions?

Thanks

+1  A: 

You cannot perform a late-bound evaluation inside a webcontrol and within a data control like DetailsView.

Assign the value on ItemDataBound. Check out this similar question.

o.k.w
Thanks for that information - it hasn't really helped, and I think I managed to confuse myself with having two data sources involved - one for the details view, and another to populate the list item members of the drop down list.What I need to work out is how can I somehow trap an "onDataBound" for each list item that is being added to the drop down list from it's data source. But the onDataBinding event doesn't appear to expose the values that I need.
Ken Ray
You can still treat the dropdownlist within the detailsview as a normal control by attaching event handlers to perform databing tasks.
o.k.w
A: 

Well, I discovered two things. The first, and most important, is that .Net doesn't allow you to disable listitems in a dropdownlist control. The second is that I really had to take o.k.w.'s suggestion and use an event handler - I chose to use an onbadabound event:

    Protected Sub lstLocations_DataBound(ByVal sender As Object, ByVal e As System.EventArgs)
    'Once the drop down list has been populated, we want to disable any locations whose status is inactive.
    Dim lstLocations As DropDownList = CType(Me.dgStaff.FindControl("lstLocations"), DropDownList)
    If Not lstLocations Is Nothing Then
        Dim dView As System.Data.DataView = CType(Me.sqlMDALocationsAll.Select(DataSourceSelectArguments.Empty), System.Data.DataView)
        If Not dView Is Nothing Then
            dView.Sort = "id"
            For nIndex As Integer = 0 To lstLocations.Items.Count - 1
                If lstLocations.Items(nIndex).Value <> "" Then
                    Dim rowIndex As Integer = dView.Find(CInt(lstLocations.Items(nIndex).Value))
                    Trace.Write("lstLocations_DataBound", "Location ID = " & lstLocations.Items(nIndex).Value & " Name = " & dView(rowIndex)("loc_name") & " Status = " & dView(rowIndex)("status"))
                    If dView(rowIndex)("status").ToString.ToLower.Trim = "inactive" Then
                        lstLocations.Items(nIndex).Text &= " (Unavailable)"
                    End If
                End If
            Next
        End If
    Else
        Trace.Write("lstLocations_DataBound", "FindControl failed")
    End If
End Sub

Originally, the line lstLocations.Items(nIndex).Text &= " (Unavailable)" actually set the "enabled" property of that listitem to false, but the only effect of that was dropping the listitem completely from the dropdown list.

Ken Ray