DataList自定义分页

DataList自定义分页
 
aspx中的代码:
<asp:datalist id="mydatalist" Runat="server">
     <ItemTemplate>
         <TABLE id="Table2" cellSpacing="0" cellPadding="0" width="400" border="0">
              <TR>
                   <TD width="15">&nbsp;</TD>
                   <TD width="85" height="22">&nbsp;</TD>
                   <TD width="211">&nbsp;</TD>
                   <TD width="80">&nbsp;</TD>
              </TR>
              <TR>
                   <TD width="15">&nbsp;</TD>
                   <TD class="ziti" bgColor="#f7eeef" colSpan="3" height="22"> 回&nbsp;&nbsp;复:</TD>
              </TR>
              <TR>
                   <TD width="15"></TD>
                   <TD bgColor="#666666" colSpan="3" height="1"></TD>
              </TR>
              <TR>
                   <TD width="15" rowSpan="3">&nbsp;</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">&nbsp;</TD>
                   <TD width="4">&nbsp;</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">&nbsp;</TD>
                   <TD class="ziti" colSpan="3" height="22"> 共有 <asp:label id="lblRecordCount" runat="server" ForeColor="red"></asp:label> 条记录&nbsp;
                                     当前为 <asp:label id="lblCurrentPage" runat="server" ForeColor="red"></asp:label>/<asp:label id="lblPageCount" runat="server" ForeColor="red"></asp:label> 页&nbsp;
                                     <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();
         }


Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1449345
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值