将 DropDownList 绑定到 GridView 的 EditItemTemplate 中的 EntityDataSource
关于这个问题有很多问题,但我不认为任何问题能解决我的具体问题。我有一个可通过网格视图编辑的 EntitySet。显示良好。但是,我有两个必须处理外键关系的下拉菜单。这适用于同一页面上的表单视图以进行插入。有一次,我在网格的编辑视图中进行了这项工作。
这是 ASP.Net 3.5(实体框架 1)中的内容。我使用了 Julie Lerman 的《Entity Framework Book》(第 11 章)第 286 页中的示例。
我收到的错误是“Eval()、XPath() 和 Bind() 等数据绑定方法只能在数据绑定控件的上下文中使用。”
我发现的大多数帖子都与 Eval 有关,而不是 Bind。该代码在显示模式下工作(评估进入标签),但在切换到编辑模式时出现错误。
任何帮助将不胜感激。如果我可以提供更多信息,请告诉我。
<asp:EntityDataSource
ID="dsChargePrintMappings"
ConnectionString="name=RateModelConnectionString"
DefaultContainerName="RateEntities"
EntitySetName="ChargePrintMappings"
Include="ChargeType, BillPrintGroup"
OrderBy="it.[EffectiveDate], it.[EffectiveEndDate]"
EnableDelete="true"
EnableUpdate="true"
EnableInsert="true"
runat="server" />
<asp:EntityDataSource
ID="dsChargeType"
ConnectionString="name=RateModelConnectionString"
DefaultContainerName="RateEntities"
EntitySetName="ChargeTypes"
runat="server" />
<asp:GridView
ID="gvChargePrintMappings"
DataSourceID="dsChargePrintMappings"
DataKeyNames="Id"
AutoGenerateColumns="false"
runat="server">
<AlternatingRowStyle CssClass="alternate" />
<Columns>
<asp:BoundField HeaderText="Id" ReadOnly="true" DataField="Id"
/>
<asp:TemplateField HeaderText="Charge Type">
<ItemTemplate>
<asp:Label ID="lbRate" Text='<%# Eval
("ChargeType.Description") %>' runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList
ID="ddChargeType"
DataSourceId="dsChargeType"
DataTextField="Description"
DataValueField="Id"
SelectedValue='<%# Bind("ChargeType.Id") %>'
AppendDataBoundItems="True"
runat="server">
<asp:ListItem Selected="True" Value="">(none)</asp:
ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
在页面的后面,我有一个与插入一起使用的表单视图:
<asp:FormView ID="fvRate" DataSourceID="dsForm" DefaultMode="ReadOnly"
runat="server">
<EmptyDataTemplate>
<asp:Button ID="btnInsert" Text="Create New" CommandName="New"
runat="server" />
</EmptyDataTemplate>
<InsertItemTemplate>
<asp:Label ID="lblEffectiveDate" Text="Effective Date:"
AssociatedControlID="txtEffectiveDate" runat="server" />
<asp:TextBox ID="txtEffectiveDate" onfocus="$(this).datepicker ()" Text='<%# Bind("EffectiveDate") %>' runat="server" /><br>
<asp:Label ID="lblEffectiveEndDate" Text="Effective End Date:"
AssociatedControlID="txtEffectiveEndDate" runat="server" />
<asp:TextBox ID="txtEffectiveEndDate" onfocus="$ (this).datepicker()" Text='<%# Bind("EffectiveEndDate") %>' runat="server"
/><br>
<asp:Label ID="lblChargeType" Text="Charge Type:"
AssociatedControlID="ddChargeType" runat="server" />
<asp:DropDownList
ID="ddChargeType"
DataSourceId="dsChargeType"
DataTextField="Description"
DataValueField="Id"
SelectedValue='<%# Bind("ChargeType.Id") %>'
AppendDataBoundItems="True"
runat="server">
<asp:ListItem Selected="True" Value="">(none)</asp:ListItem
>
</asp:DropDownList><br>
<asp:Label ID="lblBillPrintGroup" Text="Bill Print Group:"
AssociatedControlID="ddBillPrintGroup" runat="server" />
<asp:DropDownList
ID="ddBillPrintGroup"
DataSourceId="dsBillPrintGroup"
DataTextField="Description"
DataValueField="Id"
SelectedValue='<%# Bind("BillPrintGroup.Id") %>'
AppendDataBoundItems="True"
runat="server">
<asp:ListItem Selected="True" Value="">(none)</asp:ListItem
>
</asp:DropDownList><br>
<asp:Button ID="btnInsert" CommandName="Insert" Text="Create"
runat="server" />
<asp:Button ID="btnCancelUpdate" CommandName="Cancel" Text ="Cancel" runat="server" />
</InsertItemTemplate>
</asp:FormView>
There are a lot of questions concerning this issue, but I don't believe any address my specific issue. I have a EntitySet that is editable via a gridview. It is displaying fine. However, I have two dropdowns that are bound to handle foreign key relationships. This works in a formview on the same page for inserting. And at one point, I had this working in Edit view for the grid.
This is in ASP.Net 3.5 (Entity Framework 1). I used an example from page 286 of Julie Lerman's Entity Framework Book (Ch 11).
The error I'm getting is "Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control."
Most posts I find on this are related to Eval, not Bind. The code works in Display mode (which Eval's into a label), but the error comes when switching to Edit Mode.
Any help would be appreciated. Let me know if I can provide more info.
<asp:EntityDataSource
ID="dsChargePrintMappings"
ConnectionString="name=RateModelConnectionString"
DefaultContainerName="RateEntities"
EntitySetName="ChargePrintMappings"
Include="ChargeType, BillPrintGroup"
OrderBy="it.[EffectiveDate], it.[EffectiveEndDate]"
EnableDelete="true"
EnableUpdate="true"
EnableInsert="true"
runat="server" />
<asp:EntityDataSource
ID="dsChargeType"
ConnectionString="name=RateModelConnectionString"
DefaultContainerName="RateEntities"
EntitySetName="ChargeTypes"
runat="server" />
<asp:GridView
ID="gvChargePrintMappings"
DataSourceID="dsChargePrintMappings"
DataKeyNames="Id"
AutoGenerateColumns="false"
runat="server">
<AlternatingRowStyle CssClass="alternate" />
<Columns>
<asp:BoundField HeaderText="Id" ReadOnly="true" DataField="Id"
/>
<asp:TemplateField HeaderText="Charge Type">
<ItemTemplate>
<asp:Label ID="lbRate" Text='<%# Eval
("ChargeType.Description") %>' runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList
ID="ddChargeType"
DataSourceId="dsChargeType"
DataTextField="Description"
DataValueField="Id"
SelectedValue='<%# Bind("ChargeType.Id") %>'
AppendDataBoundItems="True"
runat="server">
<asp:ListItem Selected="True" Value="">(none)</asp:
ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
Later in the page I have a formview that works with insert:
<asp:FormView ID="fvRate" DataSourceID="dsForm" DefaultMode="ReadOnly"
runat="server">
<EmptyDataTemplate>
<asp:Button ID="btnInsert" Text="Create New" CommandName="New"
runat="server" />
</EmptyDataTemplate>
<InsertItemTemplate>
<asp:Label ID="lblEffectiveDate" Text="Effective Date:"
AssociatedControlID="txtEffectiveDate" runat="server" />
<asp:TextBox ID="txtEffectiveDate" onfocus="$(this).datepicker ()" Text='<%# Bind("EffectiveDate") %>' runat="server" /><br>
<asp:Label ID="lblEffectiveEndDate" Text="Effective End Date:"
AssociatedControlID="txtEffectiveEndDate" runat="server" />
<asp:TextBox ID="txtEffectiveEndDate" onfocus="$ (this).datepicker()" Text='<%# Bind("EffectiveEndDate") %>' runat="server"
/><br>
<asp:Label ID="lblChargeType" Text="Charge Type:"
AssociatedControlID="ddChargeType" runat="server" />
<asp:DropDownList
ID="ddChargeType"
DataSourceId="dsChargeType"
DataTextField="Description"
DataValueField="Id"
SelectedValue='<%# Bind("ChargeType.Id") %>'
AppendDataBoundItems="True"
runat="server">
<asp:ListItem Selected="True" Value="">(none)</asp:ListItem
>
</asp:DropDownList><br>
<asp:Label ID="lblBillPrintGroup" Text="Bill Print Group:"
AssociatedControlID="ddBillPrintGroup" runat="server" />
<asp:DropDownList
ID="ddBillPrintGroup"
DataSourceId="dsBillPrintGroup"
DataTextField="Description"
DataValueField="Id"
SelectedValue='<%# Bind("BillPrintGroup.Id") %>'
AppendDataBoundItems="True"
runat="server">
<asp:ListItem Selected="True" Value="">(none)</asp:ListItem
>
</asp:DropDownList><br>
<asp:Button ID="btnInsert" CommandName="Insert" Text="Create"
runat="server" />
<asp:Button ID="btnCancelUpdate" CommandName="Cancel" Text ="Cancel" runat="server" />
</InsertItemTemplate>
</asp:FormView>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在回答中重新创建我的评论,以便我可以格式化:
仅供参考,我打开了那本书中的相关示例(第 1 版和示例是 3.5,因此使用 VS2008 来运行它)。我在使用的下拉菜单中编辑和保存值没有问题:
我知道“在我的电脑上运行”并不是很有帮助,但还有其他事情发生。也许我通过 Twitter 发送给您的链接可能有用?
<一href="http://aspadvice.com/blogs/joteke/archive/2008/08/24/Is-_2200_Databinding-methods-such-as-Eval_280029002C00_-XPath_280 029002C00_-and-Bind_28002900_-只能在-databound-control_2E002200_-causing-you-grief_3F00_.aspx 的上下文中使用” rel="nofollow">http://aspadvice.com/blogs
recreating my comment in answer so I can format:
Just an FYI, I opened the relevant sample from that book (the 1st edition and sample is 3.5 so using VS2008 to run it). I had no problem editing and saving the value in the drop down that uses:
I know "works on my pc" isn't hugely helpful but something else is going on. Perhaps that link I sent you via twitter might be useful?
http://aspadvice.com/blogs
事实证明,FormView(及其数据源)绑定到网格导致了问题。我保留了来自 GridView 的 SelectedValue 的 Id 参数,但由于 GridView 是可编辑的,因此在尝试以两种可编辑的方式绑定记录时遇到问题。
具体来说,我需要从 GridView 中删除 DataKeyNames 属性,以及 FormView 数据源中引用 GridView 的任何参数。
由于 FormView 仅用于插入,因此需要使用默认模式,以便它可见。当没有绑定到 GridView 时,EmptyItemTemplate 不可见,因此我使用按钮和 jquery-ui 来隐藏和显示 formview(始终处于插入模式)。
It turned out that the FormView (and it's DataSource) being bound to the Grid caused the problem. I had left in the Id Parameter coming from the SelectedValue of the GridView, but since the GridView was editable, it was having problems trying to bind the record in two editable ways.
To be specific, I needed to remove the DataKeyNames attribute from the GridView, and any parameters in the FormView DataSource that referenced the GridView.
Since the FormView was then only used to insert, that needed to be the default mode so it was visible. The EmptyItemTemplate wasn't visible when there was no binding to the GridView, so I used a button and jquery-ui to hide and show the formview (which was always in insert mode).