1

由于某种原因,在删除一行后,gridview 不会自动刷新。当我调试代码时,它会到达 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();
}
4

1 回答 1

0

删除后需要重新绑定网格。正如你之前绑定的那样。

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

于 2012-10-29T13:29:30.603 回答