Answered by:
Paging GridViews in repeater/DataList

Question
-
User899768610 posted
Hi everyone,
I have to create N tables from a DB table. so I am trying to use a repeater, but I cannot get pagination to work. Simplifying:
<asp:Repeater ID="rep1" runat="server"> <ItemTemplate> <asp:GridView runat="server" AutoGenerateColumns="true" PageSize = "3" OnPageIndexChanging="GridViewPageIndexChanging" AllowPaging="true" DataSource='<%# DataBinder.Eval(Container.DataItem, "data") %>'> </asp:GridView> </ItemTemplate> </asp:Repeater>
and
protected void LinkButton2_Click(object sender, EventArgs e) { DataTable dt = new DataTable(); dt.Columns.Add("id", typeof(string)); dt.Columns.Add("data", typeof(DataTable)); dt.Rows.Add("1", getData1("1")); //this return an example DataTable dt.Rows.Add("2", getData1("2")); rep1.DataSource = dt; rep2.DataBind(); } protected void GridViewPageIndexChanging(object sender, GridViewPageEventArgs e) { GridView grid = (GridView)sender; //grid.DataSource = getData1("2"); grid.PageIndex = e.NewPageIndex ; grid.DataBind(); }
Clicking on the button (LinkButton2_Click) the gridviews appears, but when you click on a page of a gridview, that gridview disappears. As you can see I also tried to reassign the datasource again without success (it also disappears)
I searched the internet but didn't find much ... or at least anything that worked.
Any suggestions?thanks
Thursday, September 3, 2020 7:47 AM
Answers
-
User-1330468790 posted
Hi Boots81,
The problem is located in below Data Binding inline code:
DataSource='<%# DataBinder.Eval(Container.DataItem, "data") %>'
As you may know, this method will be called when the control calls Databind() method. However, when you change the page index, the method will no be executed and cause data loss.
You could refer to below codes to explicitly bind the data for child grid view controls and implement paging for them.
.aspx:
<form id="form1" runat="server"> <div> <asp:Repeater ID="rep1" runat="server" OnItemDataBound="rep1_ItemDataBound"> <ItemTemplate> <asp:HiddenField ID="hf_tableId" runat="server" Value='<%# Eval("id") %>' /> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true" PageSize="3" OnPageIndexChanging="GridViewPageIndexChanging" AllowPaging="true" > <PagerSettings Mode="Numeric" Position="Bottom" PageButtonCount="10" /> <PagerStyle BackColor="LightBlue" Height="30px" VerticalAlign="Bottom" HorizontalAlign="Center" /> </asp:GridView> </ItemTemplate> </asp:Repeater> <asp:LinkButton ID="LinkButton2" runat="server" Text="LinkButton" OnClick="LinkButton2_Click"></asp:LinkButton> </div> </form>
Code behind:
protected void Page_Load(object sender, EventArgs e) { } protected void LinkButton2_Click(object sender, EventArgs e) { BindRepeater(); } private void BindRepeater() { DataTable dt = new DataTable(); dt.Columns.Add("id", typeof(string)); dt.Columns.Add("data", typeof(DataTable)); dt.Rows.Add("1", getData1("1")); //this return an example DataTable dt.Rows.Add("2", getData1("2")); rep1.DataSource = dt; rep1.DataBind(); } protected void GridViewPageIndexChanging(object sender, GridViewPageEventArgs e) { // Get tableId from current repeater item RepeaterItem item = ((GridView)sender).Parent as RepeaterItem; string tableId = ((HiddenField)rep1.Items[item.ItemIndex].FindControl("hf_tableId")).Value; // Rebind data for current GridView GridView grid = (GridView)sender; grid.PageIndex = e.NewPageIndex; grid.DataSource = getData1(tableId); grid.DataBind(); } protected void rep1_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { // Get table Id from current item string tableId = ((HiddenField)e.Item.FindControl("hf_tableId")).Value; // Bind data for current GridView GridView GridView1 = e.Item.FindControl("GridView1") as GridView; GridView1.DataSource = getData1(tableId); GridView1.DataBind(); } } protected DataTable getData1(string index) { DataTable dt = new DataTable(); switch (index) { case "1": dt = DataForTable1(); break; case "2": dt = DataForTable2(); break; default: break; } return dt; } protected DataTable DataForTable1() { DataTable dt = new DataTable(); dt.Columns.Add("Id", typeof(string)); dt.Columns.Add("Name", typeof(string)); dt.Rows.Add("1", "Name1"); dt.Rows.Add("2", "Name2"); dt.Rows.Add("3", "Name3"); dt.Rows.Add("4", "Name4"); dt.Rows.Add("5", "Name5"); dt.Rows.Add("6", "Name6"); dt.Rows.Add("7", "Name7"); return dt; } protected DataTable DataForTable2() { DataTable dt = new DataTable(); dt.Columns.Add("Id", typeof(string)); dt.Columns.Add("Location", typeof(string)); dt.Rows.Add("1", "Location1"); dt.Rows.Add("2", "Location2"); dt.Rows.Add("3", "Location3"); dt.Rows.Add("4", "Location4"); dt.Rows.Add("5", "Location5"); dt.Rows.Add("6", "Location6"); dt.Rows.Add("7", "Location7"); dt.Rows.Add("8", "Location8"); dt.Rows.Add("9", "Location9"); dt.Rows.Add("10", "Location10"); dt.Rows.Add("11", "Location11"); dt.Rows.Add("12", "Location12"); return dt; }
Demo:
Hope this can help you.
Best regards,
Sean
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, September 3, 2020 11:28 AM
All replies
-
User-1330468790 posted
Hi Boots81,
The problem is located in below Data Binding inline code:
DataSource='<%# DataBinder.Eval(Container.DataItem, "data") %>'
As you may know, this method will be called when the control calls Databind() method. However, when you change the page index, the method will no be executed and cause data loss.
You could refer to below codes to explicitly bind the data for child grid view controls and implement paging for them.
.aspx:
<form id="form1" runat="server"> <div> <asp:Repeater ID="rep1" runat="server" OnItemDataBound="rep1_ItemDataBound"> <ItemTemplate> <asp:HiddenField ID="hf_tableId" runat="server" Value='<%# Eval("id") %>' /> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true" PageSize="3" OnPageIndexChanging="GridViewPageIndexChanging" AllowPaging="true" > <PagerSettings Mode="Numeric" Position="Bottom" PageButtonCount="10" /> <PagerStyle BackColor="LightBlue" Height="30px" VerticalAlign="Bottom" HorizontalAlign="Center" /> </asp:GridView> </ItemTemplate> </asp:Repeater> <asp:LinkButton ID="LinkButton2" runat="server" Text="LinkButton" OnClick="LinkButton2_Click"></asp:LinkButton> </div> </form>
Code behind:
protected void Page_Load(object sender, EventArgs e) { } protected void LinkButton2_Click(object sender, EventArgs e) { BindRepeater(); } private void BindRepeater() { DataTable dt = new DataTable(); dt.Columns.Add("id", typeof(string)); dt.Columns.Add("data", typeof(DataTable)); dt.Rows.Add("1", getData1("1")); //this return an example DataTable dt.Rows.Add("2", getData1("2")); rep1.DataSource = dt; rep1.DataBind(); } protected void GridViewPageIndexChanging(object sender, GridViewPageEventArgs e) { // Get tableId from current repeater item RepeaterItem item = ((GridView)sender).Parent as RepeaterItem; string tableId = ((HiddenField)rep1.Items[item.ItemIndex].FindControl("hf_tableId")).Value; // Rebind data for current GridView GridView grid = (GridView)sender; grid.PageIndex = e.NewPageIndex; grid.DataSource = getData1(tableId); grid.DataBind(); } protected void rep1_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { // Get table Id from current item string tableId = ((HiddenField)e.Item.FindControl("hf_tableId")).Value; // Bind data for current GridView GridView GridView1 = e.Item.FindControl("GridView1") as GridView; GridView1.DataSource = getData1(tableId); GridView1.DataBind(); } } protected DataTable getData1(string index) { DataTable dt = new DataTable(); switch (index) { case "1": dt = DataForTable1(); break; case "2": dt = DataForTable2(); break; default: break; } return dt; } protected DataTable DataForTable1() { DataTable dt = new DataTable(); dt.Columns.Add("Id", typeof(string)); dt.Columns.Add("Name", typeof(string)); dt.Rows.Add("1", "Name1"); dt.Rows.Add("2", "Name2"); dt.Rows.Add("3", "Name3"); dt.Rows.Add("4", "Name4"); dt.Rows.Add("5", "Name5"); dt.Rows.Add("6", "Name6"); dt.Rows.Add("7", "Name7"); return dt; } protected DataTable DataForTable2() { DataTable dt = new DataTable(); dt.Columns.Add("Id", typeof(string)); dt.Columns.Add("Location", typeof(string)); dt.Rows.Add("1", "Location1"); dt.Rows.Add("2", "Location2"); dt.Rows.Add("3", "Location3"); dt.Rows.Add("4", "Location4"); dt.Rows.Add("5", "Location5"); dt.Rows.Add("6", "Location6"); dt.Rows.Add("7", "Location7"); dt.Rows.Add("8", "Location8"); dt.Rows.Add("9", "Location9"); dt.Rows.Add("10", "Location10"); dt.Rows.Add("11", "Location11"); dt.Rows.Add("12", "Location12"); return dt; }
Demo:
Hope this can help you.
Best regards,
Sean
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, September 3, 2020 11:28 AM -
User899768610 posted
Hi Sean
Thank you very much! your solution works like a charm. I am new to asp ...
Thursday, September 3, 2020 2:05 PM