2012-10-10 152 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有点奇怪。 –