locked
Paging GridViews in repeater/DataList RSS ?. RRS feed

  • Question

  • User780539974 posted

    Hello,

    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.

    thanks

    alexsunny

    Saturday, May 22, 2021 3:43 PM

All replies

  • User1535942433 posted

    Hi alexsunny123,

    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.

    <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>
    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;
    
            }

    There is a same problem with you.You could refer:

    https://forums.asp.net/t/2170448.aspx?Paging+GridViews+in+repeater+DataList

    Best regards,

    Yijing Sun

    Monday, May 24, 2021 5:35 AM