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" P开发者_运维技巧ropertyName="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?
Remove Binding Expression SelectedValue='<%# Bind("accountId") %>'
from ddlAccounts. This is causing the problem. You need to handle this from code behind.
When the item is try to update, you have to pass this dropdown Selected value in FormView ItemUpdating Event
Add a flag for when FormView's ItemUpdated happens. In FormView's PreRender check if (IsPostBack && !_fvWasUpdated) {formView1.DataBind();}
This will fix it. The problem is that FormView doesn't do DataBinding on postback, and if postback doesn't come from formview itself, it will lose its datacontext.
Can you try Eval()
instead of Bind()
SelectedValue='<%# xx(DataBinder.Eval(Container.DataItem,"fieldname")) %>'
make xx a function like so:
Function xx(ByVal a As String) As String
Return a
End Function
精彩评论