4

I have a couple of DropDownLists in a FormView EditItemTemplate. One of them is a list of brokers and the other a list of broker accounts. When the Broker DropDownList is changed, I want the Accounts DropDownList to be populated with a list of Accounts for that broker.

Page starts like this:

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

I then have

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

When the page loads I get this error:

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

If I move the CustomObjectDataSources srcBrokers and srcAccounts "inside" the EditItemTemplate, the page loads fine, HOWEVER, when I select a broker in ddlBrokers, I get the same error again:

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

Any ideas on how to fix this? Should the data sources be outside of the EditItemTemplate or inside?

4

4 回答 4

0

SelectedValue='<%# Bind("accountId") %>'从 ddlAccounts 中删除绑定表达式。这导致了问题。您需要从后面的代码中处理这个问题。

当项目尝试更新时,您必须将此下拉列表中的选定值传递给FormView ItemUpdating Event

于 2011-05-18T15:24:43.183 回答
0
SelectedValue='<%# xx(DataBinder.Eval(Container.DataItem,"fieldname")) %>' 

使 xx 成为这样的函数:

Function xx(ByVal a As String) As String
    Return a
End Function
于 2014-02-28T16:25:34.747 回答
0

当 FormView 的 ItemUpdated 发生时添加一个标志。在 FormView 的 PreRender 中检查 if (IsPostBack && !_fvWasUpdated) {formView1.DataBind();}

这将解决它。问题是FormView在回发时不做DataBinding,如果回发不是来自formview本身,它会丢失它的datacontext。

于 2012-02-26T23:42:40.770 回答
0

你可以尝试Eval()而不是Bind()

于 2011-05-18T13:58:52.023 回答