entity-frameworkwebformsentitydatasource

Binding DropDownList to an EntityDataSource in EditItemTemplate of a GridView


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>

Solution

  • 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).