在asp.net c#中,子gridview抛出异常的行删除事件
本文关键字:抛出异常 删除 事件 gridview net asp | 更新日期: 2023-09-27 17:50:59
我已经搜索了很多,但无法找到解决我的问题的方法。
c#。我在主子关系中有两个gridview控件,如下所示:
<asp:GridView ID="gridViewExistingSchedules"
runat="server" DataKeyNames="SchedulerId"
AutoGenerateColumns="false"
OnRowDataBound="gridViewExistingSchedules_RowDataBound"
OnRowCommand="gridViewExistingSchedules_RowCommand"
OnRowDeleting="gridViewExistingSchedules_RowDeleting">
<Columns>
<asp:TemplateField ItemStyle-Width="20px">
<ItemTemplate>
<asp:GridView
ID="gridViewSchedulerDetails"
runat="server"
AutoGenerateColumns="false"
DataKeyNames="SchedulerId">
<Columns>
<asp:BoundField DataField="DetailId" Visible="false" />
<asp:BoundField DataField="Survey" HeaderText="Survey" />
<asp:BoundField DataField="TimeDescription" HeaderText="Time" />
<asp:BoundField DataField="FromDate" HeaderText="From Date" />
<asp:BoundField DataField="ToDate" HeaderText="To Date" />
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="imgDelete" CommandArgument='<%# Bind("SchedulerId")%>' CommandName="Delete"
runat="server" ImageUrl="~/images/delete1.png" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="imgEdit" CommandArgument='<%# Bind("SchedulerId")%>' CommandName="Edit"
runat="server" ImageUrl="~/images/edit.png" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</ItemTemplate>
<ItemStyle Width="20px"></ItemStyle>
</asp:TemplateField>
<asp:BoundField DataField="Frequency" HeaderText="Frequency" />
<asp:BoundField DataField="DayOfWeek" HeaderText="Day Of Week" />
<asp:BoundField DataField="Time" HeaderText="Time" />
<asp:BoundField DataField="NextRunOn" HeaderText="Next Run On" />
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="imgDelete" CommandArgument='<%# Bind("SchedulerId")%>' CommandName="Delete"
runat="server" ImageUrl="~/images/delete.png" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
父/主gridview "gridViewExistingSchedules"显示计划项目,而子gridview "gridViewSchedulerDetails"显示计划项目的详细信息(如哪些项目被计划等)
我想添加一个功能,其中一行在详细的gridview(即gridViewSchedulerDetails)可以被删除/编辑。我有以下代码处理row_deleting和row_command事件:
protected void gridViewExistingSchedules_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int schedulerId = int.Parse(this.gridViewExistingSchedules.DataKeys[e.Row.RowIndex].Value.ToString());
GridView gvDetails = e.Row.FindControl("gridViewSchedulerDetails") as GridView;
gvDetails.RowCommand += new GridViewCommandEventHandler(gvDetails_RowCommand);
gvDetails.RowDeleting += new GridViewDeleteEventHandler(gvDetails_RowDeleting);
UICaller caller = new UICaller();
gvDetails.DataSource = caller.BindSchedulerDetails(schedulerId);
gvDetails.DataBind();
}
}
void gvDetails_RowCommand(object sender, GridViewCommandEventArgs e)
{
UIWriter writer = new UIWriter();
if (e.CommandName.Equals("Delete"))
{
int surveyDetailId = int.Parse(e.CommandArgument.ToString());
if (writer.RemoveSurvey(surveyDetailId))
{
this.labelUserNotification.Text = "Deleted successfully";
}
else
this.labelUserNotification.Text = "Due to some internal error, selected item cannot be deleted";
//bind existing scheduler
UICaller caller = new UICaller();
this.gridViewExistingSchedules.DataSource = caller.BindScheduler();
this.gridViewExistingSchedules.DataBind();
}
else if (e.CommandName.Equals("Edit"))
{
}
}
void gvDetails_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
}
上面给出的代码有一个运行时异常:
" GridView 'gridViewSchedulerDetails'触发的事件RowDeleting未处理。"
首先我认为,由于在父/子关系主gridview需要处理子"gridViewSchedulerDetails"的row_command事件,所以我将代码更改为:
void gvDetails_RowCommand(object sender, GridViewCommandEventArgs e)
{
}
void gvDetails_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
}
protected void gridViewExistingSchedules_RowCommand(object sender, GridViewCommandEventArgs e)
{
UIWriter writer = new UIWriter();
if (e.CommandName.Equals("Delete"))
{
int surveyDetailId = int.Parse(e.CommandArgument.ToString());
if (writer.RemoveSurvey(surveyDetailId))
{
this.labelUserNotification.Text = "Deleted successfully";
}
else
this.labelUserNotification.Text = "Due to some internal error, selected item cannot be deleted";
//bind existing scheduler
UICaller caller = new UICaller();
this.gridViewExistingSchedules.DataSource = caller.BindScheduler();
this.gridViewExistingSchedules.DataBind();
}
else if (e.CommandName.Equals("Edit"))
{
}
}
protected void gridViewExistingSchedules_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
}
但我仍然得到相同的错误上面给出。请告知我如何处理子gridview行删除甚至是什么实际发生在这里
您已经指定在aspx代码中删除事件,并且事件处理程序不在您的.cs文件代码中,这就是为什么它会产生问题。或者像下面这样编写一个事件处理程序。
void gridViewExistingSchedules_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
}
或者如果你不需要从aspx代码中删除以下内容。
OnRowDeleting="gridViewExistingSchedules_RowDeleting"
几件事…
如果您指定OnRowCommand="gridViewExistingSchedules_RowCommand"
,那么从技术上讲,这也将捕获删除命令。因此,您可以删除OnRowDeleting="gridViewExistingSchedules_RowDeleting"
并使用开关打开命令名来捕获它。(见http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand%28v=vs.110%29.aspx)
让我们继续讨论错误。
GridView 'gridViewSchedulerDetails'触发了未处理的RowDeleting事件。
您正在收到此消息,因为在未处理的gridview gridViewSchedulerDetails
上调用了delete方法。您有两个选项可以删除它
- 添加
OnRowDeleting
方法到子网格(gridViewSchedulerDetails
)并处理它。 - 添加
OnRowCommand
方法到子网格(gridViewSchedulerDetails
)并处理它。
只是认为你的图像按钮包含命令名称delete
和edit
…它们分别为删除和编辑事件保留,并触发它们。当您在数据绑定中分配不同的事件时,这可能会导致冲突。尝试在您的子网格视图中将图像按钮上的CommandName
更改为del
和ed
,看看是否有帮助。