EntityDataSource is empty when paging with GridView
I have searched trying to find an answer to this question but have not found a solution yet. I am trying to use EntityDataSource
with a GridView (C #). Initially the GridView is not populated on Page_Load
as I want the user to select some information from the dropdown. When the user makes some selections and clicks a button, I then change the connectionstring property for the EDS to make it user specific. This all works great and the GridView is filled with data.
Then I will try to go to page # 2 which works fine. The problem is when I tried to go to page # 3 (this is arbitrary ... I could start at 4 and try to go to 6 ... doesn't matter). At this point, I watched in debug and saw that it is EntityDataSource
empty (TotalRowCount = 0). I really don't want to repeat this with a method non-EntityDataSource
, but with no other choice.
I also have an arrow PageIndexChanging
and I am trying to set the new index correctly, so this is not the cause of the problem ... I am not sure how I am losing state EntityDataSource
.
Here are the code snippets:
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>
What am I doing wrong here? I do not understand how the EDS becomes empty or how I should maintain its state.
source to share
No one has answered this question yet
Check out similar questions: