【问题标题】:Refreshing a DataGrid after delete/update删除/更新后刷新 DataGrid
【发布时间】:2012-10-10 12:05:36
【问题描述】:

设置

我有一个应用程序页面,我尝试将其部署到 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 的对象,还是正在向用户发送回调在这些对象更新之后。

【问题讨论】:

    标签: asp.net linq-to-sql sharepoint-2010 datagrid linqdatasource


    【解决方案1】:

    您是否尝试过创建一个将数据源绑定到网格的方法并在您进行更改后调用该方法?

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

    【讨论】:

    • 如果我这样做,它确实有效,但这会在 ASP 中引入另一个令人讨厌的缺点:如果 GridView 没有设置 DataSourceID(没有线索为什么)。不过,我可以通过使用 rowindex 来解决它,但即便如此,更新也会很痛苦。它也破坏了我的分页。
    • 我想我遇到了您所说的寻呼问题。我解决分页问题的方法是奇怪地执行 DataBind() 两次。它以什么方式破坏了您的分页?不确定这是否可以解决您的分页问题。奇怪的是,您无法检索密钥的数据。您可能可以像您所说的那样使用行索引。 EntitiesGrid.SelectedRow.RowIndex.ToString();
    • 是的,这就是我目前正在尝试的方法。分页中断是因为以前数据源为我处理了这个问题。如果我将网格绑定到一个对象,我将不得不自己创建分页和排序方法。这一切都是因为 LinqDataSource 的行为有点怪异。
    • 只是一个更新:我设法通过直接绑定到我的实体而不是使用 LinqDataSource 来让一切正常工作。我必须自己进行排序、过滤、分页、更新、删除和插入,但至少现在一切正常。我已经继续并标记了您的答案。
    猜你喜欢
    • 2013-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-08
    • 2012-09-28
    • 1970-01-01
    相关资源
    最近更新 更多