简体   繁体   English

asp.net C#中子gridview抛出异常的行删除事件

[英]Row Deleting event of a child gridview throwing exception in asp.net C#

I have searched a lot but couldn't find a solution to my problem. 我进行了很多搜索,但找不到解决我问题的方法。

With C#.Net, Asp.net 3.5 I have a 2 gridview controls in master child relation as following: 使用C#.Net,Asp.net 3.5,我在主子关系中有2个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>

Parent/master gridview "gridViewExistingSchedules" displays scheduled items where as child gridview "gridViewSchedulerDetails" displays details of a scheduled item (like which items were scheduled etc.) 父/主gridview“ gridViewExistingSchedules”显示计划的项目,而子gridview“ gridViewSchedulerDetails”显示计划项目的详细信息(例如计划了哪些项目等)。

I want to add a functionality, where a row in detailed gridview (ie gridViewSchedulerDetails can be deleted/edited. I have following code which handles row_deleting and row_command events: 我想添加一个功能,其中详细的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)
        {


        }

With above given code there is run time exception: 上面给出的代码存在运行时异常:

" The GridView 'gridViewSchedulerDetails' fired event RowDeleting which wasn't handled. " GridView'gridViewSchedulerDetails'触发了未处理的事件RowDeleting。

First I thought that since being in parent/child relation master gridview need to handle the row_command event of child "gridViewSchedulerDetails" so I changed the code to: 首先,我认为,由于处于父/子关系中,所以主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)
            {

            }

But I am still getting same error given above. 但是我仍然收到上面给出的相同错误。 Please advise how can I handle child gridview row delete even and what is actually happening here 请告知我该如何处理子gridview行删除,以及此处实际发生的情况

You have specified that deleting event in your aspx code and that event handler is not there in your .cs file code that's why it creating problem. 您已在aspx代码中指定了删除事件,而.cs文件代码中没有该事件处理程序,这就是它造成问题的原因。 Either write a event handler like following. 编写如下的事件处理程序。

void gridViewExistingSchedules_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
}

or remove following from your aspx code if you don't need that. 或从不需要的aspx代码中删除以下内容。

OnRowDeleting="gridViewExistingSchedules_RowDeleting"

Couple of things... 几件事...

If you specify OnRowCommand="gridViewExistingSchedules_RowCommand" then technically this will catch the delete command too. 如果您指定OnRowCommand="gridViewExistingSchedules_RowCommand"那么从技术上讲,这也会捕获删除命令。 therefore you can remove OnRowDeleting="gridViewExistingSchedules_RowDeleting" and catch it using a switch on command name. 因此,您可以删除OnRowDeleting="gridViewExistingSchedules_RowDeleting"并使用打开的命令名捕获它。 (see here http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand%28v=vs.110%29.aspx ) (请参见http://msdn.microsoft.com/zh-cn/library/system.web.ui.webcontrols.gridview.rowcommand%28v=vs.110%29.aspx

That aside lets move on to the error. 顺便说一句,让我们继续错误。

The GridView 'gridViewSchedulerDetails' fired event RowDeleting which wasn't handled. GridView'gridViewSchedulerDetails'触发了未处理的事件RowDeleting。

You are receiving this because the delete method is called on the gridview gridViewSchedulerDetails which is not being dealt with. 之所以会收到此消息,是因为在gridViewSchedulerDetails的gridview gridViewSchedulerDetails上调用了delete方法。 You have 2 options to get rid of it. 您有2种选择可以摆脱它。

  1. Add an OnRowDeleting method to the child grid ( gridViewSchedulerDetails ) and handle that. OnRowDeleting方法添加到OnRowDeleting格( gridViewSchedulerDetails )并进行处理。
  2. Add an OnRowCommand method to the child grid ( gridViewSchedulerDetails ) and handle that. OnRowCommand方法添加到OnRowCommand格( gridViewSchedulerDetails )并进行处理。

UPDATE 更新

Just thought your image buttons contain the command name delete and edit ... these are reserved for the events delete and edit and fire them respectively. 只是以为您的图像按钮包含命令名称deleteedit ...这些分别保留给事件delete和edit并触发它们。 As you are assigning different events in your databound this might be causing a conflict. 当您在数据绑定中分配不同的事件时,这可能会导致冲突。 Try changing the CommandName on your image buttons to del and ed in your child grid view and see if that helps. 尝试将图像按钮上的CommandName更改为子网格视图中的deled ,看看是否有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM