Gridview Delete Linkbutton 不触发 RowDeleting 事件

Gridview Delete Linkbutton not firing RowDeleting Event

我很沮丧。使用 asp.net 网格视图。使用带 CommandName="Delete" 的 LinkBut​​ton。不明白为什么页面没有回发。我已经在其他应用程序中这样做了一百万次。我将它们相互比较,没有什么不同。不过,我会先说这是别人的模板,所以这不是我的模板。

有什么可能导致我的问题的想法吗?

我的场景是这样的:

ASPX 页面(已编辑以添加 HTML;site.Master 中还有更多 HTML,页面顶部还有一个用于 AjaxControlToolkit 的标记):

<div class="width80 container body-content">
    <h2 class="marginTop50">Message Board</h2>
    <asp:Panel ID="pnlMsgsForUser" runat="server" Visible="false">
        <div class="jumbotronSmallFont">
            <asp:Label ID="lblErrorMessage" CssClass="has-error" runat="server"></asp:Label>
        </div>
    </asp:Panel>

    <div class="jumbotronSmallFont">
        <h4>New Message</h4>
        <form>
            <div class="form-group">
                <label for="messageBody">Message Body</label>
                <textarea class="form-control" id="messageBody" rows="3" style="max-width: 600px;"></textarea>
            </div>
            <div class="text-center">
                <button id="btnSave" class="btn btn-primary" style="width: 75px;">Save</button>
                <button type="button" id="btnReset" class="btn btn-default marginLeft15px" style="width: 75px;">Reset</button>
            </div>
        </form>
    </div>

    <div>
        <asp:GridView ID="gvMessages" runat="server" Width="100%"
            CssClass="table adminMessageBoardTable marginAuto" AutoGenerateColumns="False"
            OnRowUpdating="gvMessages_RowUpdating"
            OnRowCancelingEdit="gvMessages_RowCancelingEdit"
            OnRowDataBound="gvMessages_RowDataBound"
            OnRowEditing="gvMessages_RowEditing"
            OnRowDeleting="gvMessages_RowDeleting"
            DataKeyNames="Id" BorderStyle="NotSet">
            <Columns>
                <asp:TemplateField HeaderText="Message" HeaderStyle-CssClass="center" SortExpression="Message">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtMsg" Width="100%" runat="server"></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label runat="server" Text='<%# Eval("MessageBody") %>' ID="lblMessage"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>

                <asp:TemplateField HeaderText="Date Updated" SortExpression="DateUpdated" HeaderStyle-CssClass="center" ItemStyle-CssClass="center">
                    <HeaderStyle Width="120px" />
                    <EditItemTemplate>
                        <asp:Label runat="server" ID="lblEditDateUpdated" Enabled="false"></asp:Label>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label runat="server" Text='<%# Eval("DateUpdated","{0:d}") %>' ID="lblDateUpdated"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>

                <asp:TemplateField HeaderText="Updated By" SortExpression="UpdatedBy" HeaderStyle-CssClass="center" ItemStyle-CssClass="center">
                    <HeaderStyle Width="120px" />
                    <EditItemTemplate>
                        <asp:Label runat="server" Text="" ID="lblEditUpdatedBy" Enabled="false"></asp:Label>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label runat="server" Text='<%# Eval("UpdatedBy") %>' ID="lblUpdatedBy"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>

                <asp:TemplateField ItemStyle-CssClass="center">
                    <HeaderStyle Width="120px" />
                    <EditItemTemplate>
                        <asp:LinkButton ID="lbUpdate" runat="server" CausesValidation="True"
                            CommandName="Update" Text="Update"
                            OnClientClick="return confirm('You are about to update this entry.  \n\nDo you wish to proceed?');"></asp:LinkButton>
                        &nbsp;<asp:LinkButton ID="lbCancel" runat="server" CausesValidation="False"
                            CommandName="Cancel" Text="Cancel"></asp:LinkButton>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:LinkButton ID="lbEdit" runat="server" CausesValidation="False"
                            CommandName="Edit" Text="Edit"></asp:LinkButton> &nbsp; &nbsp;
                        <asp:LinkButton ID="lbDelete" runat="server" CommandArgument='<%# Eval("Id") %>' CausesValidation="False" 
                            CommandName="Delete" Text="Delete" OnClientClick="return confirm('You are about to delete this entry. \n\Do you wish to proceed?');"></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>

            </Columns>
        </asp:GridView>

    </div>

</div>

在我的代码隐藏中,我有:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            var user = CommonFunctions.GetUserID(true);
            var Admin = Roles.IsUserInRole(user, "Administrator");
            var Dev = Roles.IsUserInRole(user, "Developer");

            if (!Admin && !Dev)
            {
                Response.Redirect("~/");
            }

            gvBind(true);
        }
    }

当我点击一行的删除链接按钮时,它会掉入 Page_Load 和

!Page.IsPostBack 

验证为 true。我不知道为什么要这样做。它甚至从未命中 RowDeleting 命令。我的断点好伤心

在我的另一个应用程序中,所有标记和代码隐藏都几乎相同。唯一的区别是 gridview 名称和 Eval 标记。但是,当我在该应用程序中单击“删除”时,它会跳过 !Page.IsPostBack 部分。 :( 它也显然触发了 RowDeleting。

我也尝试过实施 RowCommand,但该事件也从未触发过。在实质上刷新页面之前,它只执行 Page_Load 和 RowDataBound。

有什么想法吗?

拜托,谢谢!!!

 OnClientClick="return confirm('Are you sure?');"

尝试从您的确认语句中添加 return。

您的代码运行良好。当我单击删除按钮时,它会触发 gvMessages_RowDeleting 事件。这是我测试它的方法 -

<asp:GridView ID="gvMessages" runat="server" Width="100%"
    CssClass="GridView marginAuto" AutoGenerateColumns="False"
    OnRowDeleting="gvMessages_RowDeleting"
    DataKeyNames="Id">
    <Columns>
        <asp:TemplateField HeaderText="Message" HeaderStyle-CssClass="center" SortExpression="Message">
            <ItemTemplate>
                <asp:Label runat="server" Text='<%# Eval("MessageBody") %>' ID="lblMessage"></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField ItemStyle-CssClass="center">
            <ItemTemplate>
                <asp:LinkButton ID="lbDelete" runat="server" CommandArgument='<%# Eval("Id") %>' CausesValidation="False"
                    CommandName="Delete" Text="Delete"
                    OnClientClick="confirm('You are about to delete this entry. \n\Do you wish to proceed?');"></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

代码隐藏

public class Item
{
    public int Id { get; set; }
    public string MessageBody { get; set; }
}

public partial class _Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            gvMessages.DataSource = new List<Item>
            {
                new Item {Id = 1, MessageBody = "One"},
                new Item {Id = 2, MessageBody = "Two"},
                new Item {Id = 3, MessageBody = "Three"},
            };
            gvMessages.DataBind();
        }
    }

    protected void gvMessages_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }
}