使用与GridView的Paging时,EntityDataSource为空

时间:2013-01-28 17:09:31

标签: c# asp.net entity-framework gridview entitydatasource

我一直在寻找答案,但到目前为止还没找到解决办法。我试图使用带有GridView(C#)的EntityDataSource。 GridView最初未在Page_Load上填充,因为我希望用户从下拉列表中选择一些信息。当用户做出一些选择并按下按钮时,我然后修改EDS的connectionstring属性以使其成为用户特定的。这一切都运行正常,GridView填充了数据。

然后我尝试去第2页工作正常。问题是当我试图去第3页时(这是任意的......我可以从4开始尝试去6 ...无关紧要)。此时,我已经在调试中看到EntityDataSource为空(TotalRowCount = 0)。我真的不想使用non-EntityDataSource方法重做这个,但我没有别的选择。

我也有PageIndexChanging解雇,我正在尝试正确设置新索引,所以这似乎不是问题的原因......我不确定我是如何失去国家EntityDataSource

以下是代码片段:

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
        EntityDataSource ed2 = (EntityDataSource)this.lv2.FindControl("EntityDataSource2");
        GridView gv1 = (GridView)this.lv2.FindControl("GridView1");
        gv1.DataSourceID = null;
        gv1.DataSource = ed2;
        gv1.DataBind(); 
        gv1.PageIndex = e.NewPageIndex;  
    }

<asp:EntityDataSource ID="EntityDataSource2" runat="server" ConnectionString="name=MAS_CMLEntities1" DefaultContainerName="MAS_CMLEntities1" EnableFlattening="False" EntitySetName="CI_Item" AutoGenerateWhereClause="false"
Select="it.[ItemCode], it.[ItemCodeDesc], it.[LastSoldDate], it.[UDF_Manufact], it.[Category2], it.[SalesUMConvFctr], it.[DefaultWarehouseCode], it.[TotalQuantityOnHand], it.[SalesUMConvFctr]" 
Where="it.[ItemCode] LIKE @siteFilter" OrderBy="it.[ItemCode]" 
ViewStateMode="Inherit" onselected="EntityDataSource2_Selected">
<WhereParameters>
<asp:Parameter Name="siteFilter" Type="String" />
</WhereParameters>
</asp:EntityDataSource> 

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ItemCode" 
        DataSourceID="EntityDataSource2" CellPadding="20"
        GridLines="None" CssClass="mGrid" PagerStyle-CssClass="pgr" 
        AlternatingRowStyle-CssClass="alt" CellSpacing="20" AllowPaging="True" 
        AllowSorting="True" onpageindexchanging="GridView1_PageIndexChanging" 
        onpageindexchanged="GridView1_PageIndexChanged" PageSize="15"
        onrowcommand="GridView1_RowCommand">
    <AlternatingRowStyle CssClass="alt"></AlternatingRowStyle>
        <Columns>
            <asp:BoundField DataField="ItemCode" HeaderText="ItemCode" ReadOnly="True" 
                SortExpression="ItemCode" />
            <asp:BoundField DataField="ItemCodeDesc" HeaderText="Desc" ReadOnly="True" 
                SortExpression="ItemCodeDesc" />
            <asp:BoundField DataField="UDF_Manufact" HeaderText="MFG" ReadOnly="True" 
                SortExpression="UDF_Manufact" />
            <asp:BoundField DataField="Category2" HeaderText="Container" ReadOnly="True" 
                SortExpression="Category2" />
            <asp:BoundField DataField="SalesUMConvFctr" HeaderText="ContainerUOM" ReadOnly="True" 
                SortExpression="SalesUMConvFctr" />
            <asp:BoundField DataField="DefaultWarehouseCode" HeaderText="WhseCode" ReadOnly="True" 
                SortExpression="DefaultWarehouseCode" />
            <asp:BoundField DataField="TotalQuantityOnHand" HeaderText="QtyOnHand" ReadOnly="True" 
                SortExpression="TotalQuantityOnHand" />
            <asp:BoundField DataField="SalesUMConvFctr" HeaderText="Units" ReadOnly="True" 
                SortExpression="SalesUMConvFctr" />
            <asp:ButtonField ButtonType="Button" CommandName="AddToCart" 
                HeaderText="Add To Request" Text="Add" >
                <ItemStyle HorizontalAlign="Center" />
            </asp:ButtonField>
        </Columns>
    <PagerStyle CssClass="pgr"></PagerStyle>
</asp:GridView>  

我在这里做错了什么?我不明白EDS如何变空或我应该如何保持其状态。

0 个答案:

没有答案