2009-10-02 77 views
0

数据绑定列表项我有一个下拉列表中一个DetailsView的EditItemTemplate中被使用,并且它正在从一个SqlDataSource填充,而我如下结合所选值:启用在DropDownList中

<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> 

所有按预期工作。现在我想要做的只是基于sqlDataSource中的另一列启用那些绑定列表项 - 有一个“状态”列可以有活动或非活动的值 - 如果条目的状态是活动的,那么我希望启用相应的列表项,否则我希望它被禁用。原因是由于这是一个编辑表单,我不希望人们能够选择一个非活动的值,但我需要在下拉列表中包含这些“非活动”条目,因为主条目是正在编辑可能有一个位置ID现在不活动的位置。

我试图用在以下atted到DropDownList的定义:

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

但没有工作 - 但有没有报告任何错误。

有什么建议吗?

感谢

回答

1

不能在三夏内,类似的DetailsView数据控件中进行后期绑定的评价。

指定ItemDataBound上的值。看看这个相似的question

+0

感谢您的信息 - 它并没有真正帮助,我想我设法与具有涉及两个数据源混淆自己 - 一个细节来看,和其他来填充下拉列表中的列表项的成员。 我需要解决的是如何以某种方式为从数据源添加到下拉列表中的每个列表项捕获一个“onDataBound”。但是onDataBinding事件似乎没有公开我需要的值。 – 2009-10-05 16:33:09

+0

您仍然可以通过附加事件处理程序来执行数据采集任务,将detailsview中的下拉列表视为常规控件。 – 2009-10-06 01:03:32

0

嗯,我发现了两件事。首先,也是最重要的是,.Net不允许你禁用下拉列表控件中的listitems。第二个是,我真的不得不采取OKW的建议和使用的事件处理程序 - 我选择使用onbadabound事件:

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 

本来,行lstLocations.Items(参数nIndex)。文本& =“( Unavailable)“实际上将该listitem的”enabled“属性设置为false,但唯一的影响是将listitem从下拉列表中完全删除。