62,071
社区成员
发帖
与我相关
我的任务
分享
int Page=1;
int totalPage=1;
int totalRecord=0;
int PageSize=5;
SqlCommand cmd=null;
//算出总行数
cmd=new SqlCommand("select count(*) as recordCount from news WHERE category='"+category+"'",cnn);
totalRecord=int.Parse(cmd.ExecuteScalar().ToString());
//输出记录
string SQL="select * from news WHERE category='"+category+"' order by date desc";
DataSet ds=new DataSet();
SqlDataAdapter da=new SqlDataAdapter(SQL,cnn);
da.Fill(ds,"news");
Paging(cnn,"news");
if(totalRecord%PageSize==0)
{
totalPage=totalRecord/PageSize;
}
else
{
totalPage=(int)Math.Floor(totalRecord/PageSize)+1;
}
if(totalPage==0)
totalPage=1;
if(Request.QueryString["Page"]==null||Request.QueryString["Page"].Equals(""))
{
Page=1;
}
else
{
try
{
Page=int.Parse(Request.QueryString["Page"]);
}
catch(Exception ex)
{
//this.GoToErrorPage("news",ex.Message,"news.aspx");
}
}
if(Page<1)
Page=1;
if(Page>totalPage)
Page=totalPage;
SqlConnection cnnNum=new SqlConnection(this.GetConn());
cnnNum.Open();
//添加表头
TableRow newRow;
TableCell newCell;
newRow=new TableRow();
newRow.BackColor=Color.FromArgb(202,202,202);
//主题
newCell=new TableCell();
newCell.HorizontalAlign=HorizontalAlign.Center;
newCell.Font.Size=10;
newCell.Font.Bold=true;
newCell.Text="主 题";
newCell.ForeColor=Color.Black;
newCell.Width=500;
newRow.Cells.Add(newCell);
//发表时间
newCell=new TableCell();
newCell.HorizontalAlign=HorizontalAlign.Center;
newCell.Font.Size=10;
newCell.Font.Bold=true;
newCell.Text="发表时间";
newCell.ForeColor=Color.Black;
newRow.Cells.Add(newCell);
this.tblNewsList.Rows.Add(newRow);
int rowIndex=(Page-1)*PageSize;
int remainRows=ds.Tables["news"].Rows.Count-(Page-1)*10;
int nVal=0;
if(PageSize<=remainRows)
{
nVal=PageSize;
}
else
{
nVal=remainRows;
}
for(int iPage=1;iPage<=nVal;iPage++)
{
//添加内容
newRow=new TableRow();
newRow.BackColor=Color.FromArgb(234,234,234);
newRow.ForeColor=Color.Black;
//主题
newCell=new TableCell();
newCell.HorizontalAlign=HorizontalAlign.Left;
newCell.Font.Size=10;
newCell.Text="<a href='news.aspx?ID="+ds.Tables["news"].Rows[rowIndex]["id"].ToString()+"' target='_blank'><font color=black>"+ds.Tables["news"].Rows[rowIndex]["title"].ToString()+"</font></a>";
newRow.Cells.Add(newCell);
//发表时间
newCell=new TableCell();
newCell.HorizontalAlign=HorizontalAlign.Center;
newCell.Font.Size=10;
newCell.Text=ds.Tables["news"].Rows[rowIndex]["date"].ToString();
newRow.Cells.Add(newCell);
this.tblNewsList.Rows.Add(newRow);
rowIndex++;
}
newRow=new TableRow();
newRow.BackColor=Color.FromArgb(234,234,234);
newRow.ForeColor=Color.Black;
newCell=new TableCell();
newCell.ColumnSpan=5;
newCell.HorizontalAlign=HorizontalAlign.Left;
newCell.Font.Size=10;
if(Page!=1)
{
newCell.Text=" <a href=newslist.aspx?Page=1>第一页</a> <a href=newslist.aspx?Page="+(Page-1)+">上一页</a>";
}
if(Page!=totalPage)
{
newCell.Text+=" <a href=newslist.aspx?Page="+(Page+1)+">下一页</a> <a href=newslist.aspx?Page="+totalPage+">最后一页</a>";
}
newCell.Text+="<font color=red> 新闻总数:"+totalRecord+" 页数:"+Page+"/"+totalPage+"</font>";
newRow.Cells.Add(newCell);
this.tblNewsList.Rows.Add(newRow);
cnn.Close();
cnnNum.Close();
this.tblNewsList.BorderStyle=BorderStyle.Solid;