DropDownList两个SqlDataSource

时间:2012-05-22 19:18:38

标签: asp.net .net drop-down-menu sqldatasource

我在DetailsView中有一个DropDownList作为TemplateField。我想在DropDownList中填充所有可能的选项,这些选项将从一个SqlDataSource中提取,同时将DropDownList的选定值分配给来自DetailsView绑定的SqlDataSource的当前值。如下所示:

<asp:DetailsView DataSourceID="SqlDataSource1">
    <asp:TemplateField>
         <asp:DropDownList ID="Serial">
           /* Here is where I need to bind the dropdown to data source 2 so that All serials are available while selecting it's current value from data source 1.
    </asp:TemplateField>
</asp:DetailsView>

<asp:SqlDataSource ID="SqlDataSource1"
    SelectCommand = "Select SerialNumber From Equipment Where EquipID = @EquipID">
   <SelectParameters>
        <asp:ControlParameter ControlID="GridView1" DefaultValue=""
            Name="EquipID" PropertyName="SelectedDataKey.Values[EquipID]" />
   </SelectParameters>
</asp:SqlDataSource>

<asp:SqlDataSource ID="SqlDataSource2"
    SelectCommand = "Select SerialNumber From Equipment">
</asp:SqlDataSource>

1 个答案:

答案 0 :(得分:1)

您可以将DataSourceID的{​​{1}}分配给填充选项的数据源,然后将DropDownList分配给绑定到{{的数据源的当前值1}}:

SelectedValue

DetailsView来自绑定到<asp:DropDownList ID="Serial" DataSourceID="SqlDataSource#" DataValueField="..." DataTextField="..." SelectedValue='<%#Bind("MYFIELDNAME")%>'> </asp:DropDownList> 的数据源,表示该记录的当前值。