简体   繁体   中英

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:

<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.)

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:

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. "

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:

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

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

OnRowDeleting="gridViewExistingSchedules_RowDeleting"

Couple of things...

If you specify OnRowCommand="gridViewExistingSchedules_RowCommand" then technically this will catch the delete command too. therefore you can remove OnRowDeleting="gridViewExistingSchedules_RowDeleting" and catch it using a switch on command name. (see here http://msdn.microsoft.com/en-us/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.

You are receiving this because the delete method is called on the gridview gridViewSchedulerDetails which is not being dealt with. You have 2 options to get rid of it.

  1. Add an OnRowDeleting method to the child grid ( gridViewSchedulerDetails ) and handle that.
  2. Add an OnRowCommand method to the child grid ( gridViewSchedulerDetails ) and handle that.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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