当我单击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();
    }
}

关于asp.net - C#分页中来自代码隐藏的GridView无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5224643/

10-12 16:21