Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top