2011-05-18 81 views
4

我在FormView EditItemTemplate中有幾個DropDownLists。其中一個是經紀人名單,另一個是經紀人賬戶列表。當Broker DropDownList發生變化時,我希望爲賬戶DropDownList填充該代理的賬戶列表。如何在EditItemTemplate中AutoPostBack?

頁開始是這樣的:

<asp:FormView 
    ID="fvwEditTrade" 
    DataSourceID="srcTrade" 
    runat="server" 
    DataKeyNames="tradeId" 
    DefaultMode="Edit" 
    CssClass="formView" 
    OnItemUpdated="fvwEditTrade_Updated" 
    OnItemCommand="fvwEditTrade_Command" 
    OnItemUpdating="fvwEditTrade_Updating"    
    > 
<EditItemTemplate> 
    <asp:Label ID="lblTradeId" Text="TradeId: " runat="server" CssClass="label" /><%# Eval("tradeId") %> 
    <br /> 

    <asp:Label ID="lblBroker" Text="Broker" runat="server" CssClass="label" /> 
    <asp:DropDownList 
    ID="ddlBrokers" 
    runat="server" 
    CssClass="dropdownlist" 
    DataSourceID="srcBrokers" 
    DataTextField="broker" 
    DataValueField="brokerId" 
    SelectedValue='<%# Bind("brokerId") %>' 
    AutoPostBack="true"    
    /> 
    <br /> 

    <asp:Label ID="lblAccount" Text="Account" AssociatedControlID="ddlAccounts" runat="server" CssClass="label" /> 
    <asp:DropDownList 
    ID="ddlAccounts" 
    runat="server" 
    CssClass="dropdownlist" 
    DataSourceID="srcAccounts" 
    DataTextField="description" 
    DataValueField="accountId" 
    SelectedValue='<%# Bind("accountId") %>' 
    /> 
    <br /> 

我再有

<asp:Button 
    id="lnkUpdate" 
    Text="Update" 
    CommandName="Update" CssClass="button" 
    Runat="server" /> 

    <asp:Button 
    id="lnkCancel" 
    Text="Cancel" 
    CommandName="Cancel" CssClass="button" 
    Runat="server" /> 

</EditItemTemplate> 
</asp:FormView>    


<CustomControls:CustomObjectDataSource 
    id="srcTrade" 
    TypeName="DatabaseComponent.DBUtil" 
    SelectMethod="GetTrade" 
    UpdateMethod="UpdateTrade" 
    runat="server"> 
    <SelectParameters> 
    <asp:QueryStringParameter Name="tradeId" QueryStringField="tradeId" />    
    </SelectParameters> 
    <UpdateParameters>     
    <asp:ControlParameter Name="tradeId" ControlId="fvwEditTrade" PropertyName="SelectedValue" /> 
    <asp:ControlParameter Name="accountId" ControlId="fvwEditTrade$ddlAccounts" PropertyName="SelectedValue" /> 
    <asp:ControlParameter Name="symbol" ControlId="fvwEditTrade$ddlSymbols" PropertyName="SelectedValue" /> 
    <asp:ControlParameter Name="riskProfileId" ControlId="fvwEditTrade$ddlRiskProfiles" PropertyName="SelectedValue" /> 
    <asp:ControlParameter Name="pctAccountRisked" ControlId="fvwEditTrade$txtPctAccountRisked" PropertyName="Text" /> 
    <asp:ControlParameter Name="tradeSetupId" ControlId="fvwEditTrade$ddlSetups" PropertyName="SelectedValue" /> 
    <asp:ControlParameter Name="amountPerUnit" ControlId="fvwEditTrade$txtamountPerUnit" PropertyName="Text" /> 
    <asp:ControlParameter Name="initialStopPrice" ControlId="fvwEditTrade$txtInitialStopPrice" PropertyName="Text" /> 
    <asp:ControlParameter Name="tfCode" ControlId="fvwEditTrade$ddlTimeFrames" PropertyName="SelectedValue" /> 
    <asp:ControlParameter Name="MAEPips" ControlId="fvwEditTrade$txtMAEPips" PropertyName="Text" /> 
    <asp:ControlParameter Name="MFEPips" ControlId="fvwEditTrade$txtMFEPips" PropertyName="Text" /> 
    <asp:ControlParameter Name="tradeGrade" ControlId="fvwEditTrade$ddlTradeGrades" PropertyName="SelectedValue" /> 
    <asp:ControlParameter Name="executionGrade" ControlId="fvwEditTrade$ddlExecutionGrades" PropertyName="SelectedValue" /> 
    <asp:ControlParameter Name="comment" ControlId="fvwEditTrade$txtComments" PropertyName="Text" /> 
    </UpdateParameters> 
</CustomControls:CustomObjectDataSource> 

<CustomControls:CustomObjectDataSource 
    id="srcBrokers" 
    TypeName="DatabaseComponent.DBUtil" 
    SelectMethod="GetBrokers" 
    runat="server"> 
</CustomControls:CustomObjectDataSource> 

<CustomControls:CustomObjectDataSource 
    id="srcAccounts" 
    TypeName="DatabaseComponent.DBUtil" 
    SelectMethod="GetBrokerAccounts" 
    runat="server"> 
    <SelectParameters> 
    <asp:ControlParameter Name="brokerId" ControlId="fvwEditTrade$ddlBrokers" PropertyName="SelectedValue" /> 
    </SelectParameters> 
</CustomControls:CustomObjectDataSource>   

頁面加載時我得到這個錯誤:

Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control. 

如果我移動CustomObjectDataSources srcBrokers和srcAccounts在EditItemTemplate的「內部」,頁面加載正常,但是當我在ddlBro中選擇代理時KERS,我再次得到了同樣的錯誤:

Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control. 

關於如何解決此問題的任何想法?數據源應該在EditItemTemplate之外還是在內部?

+0

我使用更新面板裏面的數據源這一點。 – Chad 2011-05-18 14:56:14

回答

0

移除ddlAccounts綁定表達式SelectedValue='<%# Bind("accountId") %>'。這是造成這個問題的原因。你需要從後面的代碼處理這個。

當產品嚐試更新,你必須通過在FormView ItemUpdating Event

0

您可以嘗試​​代替Bind()

+0

我將Bind()更改爲Eval(),我得到相同的錯誤。 – 2011-05-18 14:35:28

0

這個下拉菜單選定值添加時FormView的ItemUpdated發生的標誌。 在FormView的在PreRender檢查,如果(!的IsPostBack & & _fvWasUpdated){formView1.DataBind();}

這將解決這個問題。問題是FormView不會在回發時執行DataBinding,並且如果回發不是來自formview本身,它將會丟失它的datacontext。

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

化妝XX功能,像這樣:

Function xx(ByVal a As String) As String 
    Return a 
End Function 
相關問題