简体   繁体   中英

GridView inside AJAX UpdatePanel is not refreshing on delete

For some reason a gridview is not being refreshed automatically after a row has been deleted. When I'm debbuging the code it reaches UserAccessGrid_RowDeleted method but nothing happens on the page. Here's my code.

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

Code behind:

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

You need to bind the grid again after delete. As you binded earlier.

Call UserAccessGridBind() method to refresh the grid after deleting record.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM