威凡网全力打造:网页编程、软件开发编程、平面设计、服务器端开发、操作系统等在线学习平台!学编程,上威凡网!
ASP教程>> ASP基础 应用技巧 数据库相关 ASP类 存储过程 FSO专栏 ASP其他
当前位置:首页 > ASP教程 > ASP类
上一节 下一节
 ASP数据岛操作类
<%
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'            programming by smartpig                              '
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
class tbgrid
    public datasource                '数据源
    public style                     '表格总风格
    public headstyle                 '表头风格
    public headitemstyle             '表头单独风格
    public itemstyle                 '单元格独立网络
    public headsort      '表头是否显示排序功能
    public columns                   '需要显示的列元素
    public alternate                 '是否交替风格
    public alternatestyle            '偶数行风格
    public normalstyle               '正常风格
    public defaultstyle              '默认风格簇
    public pagesize                  '页大小
    public allowpageing              '是否分页
    public pageingstyle              '页数风格

    private templates                '自定义单元项
    private curpage                   '当前页
    private pagestart     '页面开始运行时间

    '内容之间的关系
    'columns.add "field","headtext"
    'addtemplate("headtext",template)
    'itemstyle.add "field","style:adsasd"
    'headsort.add "field",true
    'datasource(columns.keys(i))

    private sub class_initialize   ' 设置 initialize 事件。
        set itemstyle  = createobject("scripting.dictionary")
        set headsort  = createobject("scripting.dictionary")
        set headitemstyle = createobject("scripting.dictionary")
        set columns   = createobject("scripting.dictionary")
        set templates  = createobject("scripting.dictionary")
        set datasource  = createobject("adodb.recordset")
        alternate   = 0
        pagestart = timer
    end sub

    private sub class_terminate   ' 设置 terminate 事件。
        set itemstyle  = nothing
        set headsort  = nothing
        set headitemstyle = nothing
        set columns   = nothing
        set datasource  = nothing
    end sub

    private sub inittable()
        'set fieldsnum    = datasource.fields.count
        'set rowsnum     = datasource.recordcount
        if columns.count = 0 then
            for i = 0 to datasource.fields.count -1
                columns.add datasource.fields(i).name,datasource.fields(i).name
                response.write(datasource.fields(i).name)
            next
        end if

        if isempty(style) and isempty(normalstyle) then
            defaultstyle = 1
        else
            defaultstyle = style
        end if

        curpage = cint(request.querystring("page"))
        if curpage = "" then
            curpage = 1
        end if

        if pagesize = empty then
            pagesize = 10
        end if

        select case defaultstyle
        case 1
            style     ="align=center border=0 cellpadding=4 cellspacing=1 bgcolor='#cccccc'"
            alternate    = 1
            headstyle    = "height=25 style=""background-color:#006699;color:#ffffff"""
            alternatestyle   = "bgcolor=#ffffff height=25"
            normalstyle    = "height=25 bgcolor=#f5f5f5"
            allowpageing   = true
            tbgrid1.pageingstyle = "bgcolor='#f5f5f5' align='right'"
        case 2
            style     ="align=center border=0 cellpadding=4 cellspacing=1 bgcolor='#cccccc'"
            alternate    = 0
            headstyle    = "height=25 style=""background-color:#ffffff"""
            alternatestyle   = "bgcolor=#ffffff height=25"
            normalstyle    = "height=25 bgcolor=#ffffff"
        case else
        end select
    end sub

    public sub addtemplate(byval columnname,byval template)
        columns.add columnname,columnname
        templates.add columnname,template
    end sub

    public sub show()
        inittable()
        dim tablestr
        dim tdstart,tdend,tbstyle,tbcontent
        dim currow
        dim clm
        dim regex,match,matches
        tablestr = "<table "&style&">" & vbcrlf
        'draw table head
        response.write(tablestr)
        response.write("<tr>")
        for each clm in columns.keys()
            tbstyle = headstyle & " " & headitemstyle(clm)
            tdstart = "<th "&tbstyle&">"
            tdend = "</th>" & vbcrlf

            response.write(tdstart)
            '加入表头排序功能
            'code by redsun
            'date:2005-1-17
            if headsort(clm) then
                response.write sort(clm,columns(clm))
            else
                response.write(columns(clm))
            end if
            response.write(tdend)
        next
        response.write("</tr>" & vbcrlf)

        'draw table items
        currow = 1
        if allowpageing <> empty then
            datasource.pagesize = pagesize
        else
            datasource.pagesize = datasource.recordcount
        end if

        if curpage < 1 then
            datasource.absolutepage = 1
        end if

        if curpage >= datasource.pagecount then
            datasource.absolutepage = datasource.pagecount
        end if

        if curpage >= 1 and curpage <= datasource.pagecount then
            datasource.absolutepage = curpage
        end if

        for currow = 1 to datasource.pagesize
            if datasource.eof then
                exit for
            end if

            response.write("<tr>")
            for each clm in columns.keys()
                if alternate = 0 then
                    tbstyle = normalstyle & " " & itemstyle(clm)
                else
                    if currow mod 2 = 0 then
                        tbstyle = alternatestyle & " " & itemstyle(clm)
                    else
                        tbstyle = normalstyle & " " & itemstyle(clm)
                    end if
                end if

                tdstart = "<td "&tbstyle&">"
                tdend = "</td>" & vbcrlf

                if templates(clm) = empty then
                    tbcontent = datasource(clm)
                else
                    tbcontent = templates(clm)
                    set regex = new regexp
                    regex.pattern= "{[a-za-z0-9_-]+}"
                    regex.ignorecase = true
                    regex.global = true
                    set matches=regex.execute(templates(clm))
                    for each match in matches
                        on error resume next
                        tbcontent = replace(tbcontent,match.value,datasource(mid(match.value,2,len(match.value)-2)),1) 
                    next

                end if

                response.write(tdstart)
                response.write(tbcontent)
                response.write(tdend)
            next
            response.write("</tr>" & vbcrlf)

            datasource.movenext
        next

        'draw pageing row
        if datasource.pagecount > 1 and lcase(pageingstyle) <> "none" then
            dim i,endpage,startpage
            response.write("<tr>")
            response.write("<td colspan=" & columns.count & " " & pageingstyle & ">")
            '改进分页功能
            'code by redsun
            'date:2005-1-17
            if curpage>4 then
                if curpage+2<datasource.pagecount then
                    startpage = curpage-2
                    endpage = curpage+2
                else
                    startpage = datasource.pagecount-4
                    endpage = datasource.pagecount
                end if
            else
                startpage = 1
                if datasource.pagecount>5 then
                    endpage = 5
                else
                    endpage = datasource.pagecount
                end if
            end if
            if curpage>1 then
                response.write "<a title='首页' href='"&geturl("page")&"page=1'><font face=webdings>9</font></a> "
                response.write "<a title='上页' href='"&geturl("page")&"page="&curpage-1&"'><font face=webdings>3</font></a> "
            else
                response.write "<font face=webdings>9</font> "
                response.write "<font face=webdings>3</font> "
            end if 
            for i=startpage to endpage
                if i <> curpage then
                    response.write("<a title='第"&i&"页' href='"&geturl("page")&"page="&i&"'>"&i&"</a> ")
                else
                    response.write("<b>"&i&"</b> ")
                end if
            next
            if curpage<datasource.pagecount then
                response.write "<a title='下页' href='"&geturl("&page&")&"page="&curpage+1&"'><font face=webdings>4</font></a> "
                response.write "<a title='尾页' href='"&geturl("&page&")&"page="&datasource.pagecount&"'><font face=webdings>:</font></a> "
            else
                response.write "<font face=webdings>4</font> "
                response.write "<font face=webdings>:</font> "
            end if
            response.write "   [共"&datasource.recordcount&"条] ["&pagesize&"条/页] [共"&datasource.pagecount&"页]"
            response.write " pageexecute:"&round((timer-pagestart)*1000,2)&" ms"
            response.write("</td></tr>" & vbcrlf)
        end if
        'draw table end
        response.write("</table>")
    end sub

    '====================================================================
    '获取当前url参数的函数
    'codeing by redsun
    '====================================================================
    private function geturl(removelist)
        dim scriptaddress, m_itemurl, m_item
        scriptaddress = cstr(request.servervariables("script_name"))&"?"'取得当前地址
        m_itemurl = ""
        for each m_item in request.querystring
            if instr(removelist,m_item)=0 then
                m_itemurl = m_itemurl & m_item &"="& server.urlencode(request.querystring(""&m_item&"")) & "&"
            end if
        next
        geturl = scriptaddress & m_itemurl
    end function

    
    '=============================
    '实现列表排序
    '返回url参数并动态改变排序方式
    '参数:需要进行排序的字段名,显示的名称
    '=============================
    private function sort(sortstr,dispname)
        if sortstr = "" or dispname="" then exit function
        sort = geturl("sorder,ssort")
        ssort = ucase(request.querystring("ssort"))
        if ssort = "desc" then
            ssort = "asc" 
        else
            ssort = "desc"
        end if
        sort = "<a class='headhref' href='"&sort&"sorder="&sortstr&"&ssort="&ssort&"'>"&dispname&sorttype(sortstr)&"</a>"
    end function

    '-----------------------------------------------
    '标识排序列为升序还是降序方式
    '参数:排序列字段名称
    '-----------------------------------------------
    private function sorttype(fieldname)
        dim sordername
        sordername = request.querystring("sorder")
        if sordername<>fieldname then exit function
        dim ssortimg
        ssortimg = request.querystring("ssort")
        sorttype = "<img src='/orderformsystem/images/"&ssortimg&".gif' border='0'>"
    end function

end class


'users like { userid,loginname,password,realname,age,gender,}
'initdb
rs.open "select * from users",cn
dim tbgrid1
set tbgrid1 = new tbgrid
set tbgrid1.datasource = rs
tbgrid1.columns.add "loginname","用户名"
tbgrid1.headsort.add "loginname",true
tbgrid1.columns.add "password","密码"
tbgrid1.addtemplate "修改","<a href='aaa.asp?id={userid}'><font color=red>{realname}</font></a>"
tbgrid1.itemstyle.add "password","align=right"
tbgrid1.itemstyle.add "修改","width=100"
tbgrid1.pagesize = 5
tbgrid1.allowpageing = true
tbgrid1.pageingstyle = "align=right"
tbgrid1.show()
'closedb
%>



申明:本教程内容由威凡网编辑整理并提供IT程序员分享学习,如文中有侵权行为,请与站长联系(QQ:254677821)!
上一节 下一节
相关教程  
其他教程  
ASP基础
应用技巧
数据库相关
ASP类
存储过程
FSO专栏
ASP其他

违法和不良信息举报中心】邮箱:254677821@qq.com
Copyright©威凡网 版权所有 苏ICP备2023020142号
站长QQ:254677821