Ammar Asjad
Ammar Asjad

Reputation: 3000

Confused with Grid view OnRowDeleting Event?

Today i was come to an issue when i was deleting a record on basis of id from grid view and i used OnRowCommand event for this. here is my gridview code :

<asp:GridView ID="gridShow" runat="server" AutoGenerateColumns="False" PageSize="5"
            AllowPaging="true" ShowHeader="false" OnRowCommand="s_index" OnRowDeleting="gridShow_RowDeleting">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:TextBox ID="txtID" runat="server" Text='<%#Eval("ID") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField ItemStyle-Width="15%">
                    <ItemTemplate>
                        <asp:TextBox ID="txtDescription" runat="server" Text='<%#Eval("RollNumber") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:TextBox ID="txtFname" runat="server" Text='<%#Eval("FirstName") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField ItemStyle-Width="15%">
                    <ItemTemplate>
                        <asp:TextBox ID="txtLname" runat="server" Text='<%#Eval("LastName") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField ItemStyle-Width="15%">
                    <ItemTemplate>

                        <asp:TextBox ID="txtEmail" runat="server" Text='<%#Eval("Email") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField ItemStyle-Width="15%">
                    <ItemTemplate>
                        <asp:Label ID="lblStatus" runat="server"></asp:Label>
                        <asp:HiddenField ID="hdnStatus" runat="server" Value='<%#Eval("UserName") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:LinkButton ID="lb" runat="server" Text="Delete" CommandName="delete" CommandArgument='<%#Eval("ID") %>'></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

Here is my C# code :

 protected void s_index(object sender, GridViewCommandEventArgs e)
 {
     if (e.CommandName == "delete")
     {
         oSRegisterBLL.BLLdelete(Convert.ToInt32(e.CommandArgument));
         gview();
      }
  }
  protected void gridShow_RowDeleting(object sender, GridViewDeleteEventArgs e)
  {
  }

I successfully did this task by adding OnRowDeleting event on my grid view and definition of that event on my page behind but when i removed this first time i have come to known and issue " ASP.datashow_aspx' does not contain a definition for 'gridShow_RowDeleting' and no extension method 'gridShow_RowDeleting' accepting a first argument of type 'ASP.datashow_aspx' could be found (are you missing a using directive or an assembly reference?) "

I am confused with that why to add OnRowDeleting event on grid view with onrowcommand event ? why i am confused because if i did not do any work with this event then why to use this event? is there any way to work with only onrowcommand event ? or adding onrowdeleting event is essential for deletion of records from gridview? I want to clear my mind for this ?

Upvotes: 3

Views: 2115

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460138

Your aspx markup of the GridView has declared the event handler here:

OnRowDeleting="gridShow_RowDeleting"

So when you try to remove it from the coedebehind you'll get that exception. So simply remove the event-handler and you can remove it from the codebehind.

Edit

Having a Delete-button, or even a regular button in a GridView with a CommandName of delete(which is the case here), will automatically try to fire OnRowDeleting.

So you have to add the event handler even if you don't use it or you have to rename the CommandName to e.g. "DeleteUser", otherwise you get exceptions like "The GridView 'gridShow' fired event RowDeleting which wasn't handled".

Upvotes: 4

Related Questions