0

我想在 gridview 上创建一个动态下拉列表,其中每页的项目可能取决于行数和初始页面大小。我怎么能那样做?提前谢谢。我有一个旧方法,但每次回发都会丢失第一个项目。谢谢!

if (e.Row.RowType == DataControlRowType.Pager)
            {
                int initialPageSize = PageSize;
                int pageSizeItems = 5;
                int pageSizeDelta = initialPageSize;
                string pageSizeLiteral = "Num. de resultados";

                if (RowCount > initialPageSize)
                {
                    pageSizeItems = RowCount / pageSizeDelta + 1;

                    while (pageSizeItems > 10)
                    {
                        pageSizeDelta = pageSizeDelta * 2;
                        pageSizeItems = RowCount / pageSizeDelta + 1;
                    }

                    DropDownList ddl = new DropDownList();
                    int i = 0;

                    if (pageSizeDelta != initialPageSize)
                    {
                        ddl.Items.Add(initialPageSize.ToString());
                    }

                    for (i = 1; i <= pageSizeItems; i++)
                    {
                        ddl.Items.Add((i * pageSizeDelta).ToString());
                    }

                    ddl.AutoPostBack = true;
                    ListItem li = ddl.Items.FindByText(this.MyGridView.PageSize.ToString());
                    if (li != null)
                    {
                        ddl.SelectedIndex = ddl.Items.IndexOf(li);
                    }

                    ddl.SelectedIndexChanged += new EventHandler(MyData_SelectedIndexChanged);
                    ddl.ID = "comboboxpager";
                    Table pagerTable = e.Row.Cells[0].Controls[0] as Table;
                    TableCell cell = new TableCell();
                    cell.Attributes.Add("class", "csspager");
                    cell.Controls.Add(new LiteralControl(pageSizeLiteral));
                    cell.Controls.Add(ddl);
                    pagerTable.Rows[0].Cells.Add(cell);
                }
            }



        protected void MyData_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.MyGridView.PageSize = int.Parse(((DropDownList)sender).SelectedValue);
            if (this.FindControl("comboboxpager") != null)
            {
                DropDownList comboboxpager = (DropDownList)this.FindControl("comboboxpager");
                comboboxpager.SelectedIndex = this.MyGridView.PageSize;
            }
            this.MyGridView.PageIndex = 0;
            PageSize = this.MyGridView.PageSize;
            DataBind(this.MyGridView);



        }
4

0 回答 0