How to make AutoPostBack in EditItemTemplate?

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

The page starts as follows:

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

Then i 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 files "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 data sources be outside of EditItemTemplate or inside?

+4
source share
4 answers

Remove the binding expression SelectedValue='<%# Bind("accountId") %>' from ddlAccounts. This is causing a problem. You need to handle this from the code behind.

When an item tries to refresh, you must pass this drop-down menu. Selected Value in FormView ItemUpdating Event

0
source

Can you try Eval() instead of Bind()

0
source

Add a flag for the case when a FormView ItemUpdated occurs. In FormView PreRender, check if (IsPostBack &! _FvWasUpdated) {formView1.DataBind ();}

This will fix it. The problem is that FormView does not execute DataBinding on postback, and if postback does not come from formview, it will lose its datacontext.

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

make xx such a function:

 Function xx(ByVal a As String) As String Return a End Function 
0
source

All Articles