【问题标题】:UpdatePanel and GridViewUpdatePanel 和 GridView
【发布时间】:2012-12-26 19:51:48
【问题描述】:

我在 UpdatePanel 中有一个 GridView,如下所示:

<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 时,相关行会从数据库中删除,但在我刷新页面之前页面不会更改。我哪里错了?

【问题讨论】:

    标签: c# asp.net gridview updatepanel


    【解决方案1】:
    <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();
        }
    }
    

    【讨论】:

    • 应该是 if (e.CommandName == "Delete") 注意Delete中的大写字母D
    猜你喜欢
    • 2011-01-31
    • 1970-01-01
    • 2011-09-02
    • 1970-01-01
    • 1970-01-01
    • 2012-03-04
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    相关资源
    最近更新 更多