我想在gridview上创建一个动态下拉列表,其中每页的项目可能取决于行计数和内部页面大小。我怎么能这样做? Thakns提前。我有一个旧的方法,但每次做回发都会丢失第一个项目。 谢谢!
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);
}