GridView在删除事件后未刷新

4

我在使用asp.net的GridView时遇到了一个奇怪的问题。我有一个GridView绑定到LinqDataSource。在GridView的行删除事件中,我从数据库中删除了该行,但是网格没有刷新(即使已经绑定到数据源)。

当我设置断点时,我可以看到OnRowDeleting事件在LinqDS_Selecting事件之后被触发。但在删除事件之后它并没有再次被触发!这可能是原因吗?我做错了什么?

请问有人能帮忙解决吗?非常感谢。

.aspx文件:

<asp:LinqDataSource
    ID="LinqDS"
    runat="server"
    OnSelecting="LinqDS_Selecting">
</asp:LinqDataSource>

<asp:GridView
    DataSourceID = "LinqDS"
    ID = "gv1"
    runat = "server"  
    DataKeyNames = "InstructionId"
    EnableViewState = "false"
    OnRowDataBound = "gv1_RowDataBound" 
    OnRowDeleting = "gv1_RowDeleting" 
    OnRowCommand = "gv1_RowCommand"
    PageSize = "30" >
    <Columns>
        <!-- My colums --->
    </Columns>
</asp:GridView>

.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{

}

protected void LinqDS_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
    // my linq to sql query
    var query = from  .... .... ;
    e.Result = query;
}

protected void gv1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    int instructionId = (int)gv1.DataKeys[e.RowIndex].Value;
    /// my delete logic
    CTX.SubmitChanges();
    gv1.DataBind();
}

gv1_RowDeleting 是否已触发?如果是,您可能还需要执行 LinqDS.DataBind() 吗? - Aristos
gv1_RowDeleting事件是否触发,并且您尝试删除的值是否实际从数据库中删除? - Denys Wessels
3个回答

1
如果您的记录已正确删除,但网格仅未显示更改,则可以尝试将GridView放置在UpdatePanel中,看看是否有所帮助。
<asp:UpdatePanel runat="server" UpdateMode="Always">
    <ContentTemplate>
        // GridView
    </ContentTemplate>
</asp:UpdatePanel>

1

我无法指出你代码中的错误,但我可以给你我的代码,这是我用来删除数据的。

<asp:GridView ID="GridView1" runat="server" OnRowDeleting="Del" DataKeyNames="Clg_ID" BackColor="#DEBA84" 
                BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" 
                CellSpacing="2">
                <Columns>
                    <asp:CommandField ShowDeleteButton="True" />
                </Columns>
                <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
                <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
                <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
                <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
                <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
                <SortedAscendingCellStyle BackColor="#FFF1D4" />
                <SortedAscendingHeaderStyle BackColor="#B95C30" />
                <SortedDescendingCellStyle BackColor="#F1E5CE" />
                <SortedDescendingHeaderStyle BackColor="#93451F" />
            </asp:GridView>

.cs 文件

protected void Del(object sender, GridViewDeleteEventArgs e)
    {
        string i = GridView1.DataKeys[0].Value.ToString();
        SqlConnection cn = new SqlConnection();
        cn.ConnectionString = ConfigurationManager.ConnectionStrings["cn"].ConnectionString.ToString();
        cn.Open();
        SqlCommand cmd = new SqlCommand("Delete from Feedback where Clg_ID='" + i + "'", cn);
        cmd.ExecuteNonQuery();
        cmd.Dispose();
        cn.Close();
        Response.Redirect("MFeedback.aspx");
    }

0
尝试在 GridView 的 ItemDeleting 方法中将 EditIndex 设置为 -1:
protected void gv1_RowDeleting(object sender, GridViewDeleteEventArgs e) {
    gv1.EditIndex = -1;
    gv1.DataBind();
}

将删除逻辑移动到 GridView 的 RowCommand 方法中:
protected void gv1_RowCommand(object sender, GridViewDeleteEventArgs e) {
    If (e.CommandName == "Delete") {
        int instructionId = (int) gv1.DataKeys[e.RowIndex].Value;
        /// my delete logic
        CTX.SubmitChanges();
    }
}

您的删除操作控件应该类似于:

<asp:LinkButton ID="lbDel" runat="server" Text="Delete" CommandName="Delete" />

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接