2012-10-10 154 views
0

的設置刪除/更新後刷新一個DataGrid

我有一個應用程序頁面,我嘗試部署到SharePoint 2010的網頁包含SPGridView和像這樣的的LinqDataSource:

<asp:UpdatePanel ID="Update" runat="server" > 
    <ContentTemplate> 
     <center> 
      <asp:LinqDataSource runat="server"      
        ID="EntitiesSource" 
        onSelecting="EntitiesSource_Selecting"         
        EnableDelete="true" 
        EnableUpdate="true" 
        EnableInsert="true" /> 

      <SharePoint:SPGridView runat="server" 
        ID="EntitiesGrid" 
        AutoGenerateColumns="false" 
        DataKeyNames="Key" 
        FilteredDataSourcePropertyName="Where" 
        FilteredDataSourcePropertyFormat='{1} == "{0}"' 
        OnRowDeleting="EntitiesGrid_RowDeleting" 
        OnRowUpdating="EntitiesGrid_RowUpdating" 
        AllowPaging="true" 
        AllowSorting="true" 
        PageSize="20" 
        ShowFooter="true" 
        DataSourceID="EntitiesSource"> 

       <pagersettings mode="Numeric" 
         position="Bottom"   
         pagebuttoncount="20"/> 

       <pagerstyle backcolor="Control" 
         verticalalign="Bottom" 
         horizontalalign="Center"/> 

       <Columns>        

        <asp:CommandField HeaderText="Actions" ButtonType="Link" ShowEditButton="true" ShowDeleteButton="true" ShowCancelButton="true" /> 
        <asp:BoundField HeaderText="Key" DataField="Key" SortExpression="Key" /> 
        <asp:BoundField HeaderText="Var a" DataField="A" SortExpression="A" /> 
        <asp:BoundField HeaderText="Var b" DataField="B" SortExpression="B" /> 
        <asp:BoundField HeaderText="Var c" DataField="C" SortExpression="C" /> 

       </Columns>          

      </SharePoint:SPGridView> 
     </center> 

    </ContentTemplate> 
</asp:UpdatePanel> 

後面的代碼如下所示:

public partial class EntitiesPage: LayoutsPageBase 
{ 
    private MyContext _dbcontext; 
    private MyContext DBContext 
    { 
     get 
     { 
      if (_dbcontext == null) 
      { 
       string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["entitiesConnectionString"].ConnectionString; 
       _dbcontext = new MyContext(connectionString); 
      } 

      return _dbcontext; 
     } 
    } 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     EntitiesGrid.PagerTemplate = null; 
    } 

    protected void EntitiesGrid_RowDeleting(object sender, GridViewDeleteEventArgs e) 
    {   
     string key = (string)e.Keys[0]; 
     DBContext.RemoveEntity(key); 
     DBContext.SubmitChanges(); 

     //the entity is gone from the context now    
     EntitiesGrid.DataBind();      
    } 

    protected void EntitiesGrid_RowUpdating(object sender, GridViewUpdateEventArgs e) 
    { 
     string key = (string)e.Keys["Key"]; 
     string newKey = (string)e.NewValues["Key"]; 

     if (string.Equals(key, newKey)) 
     { 
      Entity entity = DBContext.GetEntity(key); 
      entity.A = (string)e.NewValues["A"]; 
      entity.B = (string)e.NewValues["B"]; 
      entity.C = (string)e.NewValues["C"]; 
      DBContext.SubmitChanges(); 
     } 
     else 
     { 
      //We need to remove the old one and make a new one since we can't edit the key 
      DBContext.RemoveEntity(key); 
      DBContext.AddEntity(new Entity{ Key = newKey, A = (string)e.NewValues["A"], B = (string)e.NewValues["B"], C = (string)e.NewValues["C"] }); 
      DBContext.SubmitChanges(); 
     } 
    } 

    protected void EntitiesSource_Selecting(object sender, LinqDataSourceSelectEventArgs e) 
    { 
     e.Result = DBContext.Entities; 
    } 

'MyContext'類是繼承DataContext並使用linq2sql的自定義類。它有我自己的實體類的單個表。

的問題從數據庫

讀取數據,排序和分頁作品真的很好,真的很快。我的網格更改,無需重新加載頁面。但是,當我更新或刪除一行時,我需要手動刷新,然後才能看到對數據庫所做的更改。我相信我所做的更改會立即執行到DataContext和數據庫。這裏缺少的鏈接似乎是要刷新DataSource或GridView(或兩者)。

調試這是一個痛苦,我不能告訴(或者我不知道該怎麼告訴)問題是更新我的DataSource/GridView的對象,還是發送回一個調用回到這些對象更新後的用戶。

回答

0

您是否嘗試創建一個方法,將數據源綁定到網格並在做出更改後調用該方法?

// Create a method for binding the data 
public void bindTheGrid() 
{ 
    EntitiesGrid.Datasource = variables; //This is whatever your properties are etc 
    EntitiesGrid.DataBind(); 
} 

//Call the method after you succesffuly make changes to the database etc 
bindTheGrid(); 
+0

如果我那樣做,它的工作,但這介紹ASP另一個討厭的缺點:我的GridView的EventArgs包含鍵或值沒有數據,如果GridView控件不具有DataSourceID的集(不知道爲什麼)。儘管如此,我可以通過使用rowindex來解決它,但即使如此,更新也是一件痛苦的事情。它也打破了我的分頁。 –

+0

我想我已經遇到了你在談論的分頁問題。我解決我的分頁問題的方式是通過兩次奇怪地執行DataBind()。它以什麼方式破壞你的分頁?不知道這是否可以解決你的分頁問題。這很奇怪,你無法檢索密鑰的數據。儘管可能,您也許可以使用像您所述的行索引。 EntitiesGrid.SelectedRow.RowIndex.ToString(); –

+0

是的,這是我目前正在嘗試的方法。 由於之前數據源爲我處理了這個問題,所以分頁就打破了。如果我將網格綁定到一個對象,我將不得不自己創建分頁和排序方法。所有這些都是因爲LinqDataSource有點奇怪。 –