如何在 EditItemTemplate 中自动回发?

发布于 2024-11-07 16:43:49 字数 4970 浏览 0 评论 0原文

我在 FormView EditItemTemplate 中有几个 DropDownList。其中一个是经纪人列表,另一个是经纪人账户列表。当经纪商 DropDownList 更改时,我希望账户 DropDownList 中填充该经纪商的账户列表。

页面如下所示:

<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 外部还是内部?

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" 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 "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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

月隐月明月朦胧 2024-11-14 16:43:49

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

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

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

我的奇迹 2024-11-14 16:43:49

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

这将修复它。问题是 FormView 不会在回发时执行 DataBinding,如果回发不是来自 formview 本身,它将丢失其数据上下文。

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.

温柔戏命师 2024-11-14 16:43:49

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

Can you try Eval() instead of Bind()

送舟行 2024-11-14 16:43:49
SelectedValue='<%# xx(DataBinder.Eval(Container.DataItem,"fieldname")) %>' 

让 xx 成为一个像这样的函数:

Function xx(ByVal a As String) As String
    Return a
End Function
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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文