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>