protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Button btnFrontPage;
protected System.Web.UI.WebControls.Button btnNextPage;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
SqlConnection conn = new SqlConnection("server=.; uid=sa; pwd=; database=luo_news");
SqlCommand cmd = new SqlCommand("select num = COUNT(*) from db_article",conn);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.SingleRow);
if(dr.Read())
{
this.DataGrid1.VirtualItemCount = (int)dr["num"];
}
this.Label1.Text = dr["num"].ToString();
dr.Close();
conn.Close();
BindToSource("fristPage");
}
}
private void NavigateToPage(object sender, System.EventArgs e)
{
string btnInfo = ((Button)sender).CommandName;
switch(btnInfo)
{
case "frontPage":if(this.DataGrid1.CurrentPageIndex > 0)
this.DataGrid1.CurrentPageIndex -= 1;
else
return;
break;
case "nextPage":if(this.DataGrid1.CurrentPageIndex < this.DataGrid1.PageCount - 1)
this.DataGrid1.CurrentPageIndex += 1;
else
return;
break;
}
BindToSource(btnInfo);
}
private void BindToSource(string btnInfo)
{
SqlConnection conn = new SqlConnection("server=.; uid=sa; pwd=; database=luo_news");
SqlCommand cmd = null;
switch(btnInfo)
{
case "fristPage":cmd = new SqlCommand("select top 2 title, articleid, writer, addtime from db_article",conn);
break;
case "frontPage":cmd = new SqlCommand("select top 2 title, articleid, writer, addtime from db_article where articleid>=@ID",conn);
cmd.Parameters.Add("@ID",SqlDbType.Int,8).Value = ViewState[(this.DataGrid1.CurrentPageIndex-1).ToString()];
break;
case "nextPage":cmd = new SqlCommand("select top 2 title, articleid, writer, addtime from db_article where articleid>@ID",conn);
cmd.Parameters.Add("@ID",SqlDbType.Int,8).Value = this.DataGrid1.Items[1].Cells[0].Text;
break;
}
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
this.DataGrid1.DataSource = dr;
this.DataGrid1.DataBind();
dr.Close();
conn.Close();
ViewState[this.DataGrid1.CurrentPageIndex.ToString()] = this.DataGrid1.Items[0].Cells[0].Text;
}
当点激下一页时出错提示是“输入字符串的格式不正确”,大家帮帮偶看看代码有没问题