【问题标题】:GridView paging control issueGridView 分页控制问题
【发布时间】:2013-05-12 17:10: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();
}
}

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

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

    让我知道结果。

    【讨论】:

    • 谢谢它的工作,但我还有另一个问题,我有 25 个按钮?_Click(null, null);事件..现在解决了吗?
    • 请重新表述您的问题,抱歉我没听懂。 :(
    • 我的意思是我还有 25 个按钮处理程序我怎么能遇到它
    • 对不起,我无法想象你想做什么,在你的问题中,你遇到的问题只是你的 gridview 的分页。
    【解决方案2】:

    删除行 换页功能中的 GridView1.DataSource=result1。

    【讨论】:

      【解决方案3】:

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

      【讨论】:

      • @DanishAhmad 我已经更新了我的答案,告诉我如何使用Session 试试这个
      猜你喜欢
      • 2013-02-12
      • 2011-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-31
      相关资源
      最近更新 更多