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();

         }

發佈了21 篇原創文章 · 獲贊 1 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章