如何在 EditItemTemplate 中自动回发?
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
从 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
为 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.
您可以尝试使用
Eval()
而不是Bind()
Can you try
Eval()
instead ofBind()
让 xx 成为一个像这样的函数:
make xx a function like so: