为什么gridview中的“删除”按钮没有按下RowCommand?

时间:2016-01-11 06:24:09

标签: c# asp.net gridview

我有独特的问题。 gridview中的删除按钮不起作用,它没有命中Row commnand事件。我尝试了调试,但根本没有点击行命令事件。为什么?

<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");

}

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

2 个答案:

答案 0 :(得分:1)

您也可以使用链接按钮代替图像按钮

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

答案 1 :(得分:0)

我将删除coloumn更改为此而不是模板并工作

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