GridView删除不重新绑定

时间:2011-08-15 13:07:02

标签: asp.net gridview updatepanel

我有一个带有删除模板字段的GridView:

<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
    <asp:GridView ID="gvCurrentDay" CssClass="gridview" OnRowCommand="gvCurrentDay_RowCommand" AutoGenerateColumns="false" runat="server">
        <Columns>
            <asp:BoundField DataField="ClientName" HeaderText="Client" />
            <asp:BoundField DataField="ProjectTitle" HeaderText="Project" />
            <asp:BoundField DataField="TimeToAdd" HeaderText="Time Allocated" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" />
            <asp:TemplateField ItemStyle-HorizontalAlign="Center">
                <ItemTemplate>
                    <asp:ImageButton ID="imbDeleteRow" ImageUrl="~/images/icons/DeleteRed.png" CommandArgument='<%# Eval("RecordID") %>' CommandName="Delete" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>    
</ContentTemplate>

当按下按钮并从数据库中删除数据库条目时,代码会运行,但GridView不会重新绑定,这是控制删除的代码:

protected void gvCurrentDay_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Delete")
    {
        int RecordID = Convert.ToInt32(e.CommandArgument);

        tdl objTdl = new tdl();
        objTdl.DeleteEntryForDay(RecordID);

        GetItineryForDay(Convert.ToDateTime(txtCalendar.Text));

        lblMessages.Text = "Entry removed";
    }
}

这是GetItineryForDay程序的第一部分:

protected void GetItineryForDay(DateTime SelectedDate)
{
    gvCurrentDay.DataSource = null;
    gvCurrentDay.DataBind();

    tdl objTdl = new tdl();
    DataTable dt = objTdl.GetUsersProjectTimeForDay(SelectedDate, Convert.ToInt32(Request.Cookies["staffid"].Value));

    if (dt.Rows.Count > 0)
    {
        DataRow row = dt.Rows[0];
        int TotalTime2 = Convert.ToInt32(row[7]);
        string TotalTime = row[7].ToString() + " minutes";

        gvCurrentDay.DataSource = dt;
        gvCurrentDay.DataBind();

您能从代码中看到GridView未更新的原因吗? GridView位于UpdatePanel中。

1 个答案:

答案 0 :(得分:1)

不使用"Delete"作为CommandName,而是使用其他内容,例如"ManualDelete""Delete"通常会被RowDeletingRowDeleted事件所困。

或者,由于“数据库条目已从数据库中删除”,您可以将重新绑定的代码放在RowDeleted事件中。

protected void gvCurrentDay_RowDeleted(object sender, GridViewDeletedEventArgs e)
{
    GetItineryForDay(Convert.ToDateTime(txtCalendar.Text));
    lblMessages.Text = "Entry removed";
}