AJAX UpdatePanel中的GridView在删除时没有刷新

时间:2012-10-29 13:27:29

标签: c# gridview updatepanel

由于某种原因,删除行后不会自动刷新gridview。当我对代码进行debbuging时,它会到达UserAccessGrid_RowDeleted方法,但页面上没有任何反应。这是我的代码。

    <ajax:UpdatePanel ID="UserAccessGridUpdatePanel" runat="server" RenderMode="Inline" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:GridView ID="UserAccessGrid" runat="server" DataKeyNames="UserID" AutoGenerateColumns="False" OnRowDeleting="UserAccessGrid_RowDeleting" OnRowDeleted="UserAccessGrid_RowDeleted"
            CellPadding="3" Width="100%">
            <RowStyle CssClass="GridRow" />
            <AlternatingRowStyle CssClass="GridAltRow" />
            <HeaderStyle HorizontalAlign="Left" />
            <Columns>     
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:LinkButton ID="DeleteButton" runat="server" CommandName="Delete" Text="Delete" OnClientClick="return confirm('Are you sure you want to delete this user?');"></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Employee Name">
                    <ItemTemplate>
                        <a href="AddUser.aspx?uid=<%# QueryStringEncryption.Encrypt( DataBinder.Eval(Container.DataItem, "UserID").ToString() )%>">
                            <%#DataBinder.Eval(Container.DataItem, "EmployeeName") %>
                        </a>
                    </ItemTemplate>                        
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Email">
                    <ItemTemplate>
                        <%#DataBinder.Eval(Container.DataItem, "EmailAddress")%>
                    </ItemTemplate>                        
                </asp:TemplateField>
                <asp:CheckBoxField 
                    DataField="IsCSEAdmin" 
                    HeaderText="CSE Administrator" 
                    ReadOnly="True" 
                    ItemStyle-HorizontalAlign="Center" >
                <ItemStyle HorizontalAlign="Center" />
                </asp:CheckBoxField>
                <asp:CheckBoxField 
                    DataField="IsAdmin" 
                    HeaderText="Country Administrator" 
                    ReadOnly="True" 
                    ItemStyle-HorizontalAlign="Center" >
                <ItemStyle HorizontalAlign="Center" />
                </asp:CheckBoxField>
            </Columns>
        </asp:GridView>
    </ContentTemplate>
    <Triggers>
        <ajax:AsyncPostBackTrigger ControlID="UserAccessGrid" />
    </Triggers>
</ajax:UpdatePanel>

代码背后:

    protected void Page_Load(object sender, EventArgs e)
{
    if (!(Master.user.IsAdmin || Master.user.IsCSEAdmin))
        Response.Redirect("Unauthorized.aspx");

    //UserAccessDS.SelectParameters["UserID"].DefaultValue = Master.user.UserID;
    if (!Page.IsPostBack)
    {
        UserAccessGridBind();
    }
}

protected void UserAccessGrid_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    int UserID = (int)UserAccessGrid.DataKeys[e.RowIndex].Value;
    if (Convert.ToInt32(Master.user.UserID) == UserID)
    {
        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Alert", "alert('Delete action permitted on that user!');", true);
    }
    else
    {
        OleDbCommand command = new OleDbCommand("dbo.usp_DeleteUserbyUserID", Master.connection);
        command.Connection.Open();

        try
        {
            command.CommandType = CommandType.StoredProcedure;
            OleDbParameter param = command.Parameters.Add("@UserID", OleDbType.Integer);
            param.Value = UserID;

            command.ExecuteNonQuery();

            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Information", "alert('Delete complete.');", true);
        }
        catch (Exception ex)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Alert", "alert('An error occured : " + ex.Message + "');", true);
        }
        finally
        {
            command.Connection.Close();
        }
    }
}

protected void UserAccessGrid_RowDeleted(object sender, GridViewDeletedEventArgs e)
{
    UserAccessGridUpdatePanel.Update();
}

private void UserAccessGridBind()
{
    int UserID = Convert.ToInt32(Master.user.UserID);

    OleDbDataAdapter adapter = new OleDbDataAdapter();
    OleDbCommand command = new OleDbCommand("dbo.usp_GetAccessListByUserID", Master.connection);

    command.CommandType = CommandType.StoredProcedure;
    command.Parameters.AddWithValue("UserID", UserID);

    adapter.SelectCommand = command;

    DataSet UserAccess = new DataSet();
    adapter.Fill(UserAccess);

    UserAccessGrid.DataSource = UserAccess;
    UserAccessGrid.DataBind();
}

1 个答案:

答案 0 :(得分:0)

删除后需要再次绑定网格。正如你之前所说的那样。

调用UserAccessGridBind()方法删除记录后刷新网格。