Вот пример, который я пытался воссоздать по вашему сценарию, и он работает. Проверьте это.
protected void Page_Load(object sender, EventArgs e)
{
GridView GridView1 = new GridView();
Panel1.Controls.Add(GridView1);
GridView1.DataSource = GetList();
GridView1.AutoGenerateColumns = true;
GridView1.EnableViewState = true;
GridView1.AllowPaging = true;
GridView1.PageSize = 4;
GridView1.DataBind();
GridView1.PageIndexChanging += new GridViewPageEventHandler(GridView1_PageIndexChanging);
}
void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
if (sender != null)
{
GridView GridView1 = sender as GridView;
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}
}
public class Person
{
public String Name { get; set; }
public int Age { get; set; }
}
private IEnumerable<Person> GetList()
{
List<Person> list = new List<Person>();
list.Add(new Person() {Age = 12, Name = "asdfsd"});
list.Add(new Person() {Age = 13, Name = "sdfsdaf"});
list.Add(new Person() {Age = 14, Name = "zxczxv"});
list.Add(new Person() { Age = 15, Name = "zxczxv" });
list.Add(new Person() { Age = 16, Name = "zxczxv" });
list.Add(new Person() { Age = 17, Name = "zxczxv" });
return list;
}
и в разметке все, что вам нужно, это иметь панель
<asp:Panel ID="Panel1" runat="server">
EDIT:
Вот тот же сценарий с использованием DataGrid
protected void Page_Load(object sender, EventArgs e)
{
DataGrid dataGrid = new DataGrid();
Panel1.Controls.Add(dataGrid);
dataGrid.DataSource = GetList();
dataGrid.AutoGenerateColumns = true;
dataGrid.EnableViewState = true;
dataGrid.AllowPaging = true;
dataGrid.PageSize = 4;
dataGrid.DataBind();
dataGrid.PageIndexChanged +=new DataGridPageChangedEventHandler(dataGrid_PageIndexChanged);
}
void dataGrid_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
{
if (source != null)
{
DataGrid dataGrid = source as DataGrid;
dataGrid.CurrentPageIndex = e.NewPageIndex;
dataGrid.DataBind();
}
}