首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在EditItemTemplate中使用AutoPostBack?

如何在EditItemTemplate中使用AutoPostBack?
EN

Stack Overflow用户
提问于 2011-05-18 21:50:16
回答 4查看 2K关注 0票数 4

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

页面是这样开始的:

代码语言:javascript
复制
<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 />

我就有了

代码语言:javascript
复制
   <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>        

当页面加载时,我得到这个错误:

代码语言:javascript
复制
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.

如果我将CustomObjectDataSources srcBrokers和srcAccounts“移入”EditItemTemplate,页面加载正常,但是,当我在ddlBrokers中选择一个代理时,我再次得到相同的错误:

代码语言:javascript
复制
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.

有什么办法解决这个问题吗?数据源应该在EditItemTemplate外部还是内部?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-05-18 23:24:43

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

当项目尝试更新时,您必须在FormView ItemUpdating Event中传递此下拉所选值

票数 0
EN

Stack Overflow用户

发布于 2011-05-18 21:58:52

你能用Bind()代替Eval()吗?

票数 0
EN

Stack Overflow用户

发布于 2012-02-27 07:42:41

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6045819

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档