为什么我在gridview中的Delete按钮没有命中RowCommand

本文关键字:RowCommand 按钮 Delete gridview 中的 为什么 | 更新日期: 2023-09-27 18:29:42

我有一个独特的问题。网格视图中的删除按钮不起作用,它没有命中行命令和事件。我试过调试,但根本没有碰到行命令事件。为什么?

<asp:GridView runat="server" ID="grdViewTrainingsCourses" AllowPaging="true" PageSize="15" OnPageIndexChanging="grdViewTrainingsCourses_PageIndexChanging" OnRowCommand="grdViewTrainingsCourses_RowCommand"
    HeaderStyle-BackColor="#99CC99" DataKeyNames="pk_TrainingsCourses_ID" AutoGenerateColumns="false" CssClass="table table-condensed table-bordered table-striped table-responsive scrollable">
    <Columns>
        <asp:BoundField DataField="pk_TrainingsCourses_ID" HeaderText="S.No" />
        <asp:BoundField DataField="PersonalInfoName" HeaderText="Employee Name" />
        <asp:BoundField DataField="TrainingsCoursesCourseTitle" HeaderText="Course Title" />
        <asp:BoundField DataField="TrainingsCoursesInstituteName" HeaderText="Institute Name" />
        <asp:BoundField DataField="TrainingsCoursesInstituteAddress" HeaderText="Institute Address" />
        <asp:BoundField DataField="TrainingsCoursesFromDate" HeaderText="From " />
        <asp:BoundField DataField="TrainingsCoursesToDate" HeaderText="To" />
        <asp:ButtonField CommandName="cmdEdit" HeaderText="Edit" ImageUrl="~/assets/global/images/shopping/edit.png" ButtonType="Image" ControlStyle-Width="25px" ControlStyle-Height="25px" />
        <asp:TemplateField HeaderText="Delete">
            <ItemTemplate>
                <asp:ImageButton ID="btnDelete" runat="server" ImageUrl="~/assets/global/images/shopping/delete.png" CommandName="cmdDelete" CommandArgument='<%# Container.DataItemIndex %>' OnClientClick="return confirm('Are you Sure ?');" ControlStyle-Width="25px" ControlStyle-Height="20px" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

.cs

if (e.CommandName == "cmdDelete")
{
    MngTrainingsCourses.DeleteTrainingsCourses(TrainingsCoursesID);
    MngAudits.InsertAuditsInfo("Data deleted from" + " " + "TrainingsCourses", MngAudits.UserMacAddress, MngAudits.UserIPAddress, UserID);
    //LoadTraningsCourses();
    ActivateTrainingsCourses_Tab();
    int PersonalInfoID = Convert.ToInt32(Session["PersonalInfoID"]);
    if (PersonalInfoID != 0)
    {
        LoadTrainingsCoursesByPersonalInfoID(PersonalInfoID);
    }
    else
    {
        LoadTrainingsCourses();
    }

    ShowMsgBox("Record Deleted Successfully");
}

请帮帮我它浪费了我很多时间

为什么我在gridview中的Delete按钮没有命中RowCommand

您也可以使用链接按钮而不是图像按钮

<asp:TemplateField HeaderText="Delete">
  <ItemTemplate>
    <asp:LinkButton ID="lnkDelete" runat="server" Text="✖ Delete" CommandName="cmdDelete"  CssClass="gvwdelete" ForeColor="Red" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"  OnClientClick="return confirm('Are you sure to Delete this record?');"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>

我将删除列改为这个而不是模板,并使用

<asp:ButtonField HeaderText="Delete" ImageUrl="~/assets/global/images/shopping/delete.png" CommandName="cmdDelete" ButtonType="Image" ControlStyle-Width="25px" ControlStyle-Height="20px" ItemStyle-Width="25px" />