I have searched around trying to find an answer to this, but have not found a solution so far. I am trying to use an EntityDataSource
with a GridView (C#). The GridView is not populated initially on Page_Load
as I want the user to select some information from dropdown list. When the user makes some selections and presses a button, I then modify the connectionstring property of the EDS to make it user-specific. This all works fine and the GridView is populated with data.
I then try to go to page #2 which work fine. The issue is when I tried to go to page #3 (this is arbitrary...I could start with 4 and try to go to 6...doesn't matter). At this point, I have watched in debug and seen that the EntityDataSource
is empty (TotalRowCount = 0). I am really don't want to redo this using a non-EntityDataSource
method but with no other choice, I will.
I also have the PageIndexChanging
firing and I'm trying to set the new index properly, so that does not seem to be the cause of the problem...I am not sure how I am losing the state of the EntityDataSource
.
Here are pieces of the code:
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'm don't understand how the EDS becomes empty or how I should maintain its state.