首頁 >後端開發 >C#.Net教程 >.NET中怎麼實現程式分頁

.NET中怎麼實現程式分頁

零下一度
零下一度原創
2017-06-24 09:59:482005瀏覽

aspx

查询

 

<%#rptList.Items.Count == 0 ? "暂无记录" : ""%>

显示 OnTextChanged="txtPageNum_TextChanged" AutoPostBack="True">条/页

CodeBehind

#public partial class xxx : Web.UI.ManagePage
{

protected int totalCount;//總記錄數
protected int page;//目前頁面
#protected int pageSize;//每頁資料大小
protected string keywords = string.Empty;//查詢條件

#protected void Page_Load(object sender, EventArgs e)
    {

     this.keytils =##     .
.GetQueryString("keywords");//獲取查詢條件
            this.pageSize = GetPageSize(10); //設置每頁數據大小


            if (!Page.IsPostBack)
            {

c合適);

     }

   }

##   }


##   }




##   }

##   }

#private void RptBind(string _strWhere, string _orderby)        {            txtKeywords.Text = this.keywords ;
            BLL.Business bll = new BLL.Business();
            this.rpbList,Data, 擲. ;
            this.rptList.DataBind();            lblTotalIncome.Text = Math. Text = this.pageSize.ToString();            string pageUrl =
Utils.
CombUrlTxt("xxx.aspx", "keywords={0}&page={1}", this.keywords, "__id__");
            PageCont
OutPageList(this.pageSize, this.page, this.totalCount, pageUrl, 8);
        }


#private int GetPageSize################private int GetPageSize(int _default_size )###        {###            int _pagesize;###          _pagesize))###            {### if (_pagesize > 0)###                {###                }###            }###            return _default_size;###  

protected void lbtnSearch_Click(object sender, EventArgs e)
        {
            Response.Aftilect(pptil; ) , txtKeywords.Text));        }

protected void txtPageNum_TextChanged(object sender,  ize;

            if (int .TryParse(txtPageNum.Text.Trim(), out _pagesize))            {
               {
                  
Utils.
WriteCookie("detail_page_size 「,」 NovelPage", _pagesize.ToString(), 14400);
                }
            }
       ombUrlTxt("xxx.aspx", "keywords={0}", this.keywords)) ;        }


#}

實用工具


public class Utils{


public static string GetQueryString(string strName)

        {

    ##        }


public static string GetQueryString(string strName, bool sqlSafeCheck)
       ) ull)
                return "";

           if.        return "不安全字串";

             return HttpContext .Current .Request.QueryString[strName];
        }


public static bool IsSafeSqlString(string str)
        {
            return !Regex.IsMatch(str, @ "[ -|;|,|\/|\(|\)|\[|\]|\}|\{|%|@|\*|!|\']");
        }




#public static int GetQueryInt(string strName, int defValue)
        {

           }# ## ###

public static int StrToInt(string expression, int defValue)
        {
           (表達式.Trim(), @"^([-]|[0-9])[0-9]*(\.\w*)?$"))
                 return defValue;
#ff ##            int rv;
            if (Int32.TryParse(表達式, out            return 配置.ToInt32(StrToFloat(表達式, defValue));
}


##public static float StrToFloat(string expression, float defValue)
        {
       #                return defValue;

           與         {                bool IsFloat = Regex.IsMatch(表達式, @"^([-] |[ 0-9])[0-9]*(\.\w*)?$");
               if (IsFloat)
   intValue);
            }
return intValue;
        }

#

public static string CombUrlTxt(string _url, string _keys, params string[] _values)
        {
          try
            {
                string [] keyArr = _keys.Split(new char[] { '&' });
                for (int i = 0; i                    {
   rlEncode(_values[i]);
                       urlParams.Append(string.Format(keyArr) [i], _values) + "&");
                    }
      if (!string.IsNullOrEmpty(urlParams.ToString()) && _url.IndexOf("?") == -1)
                           {
                return _url;
        , "&");
        }



public static string UrlEncode(string str)
                {
                return "";
            }
        return HttpContext.Current.Server.UrlEncode(str);
        }


//刪除最後結尾的字元指定後面的字元



#public static string DelLastChar(string str, string strchar)
        {
# if (string.IsNullOrEmpty(str))
                return "";
        th - 1)

            {

                return str.Substring(0, str.LastIndexOf(strchar));      1       }

//回傳分頁碼

public static string OutPageList(int pageSize, int pageIndex, int totalCount, string linkUrl, int centSize)
        {#    if (totalCount             {
                return "";
          return "";
   = totalCount / pageSize;
            if (pageCount             }
            if (totalCount % pageSize > 0)
            {
         }
            if (pageCount             {
return "";
#            }
            StringBuilder pageStr = new StringBuilder();
        string firstBtn = "            string lastBtn = "下一頁»";
            string firstStr = "1";
            string lastStr = "; ” + pageCount.ToString() + "";

            if (pageIndex      Btn = "";
            }
           if (pageIndex >= page      lastBtn = "下頁»            }
#           if if (pageIndex == 1)
    firstStr = "1            }
            if (pageIndex == pageCount)
            {
     + pageCount.ToString() + "
";
            }
int firstNum = pageIndex - (centSize / 2); //中間開始的頁碼
            if (pageIndex < centSize)##  #         int lastNum = pageIndex + centSize - ((centSize / 2) + 1); //中間結束的頁碼
            if (lastNum >= pageCount)
           .Append("共" + totalCount + "記錄< ;/span>");
            pageStr.Append(firstBtn + firstStr);
        #                pageStr.Append("...< ;/span>\n");
            }
            for (int i = first.             if (i == pageIndex)
                {
                                 else
#                {
    ("" + i + "");
        
            if (pageCount - pageIndex > centSize - ((centSize / 2)))
        ...");
}
            pageStr.Append(lastStr + lastBtn);
           , iginalStr, string oldStr, string newStr)
        {
            if (string.IsNullOrEmpty(oldStr))
            {
                return "";
            }
            return originalStr.Replace(oldStr, newStr);
}

public static string GetCookie(string strName, string key)
        {
            if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null && HttpContext.Current.Request.Cookies[strName][key] != null)
               ,     return "";
        }

public static string UrlDecode(string str)

        {         {
return "";
            }
            return HttpContext.Current.Server.Url strName, string key, string strValue, int 過期)
        {
            HttpCookie cookie = HttpContext.Cur              {
                cookie = new HttpCookie(strName );
            }
#            cookie[key] = UrlEncode(str.          HttpContext.Current.Response.AppendCookie(cookie);

        }



}







##名詞空間

## 公共業務類別

    {

        private readonly DAL.xxx xxxDal;

# # #            xxxDal = new DAL.xxx();

##public List; GetList(int pageSize,int pageIndex,字串strWhere,字串filedOrder,out int recordCount,out小數totalIncome)
        {
      鈴   re_cOrout,Dpage Income); # ##        }

}

DAL

##命名空間


##public 分部類別 xxx
{

公共清單 GetList(int pageSize, int pageIndex, string strWhere, string returnedOrder, out int recordCount,out 小數totalIncome)
        {##     StringBuilder strSql = new StringBuilder();
            strSql.Append (@"select a.oid,ocreatetime,a.oaccname,a.oacctel,(從AppUser 中選擇ucardnum,其中uid=a.ouid) as ucardnum,c.pname,
       corealmoney, 0) as corealmoney,ISNULL(b.cooid,0) as cooid,
                      ULL(codowndrawfee,0) codowndrawfee,ISNULL(d.uname 、'無') as uname,
                                       ROW_NUMBER() over(order by ocreatetime desc) r from xxx a left join
        comoney、corealmoney,codowndrawfee,codrawfee,couupperid,couserrate from xxx where costatus=1)b
               c.pid
                           left join xxx d on b . couupperid= d.[uid] where a.ostatus=1 ");
            if (strWhere.Trim()!= "")
    strSql.Append(strWhere);
            }
using (var context = DataBaseConnection.GetSdkBaseConnection())
            {
           ()));
                if (recordCount > 0)
               {
                        strSql2.Append(@"從xxx a 左連接選擇SUM(corealmoney)
                、codowndrawfee、 comoney ,codrawfee,couupperid,couserrate 來自xxx,其中costatus=1)b
                     pid
                                地加入xxx d。strSql2.Append(strWhere);
                    }
       return context.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, strSql.ToString(), filedOrder)).ToList( );
            }
#        }

#}

PagingHelf

##PagingHel

PagingHel

PagingHel






Paginging



























#都

##都編號############// ROW_NUMBER高效率分頁(僅支援MSSQL2005以上)############public static class PagingHelper### {##### #######// 取得分頁SQL語句,預設row_number為關健字,所有資料表不允許使用該欄位名稱############public static string CreatePagingSql(int _recordCount, int _pageSize, int _pageIndex, string _safeSql, string _orderField)###        {###         0 ? _recordCount : _pageSize;###            int pageCount = (_recordCount + _pageSize - 1 ) / _pageSize;######            //檢視目前頁數###           ,          _pageIndex = 1;###            }####         pageCount)###            {####               與page  //拼接SQL字串,加上ROW_NUMBER函數進行分頁###            StringBuilder newSafeSql = new StringString();###     ("SELECT ROW_NUMBER() OVER(ORDER BY {0}) as row_number,", _orderField);###            newSafeSql.Append(_safeS.l.Substring_Upm.Append(Inqper); ######            //拼接成最終的SQL語句###             String  );###            sbSql.Append(newSafeSql.ToString ()); ###           + 1, _pageIndex * _pageSize);######            return sbSql.ToString();###       ing CreateCountingSql(string _safeSql)###        {### return string.Format(" SELECT COUNT(1) AS RecordCount FROM ({0}) AS T ", _safeSql);###        }############} ## ###

以上是.NET中怎麼實現程式分頁的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn