0

设置

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

4

1 回答 1

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();
于 2012-10-10T13:47:51.710 回答