我正在尝试将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>
答案 0 :(得分:0)
根据您的代码,我认为您错过了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