2012-01-10 66 views
0

關於這個問題有很多問題,但我不相信任何解決我的具體問題。我有一個可以通過gridview編輯的EntitySet。它顯示正常。但是,我有兩個下拉框必須處理外鍵關係。這可以在同一頁面上的formview中進行插入。有一次,我在編輯視圖中爲網格工作。將DropDownList綁定到GridView的EditItemTemplate中的EntityDataSource

這是在ASP.Net 3.5(實體框架1)中。我使用了Julie Lerman的實體框架書(第11章)第286頁中的一個例子。

我得到的錯誤是「Eval(),XPath()和Bind()等數據綁定方法只能在數據綁定控件的上下文中使用。」

我在這發現的大多數帖子都與Eval有關,而不是綁定。該代碼在顯示模式下工作(將Eval添加到標籤中),但切換到編輯模式時出現錯誤。

任何幫助,將不勝感激。讓我知道我是否可以提供更多信息。

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

在頁面後來我有一個與插入工作一個FormView:

<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> 
+0

只是一個FYI,我打開了該書的相關示例(第一版和樣本是3.5,因此使用VS2008來運行它)。我沒有問題編輯並保存在下拉使用的值: 2012-01-10 16:56:20

回答

0

原來FormView控件(和它的數據源)被綁定到電網造成的問題。我留在來自GridView的SelectedValue的Id參數中,但由於GridView是可編輯的,因此嘗試以兩種可編輯方式綁定記錄時遇到問題。

具體而言,我需要從GridView中刪除DataKeyNames屬性,以及引用GridView的FormView數據源中的任何參數。

由於FormView僅用於插入,所以需要將其設置爲默認模式,以便可見。當沒有綁定到GridView時,EmptyItemTemplate不可見,所以我使用了一個按鈕和jquery-ui來隱藏和顯示formview(始終處於插入模式)。

1

重建我的回答評論,所以我可以格式化: 只是一個供參考的,我從書打開了相關樣本(第一版和樣本是3.5,因此使用VS2008來運行它)。我沒有問題,編輯並在下拉保存價值減記使用:

<asp:TemplateField HeaderText="PrimaryActivity" SortExpression="PrimaryActivity"> 
    <EditItemTemplate> 
    <asp:DropDownList runat="server" ID="act1DDL" 
     DataTextField="ActivityName" 
     DataValueField="ActivityID" 
     DataSourceID="EntityDataSource5" 
     AppendDataBoundItems="True" 
     SelectedValue='<%# Bind("PrimaryActivity.ActivityID") %>'> 
     <asp:ListItem Value="">*</asp:ListItem> 
    </asp:DropDownList> 
    </EditItemTemplate> 
    <ItemTemplate> 
     <asp:Label ID="Label1" runat="server" 
       Text='<%# Bind("PrimaryActivity.ActivityName") %>'></asp:Label> 
    </ItemTemplate> 
</asp:TemplateField> 

我知道「的作品在我的電腦」是不是巨大的幫助,但別的東西是怎麼回事。也許我通過twitter向你發送的鏈接可能有用嗎?

http://aspadvice.com/blogs

相關問題