UpdatePanel and GridView

本文关键字:GridView and UpdatePanel | 更新日期: 2023-09-27 17:49:31

我有一个GridView在一个UpdatePanel如下:

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" GridLines="None"
            AllowPaging="True" OnRowCommand="GridViewAllProducts_RowCommand">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <img id="imgImage" src='<%# Bind("Image") %>' class="imgNewsAllNewsUC noBorder" alt=""
                            runat="server" />
                        <asp:ImageButton ID="ibDelete" CommandName="delete" CommandArgument='<%# Bind("Id") %>'
                            runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>

后面的代码如下:

protected void GridViewAllProducts_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "delete")
    {
        new ProductC().DeleteBy_Id(Convert.ToInt32(e.CommandArgument));
        GridViewAllProducts.DataSource = new ProductC().GetAllProducts();
        GridViewAllProducts.DataBind();
    }
}

当我单击ibDelete时,相关的行从数据库中删除,但是直到我刷新页面才更改页面。我哪里做错了?

UpdatePanel and GridView

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
    ....
</asp:UpdatePanel>
protected void GridViewAllProducts_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "delete")
    {
        new ProductC().DeleteBy_Id(Convert.ToInt32(e.CommandArgument));
        GridViewAllProducts.DataSource = new ProductC().GetAllProducts();
        GridViewAllProducts.DataBind();
        UpdatePanel1.Update();
    }
}