1. 程式人生 > >DataGrid的無重新整理分頁

DataGrid的無重新整理分頁

cs 檔案

 using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
public partial class Property_RentBuilder : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        AjaxPro.Utility.RegisterTypeForAjax(typeof(Property_RentBuilder));
        this.PageCount_lb.Text = this.GetPageCount().ToString();
        this.RecorderCount_lb.Text = this.GetRecorderCount().ToString();
    }

    int PageSize = 31;
    DataGrid datagrid = new DataGrid();
    [AjaxPro.AjaxMethod]
    public  string CreateDource(int index)
    {
        string str = "select top " + this.PageSize + " * from Property_Rent where RentID not in(select top " + this.PageSize*index + " RentID from Property_Rent where Type=2 order by RentID) and Type=2 order by RentID";
        DataTable dt = DataBase.ExeSqldt(str);
        datagrid.AutoGenerateColumns = false;
        datagrid.DataSource = dt;
        datagrid.PageSize = this.PageSize;
        datagrid.CurrentPageIndex = index;
        CreateColumn();
        datagrid.DataBind();
        CreateStyle();
        StringWriter wr = new StringWriter();
        HtmlTextWriter writer = new HtmlTextWriter(wr);
        datagrid.RenderControl(writer);
        string gridHTMl = wr.ToString();
        wr.Close();
        writer.Close();
        return gridHTMl;
    }

    #region 建立DataGrid的列
    private void CreateColumn()
    {
        BoundColumn bc1 = new BoundColumn();
        bc1.DataField = "WantType";
        bc1.HeaderText = "需求型別";
        datagrid.Columns.Add(bc1);

        BoundColumn bc2 = new BoundColumn();
        bc2.DataField = "RentType";
        bc2.HeaderText = "房屋型別";
        datagrid.Columns.Add(bc2);

        TemplateColumn tc = new TemplateColumn();
        tc.ItemTemplate = new ColumnTemplate1();
        tc.HeaderText = "地址";
        datagrid.Columns.Add(tc);

        BoundColumn bc3 = new BoundColumn();
        bc3.DataField = "RentPrice";
        bc3.DataFormatString = "{0:c}";
        bc3.HeaderText = "價格";
        datagrid.Columns.Add(bc3);

        BoundColumn bc4 = new BoundColumn();
        bc4.DataField = "Announcetime";
        bc4.DataFormatString = "{0:d}";
        bc4.HeaderText = "釋出時間";
        datagrid.Columns.Add(bc4);

        HyperLinkColumn hlc = new HyperLinkColumn();
        hlc.DataNavigateUrlField = "RentID";
        hlc.Text = "詳細";
        hlc.DataNavigateUrlFormatString = "TowBuiderInfomation.aspx?rentid={0}";
        hlc.Target = "_blank";
        datagrid.Columns.Add(hlc);
    }
    #endregion

    #region  建立DataGrid的樣式
    private void CreateStyle()
    {
        datagrid.Width = 585;
        datagrid.GridLines = GridLines.Horizontal;
        datagrid.HeaderStyle.Font.Bold = true;
        datagrid.HeaderStyle.ForeColor = System.Drawing.Color.FromName("#cccccc");
        this.datagrid.AlternatingItemStyle.BackColor = System.Drawing.Color.FromName("#f7f7f7");
        datagrid.Columns[0].ItemStyle.Width = 70;
        this.datagrid.Columns[0].HeaderStyle.Height = 25;
        this.datagrid.Columns[0].ItemStyle.Height = 20;
        this.datagrid.Columns[0].HeaderStyle.BackColor = System.Drawing.Color.Violet;
        datagrid.Columns[1].ItemStyle.Width = 100;
        this.datagrid.Columns[1].HeaderStyle.Height = 25;
        this.datagrid.Columns[1].ItemStyle.Height = 20;
        this.datagrid.Columns[1].HeaderStyle.BackColor = System.Drawing.Color.Violet;
        datagrid.Columns[2].ItemStyle.Width = 200;
        this.datagrid.Columns[2].HeaderStyle.Height = 25;
        this.datagrid.Columns[2].ItemStyle.Height = 20;
        this.datagrid.Columns[2].HeaderStyle.BackColor = System.Drawing.Color.Violet;
        datagrid.Columns[3].ItemStyle.Width = 50;
        this.datagrid.Columns[3].HeaderStyle.Height = 25;
        this.datagrid.Columns[3].ItemStyle.Height = 20;
        this.datagrid.Columns[3].HeaderStyle.BackColor = System.Drawing.Color.Violet;
        datagrid.Columns[4].ItemStyle.Width = 80;
        this.datagrid.Columns[4].HeaderStyle.Height = 25;
        this.datagrid.Columns[4].ItemStyle.Height = 20;
        this.datagrid.Columns[4].HeaderStyle.BackColor = System.Drawing.Color.Violet;
        datagrid.Columns[5].ItemStyle.Width = 50;
        this.datagrid.Columns[5].HeaderStyle.Height = 25;
        this.datagrid.Columns[5].ItemStyle.Height = 20;
        this.datagrid.Columns[5].HeaderStyle.BackColor = System.Drawing.Color.Violet;
    }
    #endregion

    public class ColumnTemplate1 : ITemplate
    {
        public void InstantiateIn(Control container)
        {
            LiteralControl l = new LiteralControl();
            l.DataBinding += new EventHandler(this.OnDataBinding);
            //資料繫結
            container.Controls.Add(l);
            //為模板列加入LiteralControl
        }
        public void OnDataBinding(object sender, EventArgs e)
        {
            LiteralControl l = (LiteralControl)sender;//LiteralControl傳送繫結請求
            DataGridItem container = (DataGridItem)l.NamingContainer;
            l.Text = Class1.Limit(((DataRowView)container.DataItem)["Address"].ToString(),15);//繫結Address欄位
        }
    }

    private int GetRecorderCount()
    {
        string str = "select * from Property_Rent where Type=2 order by Announcetime desc";
        DataTable dt = DataBase.ExeSqldt(str);
        return dt.Rows.Count;
    }

    [AjaxPro.AjaxMethod]
    public int GetPageCount()
    {
        int PageCount;
        if (this.GetRecorderCount() % this.PageSize == 0)
        {
            PageCount = (this.GetRecorderCount() / this.PageSize);
        }
        else
        {
            PageCount = (this.GetRecorderCount() / this.PageSize) + 1;
        }
        return PageCount;
    }
}

html 檔案程式碼

<%@ Page Language="C#" MasterPageFile="~/Guide.master" AutoEventWireup="true"

CodeFile="RentBuilder.aspx.cs" Inherits="Property_RentBuilder" Title="Untitled Page" %>

<%@ Register Src="Guide.ascx" TagName="Guide" TagPrefix="uc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<script language="javascript">
       function getRentInfomation()
       {
           Property_RentBuilder.CreateDource(curPage,Rentcallback);
       }
       var curPage = 0;
       var PageCount = Property_RentBuilder.GetPageCount().value;
       function Rentcallback(res)
       {
           var load_div = document.getElementById('load_div');
           var str = res.value;
           if(str == '')
            {
                load_div.style.display = "";
            }
   infomation_span.innerHTML = str;
   document.getElementById

('ctl00_ContentPlaceHolder1_CurPage_lb').innerHTML=curPage + 1;
       }
      
      
       function firstPage()
        {
            if(curPage != 0)
            {
                curPage = 0;
                getRentInfomation();
            }
            document.getElementById

('ctl00_ContentPlaceHolder1_CurPage_lb').innerHTML=curPage + 1;
        }
        function prvPage()
        {
           if(curPage != 0)
           {
                curPage -= 1;
               getRentInfomation();
              
           }
           document.getElementById

('ctl00_ContentPlaceHolder1_CurPage_lb').innerHTML=curPage + 1;
        }
        function nextPage()
        {
            if(curPage != PageCount - 1)
            {
                curPage += 1;
                getRentInfomation();
            }
            document.getElementById

('ctl00_ContentPlaceHolder1_CurPage_lb').innerHTML=curPage + 1;
        }
        function lastPage()
        {
            if(curPage != PageCount - 1)
            {
                curPage = PageCount - 1;
                getRentInfomation();
               
            }
            document.getElementById('ctl00_ContentPlaceHolder1_CurPage_lb').innerHTML =

curPage + 1;
        }
</script>
<center>
        <table width="762" border="0" cellpadding="0" cellspacing="0">
  <!--DWLayoutTable-->
  <tr>
    <td width="762" height="3">
        <uc1:Guide ID="Guide1" runat="server" />
    </td>
  </tr>
</table>
<table width="762" border="0" cellpadding="0" cellspacing="0">
  <!--DWLayoutTable-->
  <tr>
    <td width="175" height="804" valign="top"><table width="100%" border="0"

cellpadding="0" cellspacing="0" class="bodyleft">
          <!--DWLayoutTable-->
          <tr>
            <td width="173" height="150" valign="top">
               
            </td>
          </tr>
          <tr>
            <td height="216" valign="top"><table width="100%" border="0" cellpadding="0"

cellspacing="0">
                <!--DWLayoutTable-->
                <tr>
                  <td width="173" height="25" valign="top"><table width="100%" border="0"

cellpadding="0" cellspacing="0" class="title_color">
                      <!--DWLayoutTable-->
                      <tr>
                        <td width="173" height="25" align="center" valign="middle"

class="title_font">最新熱門</td>
                      </tr>
                  </table></td>
                </tr>
                <tr>
                  <td valign="top" style="height: 191px"><table width="100%" border="0"

cellpadding="0" cellspacing="0">
                      <!--DWLayoutTable-->
                      <tr>
                        <td width="173" height="191">&nbsp;</td>
                      </tr>
                  </table></td>
                </tr>
            </table></td>
          </tr>
          <tr>
            <td height="200" valign="top"><table width="100%" border="0" cellpadding="0"

cellspacing="0">
                <!--DWLayoutTable-->
                <tr>
                  <td width="173" height="25" valign="top"><table width="100%" border="0"

cellpadding="0" cellspacing="0" class="title_color">
                      <!--DWLayoutTable-->
                      <tr>
                        <td width="173" height="25" align="center" valign="middle"

class="title_font">欄目熱點</td>
                      </tr>
                  </table></td>
                </tr>
                <tr>
                  <td height="175" valign="top"><table width="100%" border="0"

cellpadding="0" cellspacing="0">
                      <!--DWLayoutTable-->
                      <tr>
                        <td width="173" height="175">&nbsp;</td>
                      </tr>
                  </table></td>
                </tr>
            </table></td>
          </tr>
          <tr>
            <td height="200" valign="top">
           
            </td>
          </tr>
            </table></td>
    <td width="587" valign="top"><table width="100%" border="0" cellpadding="0"

cellspacing="0" class="bodyleft">
       
        <tr>
          <td height="400" valign="top"><table width="100%" border="0" cellpadding="0"

cellspacing="0">
                <!--DWLayoutTable-->
                <tr>
                  <td width="585" height="25" valign="top"><table width="100%" border="0"

cellpadding="0" cellspacing="0" class="title_color">
                      <!--DWLayoutTable-->
                      <tr>
                        <td width="585" height="25" align="left" valign="middle"

class="title_font">  求/出租房列表 </td>
                      </tr>
                  </table></td>
                </tr>
                <tr>
                  <td height="780" align="left" valign="top">
                     <div id="chuzu_div">
                         <span id="infomation_span"></span>
                         <script language="javascript">
                             getRentInfomation();
                         </script>
                                <br />
                                <div id="load_div" style="display:none">
                                    <img src="images/loading.gif" alt="" />資料載入中....
                                </div>
                       <table  align="right">
                           <tr>
                              <td align="right">
                                 共<asp:Label runat="server" ID="PageCount_lb"></asp:Label>


                                  當前第<asp:Label runat="server"

ID="CurPage_lb"></asp:Label>頁 
                                   共<asp:Label runat="server"

ID="RecorderCount_lb"></asp:Label>條
                                  <a id="first_page" onclick="firstPage()"

style="cursor:hand">首頁</a>
                                 <a id="prv_page" onclick="prvPage()" style="cursor:hand">

上一頁</a>
                                 <a id="new_page" style="cursor:hand" onclick="nextPage()">

下一頁</a>
                                 <a id="last_page" onclick="lastPage()"

style="cursor:hand">尾頁</a>
                              </td>
                           </tr>
                       </table>
                     </div>
                  </td>
                </tr>
                    </table></td>
          </tr>
      
    </table></td>
  </tr>
</table>
<table width="762" border="0" cellpadding="0" cellspacing="0">
  <!--DWLayoutTable-->
  <tr>
    <td width="762" height="8"></td>
  </tr>
</table>
</center>
</asp:Content>