删除/更新后刷新DataGrid

时间:2012-10-10 12:05:36

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

设置

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

1 个答案:

答案 0 :(得分: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();
相关问题