当我点击>在我的GridView上,它不会转到下一组记录。
DataGrid dataGrid = new DataGrid();
dataGrid.PageSize = 5;
dataGrid.AllowPaging = true;
dataGrid.EnableViewState = true;
dataGrid.DataSource = customerDataTable;
dataGrid.AllowPaging ();
if (!IsPostBack)
{
dataGrid.DataBind();
}
根据我的代码,它会保留在前5个或网格不显示。
我已经在IsPostBack内外尝试了DataBind()。
我也尝试过添加
dataGrid.PageIndexChanged += new DataGridPageChangedEventHandler(dataGrid_PageIndexChanged);
和
void dataGrid_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
{
DataGrid dg = (DataGrid)source;
dg.DataBind();
}
但我不能让这个工作。我做错了什么?
谢谢!
答案 0 :(得分:8)
以下是我尝试重新创建场景的示例,它可以正常运行。看看吧。
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">
编辑:
以下是使用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();
}
}