0

我正在使用 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();
}
}
4

3 回答 3

0

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

于 2013-05-12T08:46:05.783 回答
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。

让我知道结果。

于 2013-05-12T08:49:33.237 回答
0

原因是当你点击链接页面回发result1时不会有任何价值。如果您想在回发时保留数据,则必须使用视图状态、会话等。

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();
}
于 2013-05-12T08:29:25.450 回答