GridView分页控制问题

时间:2013-05-12 07:50:05

标签: c# asp.net

我正在使用asp.net的网格视图网页控件,即时通讯使用分页控制,我处理了分页事件,但如果我点击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();
}
}

3 个答案:

答案 0 :(得分:0)

原因是当您点击链接页面回发时,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; 
}
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();
}

答案 1 :(得分:0)

删除行 页面更改功能中的GridView1.DataSource = result1。

答案 2 :(得分:0)

将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。

让我知道结果。