ASP数据岛操作类
来源:网络来源TAG:数据岛浏览数: 日期:2010-8-20评论:
折叠ASP/Visual Basic Code复制内容到剪贴板
  1. <%    
  2. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''    
  3. ' Programming By Smartpig '    
  4. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''    
  5. Class TBGrid    
  6. public DataSource '数据源    
  7. public style '表格总风格    
  8. public HeadStyle '表头风格    
  9. public HeadItemStyle '表头单独风格    
  10. public itemStyle '单元格独立网络    
  11. public HeadSort '表头是否显示排序功能    
  12. public Columns '需要显示的列元素    
  13. public Alternate '是否交替风格    
  14. public AlternateStyle '偶数行风格    
  15. public NormalStyle '正常风格    
  16. public DefaultStyle '默认风格簇    
  17. public PageSize '页大小    
  18. public AllowPageing '是否分页    
  19. public PageingStyle '页数风格    
  20.   
  21. Private Templates '自定义单元项    
  22. private CurPage '当前页    
  23. private PageStart '页面开始运行时间    
  24.   
  25. '内容之间的关系    
  26. 'Columns.add "Field","HeadText"    
  27. 'AddTemplate("HeadText",Template)    
  28. 'itemStyle.add "Field","style:adsasd"    
  29. 'HeadSort.add "Field",True    
  30. 'DataSource(Columns.Keys(i))    
  31.   
  32. Private Sub Class_Initialize ' 设置 Initialize 事件。    
  33. Set itemStyle = CreateObject("Scripting.Dictionary")    
  34. Set HeadSort = CreateObject("Scripting.Dictionary")    
  35. Set HeadItemStyle = CreateObject("Scripting.Dictionary")    
  36. Set Columns = CreateObject("Scripting.Dictionary")    
  37. Set Templates = CreateObject("Scripting.Dictionary")    
  38. Set DataSource = CreateObject("ADODB.Recordset")    
  39. Alternate = 0    
  40. PageStart = Timer    
  41. End Sub    
  42.   
  43. Private Sub Class_Terminate ' 设置 Terminate 事件。    
  44. Set itemStyle = Nothing    
  45. Set HeadSort = Nothing    
  46. Set HeadItemStyle = Nothing    
  47. Set Columns = Nothing    
  48. Set DataSource = Nothing    
  49. End Sub    
  50.   
  51. Private Sub InitTable()    
  52. 'Set FieldsNum = DataSource.Fields.Count    
  53. 'Set RowsNum = DataSource.RecordCount    
  54. if Columns.Count = 0 then    
  55. For i = 0 to DataSource.Fields.Count -1    
  56. Columns.add DataSource.Fields(i).Name,DataSource.Fields(i).Name    
  57. response.Write(DataSource.Fields(i).Name)    
  58. Next    
  59. end if    
  60.   
  61. if IsEmpty(Style) and IsEmpty(NormalStyle) then    
  62. DefaultStyle = 1    
  63. Else    
  64. DefaultStyle = Style    
  65. end if    
  66.   
  67. CurPage = CInt(Request.QueryString("page"))    
  68. if CurPage = "" then    
  69. CurPage = 1    
  70. End If    
  71.   
  72. if PageSize = Empty then    
  73. PageSize = 10    
  74. end if    
  75.   
  76. select Case DefaultStyle    
  77. Case 1    
  78. Style ="align=center border=0 cellpadding=4 cellspacing=1 bgcolor='#cccccc'"    
  79. Alternate = 1    
  80. HeadStyle = "Height=25 style=""background-color:#006699;color:#ffffff"""    
  81. AlternateStyle = "bgColor=#ffffff height=25"    
  82. NormalStyle = "height=25 bgcolor=#f5f5f5"    
  83. AllowPageing = true    
  84. tbGrid1.PageingStyle = "bgcolor='#f5f5f5' align='right'"    
  85. Case 2    
  86. Style ="align=center border=0 cellpadding=4 cellspacing=1 bgcolor='#cccccc'"    
  87. Alternate = 0    
  88. HeadStyle = "Height=25 style=""background-color:#ffffff"""    
  89. AlternateStyle = "bgColor=#ffffff height=25"    
  90. NormalStyle = "height=25 bgcolor=#ffffff"    
  91. Case Else    
  92. End Select    
  93. End sub    
  94.   
  95. public Sub AddTemplate(ByVal ColumnName,ByVal Template)    
  96. Columns.add ColumnName,ColumnName    
  97. Templates.add ColumnName,Template    
  98. End Sub    
  99.   
  100. public Sub Show()    
  101. InitTable()    
  102. Dim tableStr    
  103. Dim tdStart,tdEnd,tbStyle,tbContent    
  104. Dim curRow    
  105. Dim clm    
  106. Dim regEx,Match,Matches    
  107. tableStr = "<table "&style">" & vbCrLF    
  108. 'Draw Table Head    
  109. Response.Write(tableStr)    
  110. Response.Write("<tr>")    
  111. for Each clm in Columns.Keys()    
  112. tbStyle = HeadStyle & " " & HeadItemStyle(clm)    
  113. tdStart = "<th "&tbStyle">"    
  114. tdEnd = "</th>" & vbCrLf    
  115.   
  116. Response.Write(tdStart)    
  117. '加入表头排序功能    
  118. 'Code by Redsun    
  119. 'Date:2005-1-17    
  120. If HeadSort(clm) Then    
  121. Response.Write Sort(clm,Columns(clm))    
  122. Else    
  123. Response.Write(Columns(clm))    
  124. End If    
  125. Response.Write(tdEnd)    
  126. Next    
  127. Response.Write("</tr>" & vbCrLF)    
  128.   
  129. 'Draw Table items    
  130. curRow = 1    
  131. if AllowPageing <> Empty then    
  132. DataSource.PageSize = PageSize    
  133. else    
  134. DataSource.PageSize = DataSource.RecordCount    
  135. end if    
  136.   
  137. if CurPage < 1 then    
  138. DataSource.AbsolutePage = 1    
  139. end if    
  140.   
  141. if CurPage >= DataSource.PageCount then    
  142. DataSource.AbsolutePage = DataSource.PageCount    
  143. end if    
  144.   
  145. if CurPage >= 1 and CurPage <= DataSource.PageCount then    
  146. DataSource.AbsolutePage = CurPage    
  147. end if    
  148.   
  149. for curRow = 1 to DataSource.PageSize    
  150. if DataSource.EOF then    
  151. Exit For    
  152. end if    
  153.   
  154. Response.Write("<tr>")    
  155. for Each clm in Columns.Keys()    
  156. if Alternate = 0 then    
  157. tbStyle = NormalStyle & " " & ItemStyle(clm)    
  158. else    
  159. if curRow mod 2 = 0 then    
  160. tbStyle = AlternateStyle & " " & ItemStyle(clm)    
  161. else    
  162. tbStyle = NormalStyle & " " & ItemStyle(clm)    
  163. end if    
  164. end if    
  165.   
  166. tdStart = "<td "&tbStyle">"    
  167. tdEnd = "</td>" & vbCrLf    
  168.   
  169. if Templates(clm) = Empty then    
  170. tbContent = DataSource(clm)    
  171. else    
  172. tbContent = Templates(clm)    
  173. Set regEx = New RegExp    
  174. regEx.Pattern= "{[A-Za-z0-9_-]+}"    
  175. regEx.IgnoreCase = True    
  176. regEx.Global = True    
  177. Set Matches=regEx.Execute(Templates(clm))    
  178. For each match in matches    
  179. On Error Resume Next    
  180. tbContent = Replace(tbContent,Match.Value,DataSource(Mid(Match.Value,2,Len(Match.Value)-2)),1)    
  181. Next    
  182.   
  183. end if    
  184.   
  185. Response.Write(tdStart)    
  186. Response.Write(tbContent)    
  187. Response.Write(tdEnd)    
  188. Next    
  189. Response.Write("</tr>" & vbCrLF)    
  190.   
  191. DataSource.MoveNext    
  192. Next    
  193.   
  194. 'Draw Pageing Row    
  195. if DataSource.PageCount > 1 and LCase(pageingStyle) <> "none" then    
  196. Dim i,EndPage,StartPage    
  197. response.write("<tr>")    
  198. response.write("<td colspan=" & Columns.Count & " " & PageingStyle & ">")    
  199. '改进分页功能    
  200. 'Code by Redsun    
  201. 'Date:2005-1-17    
  202. If CurPage>4 Then    
  203. If CurPage+2<DataSource.PageCount Then    
  204. StartPage = CurPage-2    
  205. EndPage = CurPage+2    
  206. Else    
  207. StartPage = DataSource.PageCount-4    
  208. EndPage = DataSource.PageCount    
  209. End If    
  210. Else    
  211. StartPage = 1    
  212. If DataSource.PageCount>5 Then    
  213. EndPage = 5    
  214. Else    
  215. EndPage = DataSource.PageCount    
  216. End If    
  217. End If    
  218. If CurPage>1 Then    
  219. Response.Write "<a title='首页' href='"&GetUrl("page")"page=1'><font face=webdings>9</font></a> "    
  220. Response.Write "<a title='上页' href='"&GetUrl("page")"page="&CurPage-1"'><font face=webdings>3</font></a> "    
  221. Else    
  222. Response.Write "<font face=webdings>9</font> "    
  223. Response.Write "<font face=webdings>3</font> "    
  224. End If    
  225. For i=StartPage to EndPage    
  226. if i <> CurPage then    
  227. response.write("<a title='第"&i"页' href='"&GetUrl("page")"page="&i"'>"&i"</a> ")    
  228. Else    
  229. response.write("<b>"&i"</b> ")    
  230. End if    
  231. next    
  232. If CurPage<DataSource.PageCount Then    
  233. Response.Write "<a title='下页' href='"&GetUrl("&page&")"page="&CurPage+1"'><font face=webdings>4</font></a> "    
  234. Response.Write "<a title='尾页' href='"&GetUrl("&page&")"page="&DataSource.PageCount"'><font face=webdings>:</font></a> "    
  235. Else    
  236. Response.Write "<font face=webdings>4</font> "    
  237. Response.Write "<font face=webdings>:</font> "    
  238. End If    
  239. Response.Write " [共"&DataSource.RecordCount"条] ["&PageSize"条/页] [共"&DataSource.PageCount"页]"    
  240. Response.Write " PageExecute:"&Round((Timer-PageStart)*1000,2)" MS"    
  241. response.write("</td></tr>" & vbCrLf)    
  242. End if    
  243. 'Draw Table end    
  244. Response.Write("</table>")    
  245. End sub    
  246.   
  247. '====================================================================    
  248. '获取当前Url参数的函数    
  249. 'Codeing by Redsun    
  250. '====================================================================    
  251. Private Function GetUrl(RemoveList)    
  252. Dim ScriptAddress, M_ItemUrl, M_item    
  253. ScriptAddress = CStr(Request.ServerVariables("SCRIPT_NAME"))"?"'取得当前地址    
  254. M_ItemUrl = ""    
  255. For Each M_item In Request.QueryString    
  256. If InStr(RemoveList,M_Item)=0 Then    
  257. M_ItemUrl = M_ItemUrl & M_Item "="& Server.URLEncode(Request.QueryString(""&M_Item"")) & "&"    
  258. End If    
  259. Next    
  260. GetUrl = ScriptAddress & M_ItemUrl    
  261. End Function    
  262.   
  263.   
  264. '=============================    
  265. '实现列表排序    
  266. '返回Url参数并动态改变排序方式    
  267. '参数:需要进行排序的字段名,显示的名称    
  268. '=============================    
  269. Private Function Sort(SortStr,DispName)    
  270. If SortStr = "" Or DispName="" Then Exit Function    
  271. Sort = GetUrl("SOrder,SSort")    
  272. SSort = UCase(Request.QueryString("SSort"))    
  273. If SSort = "DESC" Then    
  274. SSort = "ASC"    
  275. Else    
  276. SSort = "DESC"    
  277. End If    
  278. Sort = "<a class='headhref' href='"&Sort"SOrder="&SortStr"&SSort="&SSort"'>"&DispName&SortType(SortStr)"</a>"    
  279. End Function    
  280.   
  281. '-----------------------------------------------    
  282. '标识排序列为升序还是降序方式    
  283. '参数:排序列字段名称    
  284. '-----------------------------------------------    
  285. Private Function SortType(FieldName)    
  286. Dim SOrderName    
  287. SOrderName = Request.QueryString("SOrder")    
  288. If SOrderName<>FieldName Then Exit Function    
  289. Dim SSortImg    
  290. SSortImg = Request.QueryString("SSort")    
  291. SortType = "<img src='/OrderFormSystem/images/"&SSortImg".gif' border='0'>"    
  292. End Function    
  293.   
  294. End Class    
  295.   
  296.   
  297. 'users Like { UserID,LoginName,Password,RealName,Age,Gender,}    
  298. 'initDB    
  299. Rs.Open "Select * from users",Cn    
  300. Dim tbGrid1    
  301. Set tbGrid1 = New TBGrid    
  302. Set tbGrid1.DataSource = Rs    
  303. tbGrid1.Columns.add "LoginName","用户名"    
  304. tbGrid1.HeadSort.add "LoginName",True    
  305. tbGrid1.Columns.add "Password","密码"    
  306. tbGrid1.AddTemplate "修改","<a href='aaa.asp?id={UserID}'><font color=red>{RealName}</font></a>"    
  307. tbGrid1.ItemStyle.add "Password","align=right"    
  308. tbGrid1.ItemStyle.add "修改","width=100"    
  309. tbGrid1.PageSize = 5    
  310. tbGrid1.AllowPageing = true    
  311. tbGrid1.PageingStyle = "align=right"    
  312. tbGrid1.Show()    
  313. 'CloseDB    
  314. %>    
  315.   
昨天新闻点击排行
一周新闻点击排行
当月新闻点击排行
新闻链接
上一篇文章:ASP缓存类
下一篇文章:ASP语法高亮类代码
相关评论
正在加载评论列表...
评论表单加载中...