2
votes

I have a GridView in an UpdatePanel like bellow:

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

And the Code Behind is like bellow:

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();
    }
}

When I click the ibDelete, the related row is deleted from database, but the page is not changed until I refresh the page. Where am I going wrong?

1

1 Answers

2
votes
<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();
    }
}