开发者

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

I'm trying to bind a GridView to datasource on RowEditing event, but it throws an exception:

[TargetInvocationException: Exception has been thrown by the target of an invocation.] Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.

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 HeaderTe开发者_运维技巧xt="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>


as per your code, i think you missed the DataValue Field for the Gridview.

THis is the Sample from MSDN, How to Bind the Dropdownlist

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

Without DataValue Field, Row Editing and other Event will not Perform the Logic. I hope i helped you. You can also check in MSDN For SqlDatasource http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜