簡體   English   中英

TargetInvocationException:調用的目標引發了異常。 關於數據綁定

[英]TargetInvocationException: Exception has been thrown by the target of an invocation. on Data Bind

我正在嘗試將GridView綁定到RowEditing事件上的數據源,但是會引發異常:

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

根據您的代碼,我認為您錯過了Gridview的DataValue字段。

這是MSDN的示例,如何綁定下拉列表

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

沒有DataValue字段,行編輯和其他事件將不會執行邏輯。 我希望我能幫助你。 您也可以在MSDN中為SqlDatasource簽入http://msdn.microsoft.com/zh-cn/library/system.web.ui.webcontrols.sqldatasource.aspx

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM