asp.net 用PagedDataSource对datalist进行分页,总是遇到问题
总显示::从索引0处开始,初始化字符串的格式不符合规范。以下是代码protectedvoidPage_Load(objectsender,EventArgse){dlBi...
总显示::从索引 0 处开始,初始化字符串的格式不符合规范。
以下是代码
protected void Page_Load(object sender, EventArgs e)
{
dlBind();
}
public void dlBind()
{
string strcon= ConfigurationSettings.AppSettings["str"];
SqlConnection conn = new SqlConnection("strcon");
conn.Open();
string strsql="select * from News";
SqlDataAdapter da = new SqlDataAdapter("strsql", conn);
DataSet ds = new DataSet();
da.Fill(ds,"News");
PagedDataSource pds = new PagedDataSource();
pds.DataSource = ds.Tables["News"].DefaultView;
pds.AllowPaging = true;
pds.PageSize = 2;
pds.CurrentPageIndex = Convert.ToInt32(lblCurrentPage.Text) - 1;
lnkBtnFirst.Enabled = true;
lnkBtnLast.Enabled = true;
lnkBtnNext.Enabled = true;
lnkBtnPrevious.Enabled = true;
if (lblCurrentPage.Text == "1")
{
lnkBtnPrevious.Enabled = false;
lnkBtnFirst.Enabled = false;
}
if (lblCurrentPage.Text == pds.PageCount.ToString())
{
lnkBtnNext.Enabled = false;
lnkBtnLast.Enabled = false;
}
lblSumPage.Text = pds.PageCount.ToString(); //实现总页数
//将分页结果绑定到DataList控件上
NewsList.DataSource = pds; //绑定数据源
NewsList.DataKeyField = "news_id";
NewsList.DataBind();
}
protected void lnkBtnFirst_Click(object sender, EventArgs e)
{
lblCurrentPage.Text = "1";
dlBind();
}
protected void lnkBtnPrevious_Click(object sender, EventArgs e)
{
lblCurrentPage.Text = (Convert.ToInt32(lblCurrentPage.Text) - 1).ToString();
dlBind();
}
protected void lnkBtnNext_Click(object sender, EventArgs e)
{
lblCurrentPage.Text = (Convert.ToInt32(lblCurrentPage.Text) + 1).ToString();
dlBind();
}
protected void lnkBtnLast_Click(object sender, EventArgs e)
{
lblCurrentPage.Text = lblSumPage.Text;
dlBind();
}
}
其中datalist的datakeyfield对应的是int型的news_id , 不知道有没有影响 展开
以下是代码
protected void Page_Load(object sender, EventArgs e)
{
dlBind();
}
public void dlBind()
{
string strcon= ConfigurationSettings.AppSettings["str"];
SqlConnection conn = new SqlConnection("strcon");
conn.Open();
string strsql="select * from News";
SqlDataAdapter da = new SqlDataAdapter("strsql", conn);
DataSet ds = new DataSet();
da.Fill(ds,"News");
PagedDataSource pds = new PagedDataSource();
pds.DataSource = ds.Tables["News"].DefaultView;
pds.AllowPaging = true;
pds.PageSize = 2;
pds.CurrentPageIndex = Convert.ToInt32(lblCurrentPage.Text) - 1;
lnkBtnFirst.Enabled = true;
lnkBtnLast.Enabled = true;
lnkBtnNext.Enabled = true;
lnkBtnPrevious.Enabled = true;
if (lblCurrentPage.Text == "1")
{
lnkBtnPrevious.Enabled = false;
lnkBtnFirst.Enabled = false;
}
if (lblCurrentPage.Text == pds.PageCount.ToString())
{
lnkBtnNext.Enabled = false;
lnkBtnLast.Enabled = false;
}
lblSumPage.Text = pds.PageCount.ToString(); //实现总页数
//将分页结果绑定到DataList控件上
NewsList.DataSource = pds; //绑定数据源
NewsList.DataKeyField = "news_id";
NewsList.DataBind();
}
protected void lnkBtnFirst_Click(object sender, EventArgs e)
{
lblCurrentPage.Text = "1";
dlBind();
}
protected void lnkBtnPrevious_Click(object sender, EventArgs e)
{
lblCurrentPage.Text = (Convert.ToInt32(lblCurrentPage.Text) - 1).ToString();
dlBind();
}
protected void lnkBtnNext_Click(object sender, EventArgs e)
{
lblCurrentPage.Text = (Convert.ToInt32(lblCurrentPage.Text) + 1).ToString();
dlBind();
}
protected void lnkBtnLast_Click(object sender, EventArgs e)
{
lblCurrentPage.Text = lblSumPage.Text;
dlBind();
}
}
其中datalist的datakeyfield对应的是int型的news_id , 不知道有没有影响 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询