我在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之外還是在內部?
我使用更新面板裏面的數據源這一點。 – Chad 2011-05-18 14:56:14