在asp上的Insert命令中将另一个DropdownList与另一个层叠在一起

时间:2013-12-12 16:39:14

标签: asp.net sql-server html-select sqldatasource

我在formview插入模板中有这个asp。

  <asp:FormView ID="FormViewReport" runat="server">
  <InsertItemTemplate>
     GAME_ID:
        <asp:DropDownList ID="DropDownListReportIns" runat="server" AutoPostBack="true"
            DataSourceID="SqlDataSourceGetGame"
            DataTextField="GAME" DataValueField="ID" SelectedValue='<%# Bind("GAME_ID") %>' AppendDataBoundItems="True">
            <asp:ListItem Text="ΠΑΡΑΚΑΛΩ ΕΠΙΛΕΞΤΕ ΑΓΩΝΑ" />
        </asp:DropDownList>
     HOME_PLAYER:
        <asp:DropDownList ID="DropDownListRepo" runat="server"
            DataSourceID="SqlDataSourceGetPlayers"
            DataTextField="HOME_PLAYER_S" DataValueField="HP_ID" SelectedValue='<%# Bind("HOME_PLAYER_ID") %>' >
        </asp:DropDownList>

第二个下拉列表的sql数据源:

      <asp:SqlDataSource ID="SqlDataSourceGetPlayers" runat="server" ConnectionString='<%$ ConnectionStrings:BasketballConnectionString1 %>' SelectCommand="SELECT HP.SURNAME  blah blah blah WHERE (GAMES.ID = @GAME_ID)">
    <SelectParameters>
        <asp:ControlParameter ControlID="FormViewReport" PropertyName="SelectedValue" Name="GAME_ID"></asp:ControlParameter>
    </SelectParameters>

在第二个Dropdownlist中,查询需要GAME_ID参数,这是第一个下拉列表的DatavalueField。如何从第一个下拉列表中获取所选值并将其提供给第二个下拉列表?

2 个答案:

答案 0 :(得分:2)

将您的SqlDatasource控件放入FormView的InsertItemTemplate(使用DropDownLists),然后将其更改为:

<asp:SqlDataSource ID="SqlDataSourceGetPlayers" runat="server" ConnectionString='<%$ ConnectionStrings:BasketballConnectionString1 %>' SelectCommand="SELECT HP.SURNAME  blah blah blah WHERE (GAMES.ID = @GAME_ID)">
    <SelectParameters>
        <asp:ControlParameter ControlID="DropDownListReportIns" PropertyName="SelectedValue" Name="GAME_ID"></asp:ControlParameter>
    </SelectParameters>
</asp:SqlDataSource

这样你的ControlParameter就会引用第一个下拉列表的选定值。

答案 1 :(得分:0)

最后,我使用asp作为第二个下拉列表:

 <asp:DropDownList ID="DropDownListRepIns" runat="server"    AppendDataBoundItems="True" EnableViewState="false" 
            DataSourceID="SqlDataSourceGetPlayers"
            DataTextField="HOME_PLAYER_S" DataValueField="HP_ID" >
                           <asp:ListItem Text="ΠΑΡΑΚΑΛΩ ΕΠΙΛΕΞΤΕ ΑΓΩΝΑ" Value="0" />
         </asp:DropDownList><br />

和用于手动绑定的事件项插入的c#代码:

       protected void FormViewReport_ItemInserting(object sender, FormViewInsertEventArgs e)
{
    e.Values["HOME_PLAYER_ID"] = ((DropDownList)((FormView)sender).FindControl("DropDownListRepIns")).SelectedValue;}

此解决方案的解决方案也可以在不需要代码的情况下运行:

  SelectedValue='<%# DataBinder.Eval (Container.DataItem, "HOME_PLAYER_ID") %>'

你必须在asp。

的第二个下拉列表中使用它