日期:2014-05-17  浏览次数:20492 次

datalist分页出现输入字符串的格式不正确。
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _1108 : System.Web.UI.Page
{
  public SqlConnection GetConnection()
  {
  string myStr = ConfigurationManager.AppSettings["ConnectionString"].ToString();
  SqlConnection myConn = new SqlConnection(myStr);
  return myConn;
  }
  protected void Page_Load(object sender, EventArgs e)
  {
  if (!IsPostBack)
  {
  Bind();
  }
  }
  public void Bind()
  {
  int CurrentPage = Convert.ToInt32(lblNowPage.Text);
  PagedDataSource ps = new PagedDataSource();
  SqlConnection con = GetConnection();
  con.Open();
  string SqlStr = "SELECT * FROM tb_Student";
  SqlDataAdapter sda = new SqlDataAdapter(SqlStr, con);
  DataSet ds = new DataSet();
  sda.Fill(ds, "tb_Student");
  ps.DataSource = ds.Tables["tb_Student"].DefaultView;
  ps.AllowCustomPaging = true;
  ps.PageSize = 4;
  ps.CurrentPageIndex = CurrentPage - 1;
  lnkbtnFront.Enabled = true;
  lnkbtnFirst.Enabled = true;
  lnkbtnNext.Enabled = true;
  lnkbtnLast.Enabled = true;
  if (CurrentPage == 1)
  {
  lnkbtnFirst.Enabled = false;
  lnkbtnFront.Enabled = false;
  }
  if (CurrentPage == ps.PageCount)
  {
  lnkbtnNext.Enabled = false;
  lnkbtnLast.Enabled = false;
  }
  this.lblCount.Text = Convert.ToString(ps.PageCount);
  this.DataList1.DataSource = ps;
  this.DataList1.DataKeyField = "ID";
  this.DataList1.DataBind();

  }
  protected void lnkbtnFirst_Click(object sender, EventArgs e)
  {
  this.lblNowPage.Text = "1";
  this.Bind();
  }
  protected void lnkbtnFront_Click(object sender, EventArgs e)
  {
  this.lblNowPage.Text = Convert.ToString(Convert.ToInt32(this.lblNowPage.Text) - 1);
  this.Bind();
  }
  protected void lnkbtnNext_Click(object sender, EventArgs e)
  {
  this.lblNowPage.Text = Convert.ToString(Convert.ToInt32(this.lblNowPage.Text) +1);
  this.Bind();
  }
  protected void lnkbtnLast_Click(object sender, EventArgs e)
  {
  this.lblNowPage.Text = this.lblCount.Text;
  this.Bind();
  }
}

------解决方案--------------------
是不是你读取的数据格式有点问题啊,比如字符想转数值型等等
------解决方案--------------------
估计类型转换那方面出错了、
------解决方案--------------------
Convert.ToInt32(lblNowPage.Text);
可能参数不是一个合法的数值格式字符串。

另外分页用aspnetpager控件比较方便。

------解决方案--------------------
this.lblNowPage.Text改为this.lblNowPage.Text.Trim()试试
------解决方案--------------------
自己调试看看在哪一行报的错啊。
还有Convert.ToString(Convert.ToInt32(this.lblNowPage.Text) - 1); 这么写很蛋疼。