簡單的ASP.NET無刷新分頁

1、新建一個分頁存儲過程:

  1. CREATE?procedure?[dbo].[P_Pager] ?
  2. (@PageNumber?int, ?
  3. @PageSize?int) ?
  4. as?
  5. ?declare?@sql?nvarchar(4000) ?
  6. ????set?@sql?=?'select?top?'?+?Convert(varchar,?@PageSize)???+?'?*?from?T_Test?where?[type]=1?and?id?not?in?(select?top?'?+?Convert(varchar,?(@PageNumber?-?1)?*?@PageSize)??+?'?id?from?T_Test?order?by?id?desc)?order?by?id?desc'?
  7. ?exec?sp_executesql?@sql ?
  8. GO?

 2、設置Ajax控件

想要實現無刷新,肯定會用到Ajax,首先是要放入必不可少的:

  1. <asp:ScriptManager?ID="ScriptManager1"?runat="server"> ?
  2. </asp:ScriptManager>?

接著把Repeater控件嵌入到UpdatePanel里面去:

  1. <asp:UpdatePanel?ID="UpdatePanel1"?runat="server"> ?
  2. ???????????<ContentTemplate> ?
  3. ???????????????????<ul> ?
  4. ???????????????????????<asp:Repeater?ID="Repeater1"?runat="server"> ?
  5. ???????????????????????????<ItemTemplate> ?
  6. ???????????????????????????????<li> ?
  7. ???????????????????????????????????<p> ?
  8. ???????????????????????????????????????<span><%#Eval("username")?%>:</span><%#Eval("content").ToString()))?%></p> ?
  9. ???????????????????????????????????<p> ?
  10. ???????????????????????????????????????<em>發表時間: ?
  11. ???????????????????????????????????????????<%#Eval("addtime")?%> ?
  12. ???????????????????????????????????????</em> ?
  13. ???????????????????????????????????</p> ?
  14. ???????????????????????????????</li> ?
  15. ???????????????????????????</ItemTemplate> ?
  16. ???????????????????????</asp:Repeater> ?
  17. ???????????????????</ul> ?
  18. ???????????????????<p> ?
  19. ???????????????????????????總記錄:<asp:Literal?ID="ltlCount"?runat="server"></asp:Literal></p> ?
  20. ???????????????????????<p> ?
  21. ???????????????????????????<webdiyer:AspNetPager?ID="AspNetPager1"?runat="server"?AlwaysShow="true"?PageIndexBoxType="DropDownList"?> ?
  22. ???????????????????????????</webdiyer:AspNetPager> ?
  23. ???????????????????????</p> ?
  24. ???????????????????</div> ?
  25. ???????????</ContentTemplate> ?
  26. ???????</asp:UpdatePanel>?

3、AspNetPager分頁控件

可以看到以上的代碼有個分頁控件,我相信這個分頁控件大家都不陌生了,我就不多介紹。必須有AspNetPager.dll,然后在項目中引用,在頭部引入這一段代碼:

  1. <%@?Register?Assembly="AspNetPager"?Namespace="Wuqi.Webdiyer"?TagPrefix="webdiyer"?%>?

這時候分頁控件應該就可以使用了。最后在后臺綁定一下數據,同時也要綁定AspNetPager控件,完整的后臺代碼如下:

  1. using?System; ?
  2. using?System.Collections.Generic; ?
  3. using?System.Web; ?
  4. using?System.Web.UI; ?
  5. using?System.Web.UI.WebControls; ?
  6. using?System.Data; ?
  7. using?Wuqi.Webdiyer; ?
  8. using?System.Data.SqlClient; ?
  9. ? ?
  10. public?partial?class?AjaxPager?:?System.Web.UI.Page ?
  11. { ?
  12. ????int?currentPageNumber;//當前頁號? ?
  13. ????int?pageSize?=?5;//每頁顯示記錄條數 ?
  14. ????protected?void?Page_Load(object?sender,?EventArgs?e) ?
  15. ????{ ?
  16. ????????if?(!IsPostBack) ?
  17. ????????{ ?
  18. ????????????currentPageNumber?=?1; ?
  19. ????????????ViewState["currentPageNumber"]?=?currentPageNumber; ?
  20. ????????????BindData(); ?
  21. ????????} ?
  22. ????????AspNetPager1.PageChanged?+=?new?EventHandler(AspNetPager1_PageChanged);??//定義控件分頁事件 ?
  23. ????} ?
  24. ? ?
  25. ????//獲取記錄總數 ?
  26. ????private?int?GetCount() ?
  27. ????{ ?
  28. ????????string?sql?=?"select?COUNT(*)?from?T_Test"; ?
  29. ????????DataTable?dt?=?GetTable(sql,?CommandType.Text,?values); ?
  30. ????????if?(dt.Rows.Count?>?0) ?
  31. ????????{ ?
  32. ????????????return?Convert.ToInt32(dt.Rows[0][0]); ?
  33. ????????} ?
  34. ????????else?
  35. ????????{ ?
  36. ????????????return?0; ?
  37. ????????} ?
  38. ????} ?
  39. ????? ?
  40. ????//綁定數據 ?
  41. ????private?void?BindData() ?
  42. ????{ ?
  43. ????????ltlCount.Text?=?GetCount().ToString(); ?
  44. ????????currentPageNumber?=?Convert.ToInt32(ViewState["currentPageNumber"]); ?
  45. ????????SqlParameter[]?values?=?{?new?SqlParameter("@PageNumber",?currentPageNumber),?new?SqlParameter("@PageSize",?pageSize)?}; ?
  46. ????????? ?
  47. ????????DataTable?dt?=?GetTable("P_Pager",?CommandType.StoredProcedure,?values);??//調用存儲過程 ?
  48. ????????if?(dt.Rows.Count?>?0) ?
  49. ????????{ ?
  50. ????????????AspNetPager1.PageSize?=?pageSize; ?
  51. ????????????AspNetPager1.RecordCount?=?GetCount(); ?
  52. ????????????AspNetPager1.CurrentPageIndex?=?currentPageNumber; ?
  53. ????????????this.Repeater1.DataSource?=?dt.DefaultView; ?
  54. ????????????this.Repeater1.DataBind(); ?
  55. ????????} ?
  56. ????} ?
  57. ? ?
  58. ????//分頁事件? ?
  59. ????void?AspNetPager1_PageChanged(object?sender,?EventArgs?e) ?
  60. ????{ ?
  61. ????????currentPageNumber?=?AspNetPager1.CurrentPageIndex; ?
  62. ????????ViewState["currentPageNumber"]?=?currentPageNumber; ?
  63. ????????BindData(); ?
  64. ????} ?
  65. ????? ?
  66. ?????//?讀取存儲過程返回table ?
  67. ????private?DataTable?GetTable(string?sql,?CommandType?t,?params?SqlParameter[]?values) ?
  68. ????{ ?
  69. ????????using?(SqlConnection?conn?=?new?SqlConnection("Data?Source=127.0.0.1;Initial?Catalog=testDB;Persist?Security?Info=True;User?ID=sa;Password=123456")) ?
  70. ????????{ ?
  71. ????????????SqlCommand?comm?=?new?SqlCommand(sql,?conn); ?
  72. ????????????comm.CommandType?=?t; ?
  73. ????????????if?(values?!=?null?&&?values.Length?>?0) ?
  74. ????????????????comm.Parameters.AddRange(values); ?
  75. ????????????SqlDataAdapter?da?=?new?SqlDataAdapter(comm); ?
  76. ????????????DataSet?ds?=?new?DataSet(); ?
  77. ????????????try?
  78. ????????????{ ?
  79. ????????????????conn.Open(); ?
  80. ????????????????da.Fill(ds); ?
  81. ????????????????return?ds.Tables[0]; ?
  82. ????????????} ?
  83. ????????????catch?(Exception) ?
  84. ????????????{ ?
  85. ????????????????return?null; ?
  86. ????????????} ?
  87. ????????????finally?
  88. ????????????{ ?
  89. ????????????????conn.Close(); ?
  90. ????????????????conn.Dispose(); ?
  91. ????????????} ?
  92. ????????} ?
  93. ????} ?
  94. }?

轉載于:https://www.cnblogs.com/nianyuwen/archive/2012/06/29/2570537.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/274741.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/274741.shtml
英文地址,請注明出處:http://en.pswp.cn/news/274741.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Koa在實際的業務場景中,路由如何做分割?【文末留言送書】

大家好&#xff0c;我是若川。文末留言送書&#xff0c;具體規則文末說明。另外為了鼓勵大家多寫源碼共讀筆記&#xff0c;我會在寫了5次及以上筆記的作者群里也抽獎送這本書。以后也會有更多福利傾斜。導讀&#xff1a;Koa是一個Node框架&#xff0c;在Node開源社區中&#xf…

設計模式_設計

設計模式Thanks for my colleague WanChing‘s help to prepare this sharing article. E-Commerce app collects plentiful products from various brands. Each brand has its brand signature colors and public image. This article introduces how we made a single page …

動態切換css

方法一&#xff1a;給link一個id&#xff0c;直接獲取該DOM操作href <link rel"stylesheet" id"stylelink" type"text/css"/> <a href"#" οnclickjavascript:document.getElementById("stylelink").href "…

使用 GTD 優化自己的工作和生活

大家好&#xff0c;我是若川。持續組織了8個月源碼共讀活動&#xff0c;感興趣的可以點此加我微信 ruochuan12 參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。同時極力推薦訂閱我寫的《學習源碼整體架構系列》 包含20余篇源碼文章。歷史面試系列。另外…

模仿不再受寵若驚

If you haven’t heard of the Jio-Zoom plagiarism clash, you’re probably living under a rock (which may not be a bad idea given the state of the world right now). The turf war between Jio Meet and Zoom began when the Indian telecom giant ripped off the Chi…

一個計算機愛好者的不完整回憶(二十八)關于計算機書籍

我只在大學階段在圖書館看了很多計算機方面的書&#xff0c;無論已經老得都殘破了還是最新出版的。前兩天又看到論壇中有關于計算機書籍特別是國內人士編寫或翻譯的計算機書籍的評論的文章&#xff0c;譚浩強老先生又毫無懸念的被牽連了進來。也發表一下自己的一些觀點吧。   …

Vue2剝絲抽繭-響應式系統 系列

大家好&#xff0c;我是若川。持續組織了8個月源碼共讀活動&#xff0c;感興趣的可以點此加我微信 ruochuan12 參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。同時極力推薦訂閱我寫的《學習源碼整體架構系列》 包含20余篇源碼文章。歷史面試系列。另外…

word文本樣式代碼樣式_使用文本樣式表達創建真相來源

word文本樣式代碼樣式As of After Effects 17.0, you can use expressions to edit text styles in After Effects. Here’s why this would transform your workflow:從After Effects 17.0開始&#xff0c;您可以使用表達式在After Effects中編輯文本樣式。 這就是這將改變您的…

mvn備忘

創建web工程 mvn archetype:generate -DgroupIdcom.malangmedia -DartifactIdautoDeployToJetty -DarchetypeArtifactIdmaven-archetype-webapp -Dversion1.0 添加jetty插件 <project xmlns"http://maven.apache.org/POM/4.0.0" xmlns:xsi"http://www.w3.or…

前端框架源碼解讀之Vite

前端工具鏈十年盤點&#xff1a;https://mp.weixin.qq.com/s/FBxVpcdVobgJ9rGxRC2zfgWebpack、Rollup 、Esbuild、Vite ?webpack: 基于 JavaScript 開發的前端打包構建框架&#xff0c;通過依賴收集&#xff0c;模塊解析&#xff0c;生成 chunk&#xff0c;最終輸出生成的打包…

hp-ux_UX中的格式塔-或-為什么設計師如此討厭間距

hp-uxI’ve been lucky so far in my design career to have worked with engineers that seem genuinely interested in learning about design. Perhaps, as mentioned in the title, it’s more about them trying to figure out why it matters so much to us that there i…

很多人都不知道,其實博客園給我們博客開了二級域名

如題。一直都在郵件簽名里寫自己的博客地址為&#xff1a; http://www.cnblogs.com/datacool&#xff1b;直到有天突然發現使用&#xff1a;http://datacool.cnblogs.com也可以訪問。不知道的趕緊測試&#xff0c;后者明顯要酷很多啊。該不是我是最后一個知道的吧&#xff0c;知…

JavaScript 數組新增 4 個非破壞性方法!

大家好&#xff0c;我是若川。持續組織了8個月源碼共讀活動&#xff0c;感興趣的可以點此加我微信 ruochuan12 參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。同時極力推薦訂閱我寫的《學習源碼整體架構系列》 包含20余篇源碼文章。歷史面試系列。另外…

自行車改裝電動車怎么樣_電動車聽起來應該是什么樣?

自行車改裝電動車怎么樣The sound of an all-electric car accelerating doesn’t have to sound like a standard combustion engine, It could sound like anything.全電動汽車加速的聲音不必聽起來像是標準的內燃機&#xff0c;它可以聽起來像任何東西。 These were the wor…

C++中的三種繼承public,protected,private(轉)

三種訪問權限 public:可以被任意實體訪問 protected:只允許子類及本類的成員函數訪問 private:只允許本類的成員函數訪問 三種繼承方式 public 繼承 protect 繼承 private 繼承 組合結果 基類中 繼承方式 子類中 public &#xff06; public繼承 > public public &#xff0…

如何碎片化時間學前端,了解前沿趨勢

我很開心在前端行業認識了一批優秀且樂于分享的朋友&#xff0c;他們的技術分享與職業觀點讓我獲益良多&#xff0c;推薦給大家一起關注。程序員成長指北Node.js 前端工程化 低代碼考拉小姐姐&#xff0c;一個有趣且樂于分享的人&#xff01;目前就職于某知名外企&#xff0c;負…

谷歌pay破解_Google Pay缺少Google聞名的一件事-UX案例研究

谷歌pay破解Disclaimer: The views expressed in the blog post is purely based on personal experience. It was not influenced by any external factor.When Google launched Tez (now Google Pay) in India during 2017, their primary goal was to design a simple payme…

進階高級前端,這位大前端架構師一定不能錯過

今天給大家介紹一位好朋友&#xff1a;這波能反殺&#xff1a;一位擁有十年工作經驗&#xff0c;對學習方法有獨到理解的資深大前端架構師。一、博客早在 2017 年初&#xff0c;波神在簡書平臺以《前端基礎進階》為名&#xff0c;更新了一系列優質文章&#xff0c;獲得大量認可…

memcached應用策略(轉)

memcached應用策略&#xff08;轉&#xff09;(2012-04-05 11:10:02) 轉載▼標簽&#xff1a; memcached 應用策略 it分類&#xff1a; linux_c memcached應用策略memcached 主要的作用是為減輕大訪問量對數據庫的沖擊&#xff0c;所以一般的邏輯是首先從memcached中讀取數據&a…

突然討厭做前端,討厭代碼_為什么用戶討厭重新設計

突然討厭做前端,討厭代碼重點 (Top highlight)The core of design thinking is to only design something that will bring value and fill the gap in consumer needs. Right? Why else would one design something that no one asked for? While that may be true to some …