2010-07-20 74 views
1

我試圖綁定一個GridView到數據源上RowEditing事件,但它拋出一個異常:TargetInvocationException:調用的目標引發了異常。在數據綁定

[TargetInvocationException:異常已通過調用的目標引發異常] 數據綁定方法,因爲Eval(),XPath()和Bind()只能用於數據綁定控件的上下文中。

lblackout = EXMailbox.GetBlackouts(); 
BlackoutGridView.DataSource=lblackout; 


protected void BlackoutGridView_RowEditing(object sender, GridViewEditEventArgs e) 
{ 
     BlackoutGridView.EditIndex = e.NewEditIndex; 
     BlackoutGridView.DataSource = lblackout; //Throws exception here. 
     BlackoutGridView.DataBind(); 
} 


      <asp:GridView ID="BlackoutGridView" runat="server" AutoGenerateColumns="False" 
       OnRowDeleting="BlackoutGridView_RowDeleting" 
       OnRowEditing="BlackoutGridView_RowEditing" 
       onrowcancelingedit="BlackoutGridView_RowCancelingEdit">        
       <Columns> 
        <asp:BoundField DataField="BlackoutId" HeaderText="BlackoutId" > 
        </asp:BoundField> 
        <asp:TemplateField HeaderText="Region"> 
         <EditItemTemplate> 
         <asp:DropDownList ID="DropDownList11" runat="server" Text='<%# Bind("Region") %>' 
           DataSourceID="lblackout" DataTextField="Region" DataValueField="Region"></asp:DropDownList> 
          <asp:ObjectDataSource ID="lblackout" runat="server" SelectMethod="GetDataItem" 
           TypeName="Exchange.MailboxMove.WebUI.BlackoutScreen"></asp:ObjectDataSource> 
         </EditItemTemplate> 
         <ItemTemplate> 
          <asp:Label ID="Label1" runat="server" Text='<%# Bind("Region") %>'></asp:Label> 
         </ItemTemplate> 
        </asp:TemplateField> 
        <asp:BoundField HeaderText="Server" DataField="Server"> 
        </asp:BoundField> 
        <asp:BoundField HeaderText="From Date" DataField="StartDateTime" 
         DataFormatString="{0:D}"> 
        </asp:BoundField> 
        <asp:BoundField HeaderText="To Date" DataField="EndDateTime" 
         DataFormatString="{0:D}"> 
        </asp:BoundField> 
        <asp:BoundField HeaderText="From Time" DataField="StartDateTime" 
         DataFormatString="{0:t}"> 
        </asp:BoundField> 
        <asp:BoundField HeaderText="To Time" DataField="EndDateTime" 
         DataFormatString="{0:t}"> 
        </asp:BoundField> 
        <asp:CheckBoxField HeaderText="IsWeekly" DataField="IsWeekly"> 
        </asp:CheckBoxField> 
        <asp:CommandField DeleteText="Cancel" HeaderText="Action" 
         ShowDeleteButton="True" ShowEditButton="True" ShowHeader="True" > 
        </asp:CommandField> 

回答

0

按你的代碼,我想你錯過了DataValue字段爲GridView。

這是從MSDN,樣本如何綁定的下拉列表

<asp:DropDownList ID="InsertCategoryDropDownList" 
          DataSourceID="CategoriesDataSource" 
          DataTextField="CategoryName" 
          DataValueField="CategoryID" 
          RunAt="Server" /> 

沒有DataValue場,行編輯和其他事件將不會執行邏輯。我希望我幫助你。 您還可以登錄MSDN For SqlDatasource http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.aspx

相關問題