当我单击 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();
}
但我无法让它发挥作用。 我做错了什么?
谢谢!
这是一个我尝试重新创建您的场景的示例,它有效。看看吧。
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();
}
}