2012-10-29 103 views
1

由於某種原因,在刪除某行後沒有自動刷新gridview。當我去掉代碼時,它會到達UserAccessGrid_RowDeleted方法,但頁面上沒有任何反應。這是我的代碼。AJAX UpdatePanel中的GridView在刪除時不會刷新

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

回答

0

你需要經過電網再次綁定刪除。正如你前面所說的那樣。

調用UserAccessGridBind()刪除記錄後刷新網格的方法。