如何在EditItemTemplate中使用AutoPostBack?

时间:2011-05-18 13:50:16

标签: asp.net formview edititemtemplate

我在FormView EditItemTemplate中有几个DropDownLists。其中一个是经纪人清单,另一个是经纪人账户清单。更改Broker DropDownList时,我希望使用该代理的Accounts列表填充Accounts DropDownList。

Page开头就是这样:

<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内部,那么页面加载正常,但是当我在ddlBrokers中选择一个代理时,我又得到了同样的错误:

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

有关如何解决此问题的任何想法?数据源应该在EditItemTemplate之外还是在里面?

4 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

当项目尝试更新时,您必须在FormView ItemUpdating Event

中传递此下拉列表选定值

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

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

使xx成为这样的函数:

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