aspx中的代码:
<asp:datalist id="mydatalist" Runat="server">
<ItemTemplate>
<TABLE id="Table2" cellSpacing="0" cellPadding="0" width="400" border="0">
<TR>
<TD width="15"> </TD>
<TD width="85" height="22"> </TD>
<TD width="211"> </TD>
<TD width="80"> </TD>
</TR>
<TR>
<TD width="15"> </TD>
<TD class="ziti" bgColor="#f7eeef" colSpan="3" height="22">回 复:</TD>
</TR>
<TR>
<TD width="15"></TD>
<TD bgColor="#666666" colSpan="3" height="1"></TD>
</TR>
<TR>
<TD width="15" rowSpan="3"> </TD>
<TD class="ziti" vAlign="top" width="85" rowSpan="3">发贴人姓名<BR>
级别:<BR>
</TD>
<TD class="ziti" vAlign="top" height="22"><IMG height="20" src="images.files/face0.gif" width="20">发表于:<%# DataBinder.Eval(Container.DataItem,"remessage_time") %></TD>
<TD class="brown" width="80">回复</TD>
</TR>
<TR>
<TD colSpan="2" height="5"></TD>
</TR>
<TR>
<TD><%# DataBinder.Eval(Container.DataItem,"remessage_context") %></TD>
<TD width="80"> </TD>
<TD width="4"> </TD>
</TR>
<TR>
<TD width="15"></TD>
<TD bgColor="#666666" colSpan="3" height="1"></TD>
</TR>
</TABLE>
</ItemTemplate>
</asp:datalist>
<TABLE id="Table3" cellSpacing="0" cellPadding="0" width="400" border="0">
<TR>
<TD width="15"> </TD>
<TD class="ziti" colSpan="3" height="22">共有<asp:label id="lblRecordCount" runat="server" ForeColor="red"></asp:label>条记录
当前为<asp:label id="lblCurrentPage" runat="server" ForeColor="red"></asp:label>/<asp:label id="lblPageCount" runat="server" ForeColor="red"></asp:label>页
<asp:linkbutton id="lbnPrevPage"runat="server" Text="上一页" CommandName="prev" OnCommand="Page_OnClick"></asp:linkbutton><asp:linkbutton id="lbnNextPage" runat="server" Text="下一页" CommandName="next" OnCommand="Page_OnClick">下一页</asp:linkbutton></TD>
<TR>
</table>
CS代码:
using System.Data.SqlClient;
using System.Configuration;
protected System.Web.UI.WebControls.Label lblmcontent;
protected System.Web.UI.WebControls.Label lblmessagetime;
protected System.Web.UI.WebControls.Label lblmkind;
protected System.Web.UI.WebControls.Label lblmauthor;
protected System.Web.UI.WebControls.Label lblmessagetitle;
protected System.Web.UI.WebControls.DataList mydatalist;
protected System.Web.UI.HtmlControls.HtmlForm Form1;
int PageSize,RecordCount,PageCount,CurrentPage;
protected System.Web.UI.WebControls.Label lblCurrentPage;
protected System.Web.UI.WebControls.Label lblPageCount;
protected System.Web.UI.WebControls.LinkButton lbnPrevPage;
protected System.Web.UI.WebControls.Label lblRecordCount;
protected System.Web.UI.WebControls.LinkButton lbnNextPage;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
PageSize = 2;
SqlConnection con=new SqlConnection();
con.ConnectionString=ConfigurationSettings.AppSettings["ConnectionString"];
SqlCommand cmd=new SqlCommand();
cmd.CommandText="select message_title,message_content,message_content,message_time,message_author from message where message_id= '"+Request.QueryString["id"]+"'";
cmd.Connection=con;
con.Open();
if(!Page.IsPostBack)
{
//设定PageSize
SqlDataReader reader=cmd.ExecuteReader();
if(reader.Read())
{
this.lblmauthor.Text=Convert.ToString(reader["message_author"]);
this.lblmcontent.Text=Convert.ToString(reader["message_content"]);
this.lblmessagetime.Text=Convert.ToString(reader["message_time"]);
this.lblmessagetitle.Text=Convert.ToString(reader["message_title"]);
}
reader.Close();
ListBind();
CurrentPage = 0;
ViewState["PageIndex"] = 0;
//计算总共有多少记录
RecordCount = CalculateRecord();
lblRecordCount.Text = RecordCount.ToString();
//计算总共有多少页
//float pagetemp=0;
if((RecordCount%PageSize)==0)
PageCount = (RecordCount/PageSize);
else
PageCount = (RecordCount/PageSize)+1;
lblPageCount.Text = PageCount.ToString();
ViewState["PageCount"] = PageCount;
}
}
//****************************
public int CalculateRecord()
{
int intCount;
SqlConnection recon=new SqlConnection();
recon.ConnectionString=ConfigurationSettings.AppSettings["ConnectionString"];
SqlCommand recmd=new SqlCommand();
recmd.CommandText="select count(*) as co from remessage where remessage_fatherid = '"+Request.QueryString["id"]+"'";
recmd.Connection=recon;
recon.Open();
SqlDataReader reader1=recmd.ExecuteReader();
if(reader1.Read())
{
intCount = Int32.Parse(reader1["co"].ToString());
}
else
{
intCount = 0;
}
reader1.Close();
return intCount;
}
ICollection CreateSource()
{
int StartIndex;
SqlConnection recon=new SqlConnection();
recon.ConnectionString=ConfigurationSettings.AppSettings["ConnectionString"];
//设定导入的起终地址
StartIndex = CurrentPage*PageSize;
string strSel = "select * from remessage where remessage_fatherid = '"+Request.QueryString["id"]+"'";
DataSet ds = new DataSet();
SqlDataAdapter MyAdapter = new SqlDataAdapter(strSel, recon);
MyAdapter.Fill(ds,StartIndex,PageSize,"remessage");
return ds.Tables["remessage"].DefaultView;
}
public void ListBind()
{
this.mydatalist.DataSource = CreateSource();
this.mydatalist.DataBind();
lbnNextPage.Enabled = true;
lbnPrevPage.Enabled = true;
if(CurrentPage==(PageCount-1)) lbnNextPage.Enabled = false;
if(CurrentPage==0) lbnPrevPage.Enabled = false;
lblCurrentPage.Text = (CurrentPage+1).ToString();
}
public void Page_OnClick(Object sender,CommandEventArgs e)
{
CurrentPage = (int)ViewState["PageIndex"];
PageCount = (int)ViewState["PageCount"];
string cmd = e.CommandName;
//判断cmd,以判定翻页方向
switch(cmd)
{
case "next":
if(CurrentPage<(PageCount-1))
{
CurrentPage++;
}
break;
case "prev":
if(CurrentPage>0) CurrentPage--;
break;
}
ViewState["PageIndex"] = CurrentPage;
ListBind();
}