GridView分页控制问题

本文关键字:问题 控制 分页 GridView | 更新日期: 2023-09-27 18:13:15

我使用asp.net的网格视图web控件,我在其中使用分页控件,我在其中处理分页事件,但它不显示数据,如果我点击2,3,4…所以在链接上,它不显示一个结果集…它不给出任何异常,只是显示除了第一页以外的任何东西。下面是代码:

public partial class Main : System.Web.UI.Page
{
protected string PostBackOption = "";
protected void Page_Load(object sender, EventArgs e)
{
    this.check.Text = " ";
    if (Page.IsPostBack)
    {
        PostBackOption = "$('"#dialog'").dialog('"open'");";
    }
}
List<Allemployees> result1 = new List<Allemployees>();
protected void Button1_Click(object sender, EventArgs e)
{ // Show all the employees currently in the table
    GridView1.DataSource = null;
    GridView1.DataBind();
    using (var db = new AdventureWorks2012_DataEntities())
    {
        List<Allemployees> result = new List<Allemployees>();
        var query = from b in db.Employees
                    join p in db.People on b.BusinessEntityID equals p.BusinessEntityID
                    orderby p.BusinessEntityID
                    select new
                    {
                        b.BusinessEntityID,
                        p.FirstName,
                        p.LastName
                    };
        foreach (var item in query)
        {
            Allemployees t1 = new Allemployees();
            t1.BusinessEntityId = item.BusinessEntityID;
            t1.FirstName = item.FirstName;
            t1.LastName = item.LastName;
            result1.Add(t1);
        }
     GridView1.DataSource = result1;
        GridView1.DataBind();
    } 
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    GridView1.DataSource = result1;
    GridView1.DataBind();
}
}

GridView分页控制问题

将GridView1_PageIndexChanging更改为:

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView gridview = (GridView)sender;
    gridview.PageIndex = e.NewPageIndex;
    Button1_Click(null, null);
    gridview.DataBind();
}

确保将GridView1 AllowPaging设置为true。

让我知道结果。

remove lineGridView1。

原因是当您点击链接页面回发时,result1将没有任何值。如果你想在回发时保留数据,你必须使用viewstate, session等。

Button1_Click中做如下

protected void Button1_Click(object sender, EventArgs e)
{ 
    // your code 
    GridView1.DataBind(); // add below line after this line
    Session["Data"] = result1; 
}

in GridView1_PageIndexChanging

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    var result = (List<Allemployees>)Session["Data"];
    GridView1.PageIndex = e.NewPageIndex;
    GridView1.DataSource = result;
    GridView1.DataBind();
}