2013-12-12 21 views
1

我有這個asp在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參數,這是第一dropdownlist.How我可以從獲得的選定值的DatavalueField第一個下拉列表並將其提供給第二個下拉列表?

回答

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

和交流的活動項目插入#代碼手動綁定:

 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") %>' 

你必須在第二個dropdownlist在asp中。

2

把你的FormView的InsertItemTemplate(與DropDownLists)內的SqlDataSource控件,然後改成這樣:

<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被引用第一降的設定值的下降。

+0

我做到了,但是當我改變第一個ddl的值時,它給了我一個錯誤:Eval(),XPath()和Bind()等數據綁定方法只能在數據綁定控件的上下文中使用。 – Apollon

+0

@Aolloll你可以看到哪一塊標記導致了這個錯誤?你是否在頁面的其他地方使用了Eval或Bind,還是僅僅在這兩個DropDownLists中? – jadarnel27

+0

錯誤是:數據綁定方法(如Eval(),XPath()和Bind()只能用於數據綁定控件的上下文中。它顯示在一塊: Apollon

相關問題