【问题标题】:TargetInvocationException: Exception has been thrown by the target of an invocation. on Data BindTargetInvocationException:调用的目标已引发异常。关于数据绑定
【发布时间】:2010-07-20 12:49:52
【问题描述】:

我正在尝试在 RowEditing 事件中将 GridView 绑定到数据源,但它会引发异常:

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

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    根据您的代码,我认为您错过了 Gridview 的 DataValue 字段。

    这是来自 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-22
      • 2018-11-08
      • 2018-02-14
      • 1970-01-01
      • 2016-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多