当前位置:编程学习 > C#/ASP.NET >>

第一次使用AspNetPager控件 但是翻页后 页面上无数据,请高手帮忙看下

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using manage.program;
using manage.aortorsTableAdapters;
using System.Data;
using System.Data.SqlClient;

namespace manage
{
    public partial class Teacherchange : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {


            if (!Page.IsPostBack)
            {

                if (Session["admin"] == null || (string)Session["admin"] == "")
                {
                    Response.Write("您还未登录!");
                    Response.Redirect("~/Login.aspx");
                }
                else
                {
                    ListBind();

                    (Master.FindControl("labUser") as Label).Text = Session["admin"].ToString();



                }

            }

        }

        protected void update_Click(object sender, ImageClickEventArgs e)
        {
            communityTableAdapter update = new communityTableAdapter();
            update.UpdateteacherQuery(teahcer.Text, community2.Text);
            update.UpdateteacherQuery(DropDownList1.SelectedValue, community1.Text);
            Response.Write("更换成功");

        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string co = DropDownList1.SelectedValue;
            communityTableAdapter update = new communityTableAdapter();

            community2.Text = update.GetDataByteacher(co).Rows[0][0].ToString();






        }

        protected void teacherddl_OnItemDataBound(object source, DataListCommandEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                Label l = e.Item.FindControl("Label1") as Label;
                l.Text = DataBinder.Eval(e.Item.DataItem, "teacher").ToString();


                Button b = e.Item.FindControl("loginSuccess") as Button;
                b.CommandName = DataBinder.Eval(e.Item.DataItem, "teacher").ToString();

            }
        }

        protected void loginSuccess_Click(object sender, EventArgs e)
        {
            Button b = sender as Button;
            teahcer.Text = b.CommandName;
        }
        private void ListBind()
        {
            string sql = "SELECT community, teacher FROM community WHERE (psss =1)";//自定义的SQL语句
            int recordcount;
            DataSet ds = GetPage(sql, this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out recordcount);
            this.AspNetPager1.RecordCount = recordcount;
            

            //.net的自定义分页
            PagedDataSource pds = new PagedDataSource();
            pds.AllowPaging = true;
            pds.PageSize = AspNetPager1.PageSize;
            pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
            pds.DataSource = ds.Tables[0].DefaultView;
            teacherddl.DataSource = pds;
            teacherddl.DataBind();
            AspNetPager1.CustomInfoHTML = "记录总数:<b>" + AspNetPager1.RecordCount.ToString() + "</b>";
            AspNetPager1.CustomInfoHTML += " 总页数:<b>" + AspNetPager1.PageCount.ToString() + "</b>";
            AspNetPager1.CustomInfoHTML += " 当前页:<font><b>" + AspNetPager1.CurrentPageIndex.ToString() + "</b></font>";


        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            ListBind();
        }

        public DataSet GetPage(string sql, int currentPage, int pagesize, out int recordcount)//获取页面数据
        {
            SqlConnection cn = new SqlConnection("Data Source = CX-PC\\HAPPY;Initial Catalog =Community management;User ID=sa;");
            SqlDataAdapter ada = new SqlDataAdapter(sql, cn);
            DataSet ds = new DataSet();
            int startRow = (currentPage - 1) * pagesize;
            ada.Fill(ds, startRow, pagesize, "community");
            recordcount = GetPageRecord(sql);
            return ds;
        }

        public int GetPagerCount(string sql, int pagesize)
        {
            int recordcount = GetPageRecord(sql);//方法见上
            int pagecount = recordcount / pagesize;
            return (recordcount % pagesize == 0 ? pagecount : pagecount + 1);
        }
        public int GetPageRecord(string sql)
        {
            SqlConnection cn = new SqlConnection("Data Source = CX-PC\\HAPPY;Initial Catalog =Community management;User ID=sa;");

            sql = "select count(*) FROM community WHERE (psss =1)";
            SqlCommand cmd = new SqlCommand(sql, cn);
            cmd.Connection.Open();
            int recordcount = (int)cmd.ExecuteScalar();
            return recordcount;
        }


    }
} --------------------编程问答-------------------- 怎么没有人回复..... --------------------编程问答-------------------- http://www.cnblogs.com/yssoft/archive/2009/05/03/1448251.html --------------------编程问答--------------------
引用 2 楼  的回复:
http://www.cnblogs.com/yssoft/archive/2009/05/03/1448251.html

能帮忙看下程序不 ? --------------------编程问答-------------------- 单步调试

 protected void AspNetPager1_PageChanged(object sender, EventArgs e)
  {
  ListBind();
  }


看看你点击第二页的时候从数据库得到了什么
友情提示:既然用了分页控件,sql语句最好也写成分页的 为了提高查询效率。。。数据量越大越明显
补充:.NET技术 ,  ASP.NET
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,