超酷的通用分页显示控制 (五) 添加、删除、修改记录、类别设置(上)

发表于:2007-06-30来源:作者:点击数: 标签:
到现在为止,我们做的工作仅仅只是把记录显示出来。 如果我们要在这个 ASP 页面中,添加、删除记录,在新窗口中修改记录,新问题就来了。 这在我们设计类似于允许用户管理自己的通讯录一类的程序时,经常会遇到。 幸好我们在前面的分页控制中已经作好了一切准
到现在为止,我们做的工作仅仅只是把记录显示出来。
如果我们要在这个 ASP 页面中,添加、删除记录,在新窗口中修改记录,新问题就来了。

这在我们设计类似于允许用户管理自己的通讯录一类的程序时,经常会遇到。

幸好我们在前面的分页控制中已经作好了一切准备,我们现在只需要做如下工作:
1、在每行记录前添加 checkbox
2、在记录列表的下面添加“选择全部记录”复选框、删除、添加按钮

如果在查询控制中显示了类别列表,则还要增加“类别设置”按钮。


当然,我们只需要修改 listrecords() 函数,以及 listrecordscon.inc 文件


请看 sample5.asp




<一> 需要分页的 ASP 文件

sample5.asp


<!--#include file="../inc/functions.inc"-->
<%
    ‘’//////////////////////////////////////////////////////////
    ‘’
    ‘’ 定义表名
    ‘’
    ‘’//////////////////////////////////////////////////////////

    theTableName= "addressbook"

    ‘’//////////////////////////////////////////////////////////
    ‘’
    ‘’ 查询条件
    ‘’
    ‘’//////////////////////////////////////////////////////////

    theQueryField    = "fld" & theTableName & "_nickname"‘’ 查询字段,完整名字
    theQueryTitle    = "昵称"                            ‘’ 字段显示标题
    theQueryTable    = "vw" & theTableName                ‘’ 字段所在的表,完整名字

    theQueryClass    = theTableName & "_class"            ‘’ 类别表名,去掉 tbl、vw 前缀
    theClassId        = c2int(request("classid"))            ‘’ 当前类别号

    ‘’ 如果是查询模式,则构造模糊查询语句
    if request("mode") = "query" then
        %><!--#include file="../inc/control/query_result.inc"--><%
    else
    ‘’ 否则忽略
        theQueryCon = "1>0"
    end if

    ‘’//////////////////////////////////////////////////////////
    ‘’
    ‘’ 限制条件
    ‘’
    ‘’//////////////////////////////////////////////////////////

    theLimitCon    = "fld" & theTableName & "_userid=" & Session("userid")

    if theClassId > 0 then
        theLimitCon = theLimitCon & " and fld" & theQueryClass & "id=" & theClassId
    end if

    ‘’//////////////////////////////////////////////////////////
    ‘’
    ‘’ 构造 SQL 语句
    ‘’
    ‘’//////////////////////////////////////////////////////////

    uSQL = "select * from " & theQueryTable & " where ( " & theQueryCon & " ) and ( " & theLimitCon & " ) "

%>
<!--#include file="../inc/control/navigator_init.inc"-->

<html>
<head>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link rel="stylesheet" href="../default.css" type="text/css">
</head>

                      <!-- 你的 HTML 代码//-->

                      <table width="100%" border="0" cellspacing="0" cellpadding="0">
                        <tr>
                          <td>
<!--查询控制-->
<!--#include file="../inc/control/query.inc"-->
                          </td>
                          <td>
<!--导航控制-->
<!--#include file="../inc/control/navigator.inc"-->
                          </td>
                        </tr>
                      </table>

                      <!-- 你的 HTML 代码//-->

                      <table width="100%" border="0" cellspacing="0" cellpadding="0">
                        <tr>
                          <td>
<form name="form_main" method="post" action="<%=theScript%>?p=1" onsubmit="javascript:return CheckForm(this);">
    <%
    ‘’ 定义记录集显示方式

    theViewType    = "view"
    theFields    = "fld" & theTableName & "_id,fld" & theTableName & "_class_title,fldaddressbook_nickname"
    theTitles    = "序号,类别,昵称"
    theAligns    = ",,m,"                ‘’ 对齐方式                :m 中 / r 右 / 缺省:左
    theTypes    = ",,,"                    ‘’ 字段类型                :l 逻辑 / 缺省:字符串
    theWidths    = "6%,14%,80%"            ‘’ 字段宽度                :
    theLenths    = "0,8,0"                ‘’ 字段可容纳的字符长度    :0 不指定
    theLinkASP    = "0,0,1"                ‘’ 是否显示链接            :0 不显示 / 1 显示
    theEnterLine= "0,0,0"                ‘’ 是否换行显示            :0 不换行 / 1 换行
    theIsAdd    = 0                        ‘’ 记录集序号显示方式    :0 递减/ 1 递增
    theCheckbox    = 1                        ‘’ 是否显示 Checkbox        :0 不显示 / 1 显示
    listRecords theFields,theTitles,theAligns,theTypes,theWidths,theLenths,theLinkASP,theEnterLine,theIsAdd,theViewType,theCheckbox
    %>
                          <table width="100%" border="0" cellspacing="0" cellpadding="0" height="30">
                            <tr>
                              <td>
    <%
    theConType    = "1,2"                    ‘’ 控制类型:1 删除 / 2 添加
    theWinW        = 300                    ‘’ 查看/编辑窗口的大小
    theWinH        = 210
    theWinW_cls    = 470                    ‘’ 类别设置窗口的大小
    theWinH_cls    = 300
    %>
    <!--显示功能按钮//-->
    <!--#include file="../inc/control/listrecordscon.inc"-->
                              </td>
                            </tr>
                         </table>
</form>
                          </td>
                        </tr>
                      </table>
<!--#include file="../inc/control/listrecordscon.inc"-->

                      <!-- 你的 HTML 代码//-->

</body>
</html>



<二> 函数集:

修改 function.inc 中的 listrecords()

<%
‘’--------------------------------------------------------
‘’Name:            ListRecords
‘’Argument:        uFields                    列元素数组,第一个必须是 ID 字段
‘’                uTitles                    列标题
‘’                uWidth                    列宽度数组
‘’                uLenth                    每列可容纳的字符串数
‘’                uLinkasp                链接地址
‘’                uEnterLine                是否换行显示
‘’                uIsAdd                    序号显示方式:    0 递减 / 1 递增
‘’                uViewType                查看方式
‘’                uCheckbox                是否显示 Checkbox,1显示/0不显示
‘’Return:
‘’Description:   列出记录集
‘’                rs、conn、theCurrentPageNum 为全局变量,在 navigator_begin.inc 中定义
‘’--------------------------------------------------------

Sub listRecords(byval uFields, byval uTitles, byval uAligns, byval uTypes, byval uWidths, byval uLenths, byval uLinkasp, byval uEnterLine,byval uIsAdd,byval uViewType, byval uCheckbox)
    Dim uNum, uMod, i, j, uStrTemp, uStr, uHeight, uLen, uLeft, uCols
    Dim uTmp1, uTmp2, uRecName, uFirstLine

    uHeight            = 20        ‘’ 行高
    uWidth_Num        = 40        ‘’ 序号列宽度
    uWidth_checkbox    = 20        ‘’ Checkbox 列宽度

    uFields            = split(uFields,",")
    uTitles            = split(uTitles,",")
    uAligns            = split(uAligns,",")
    uTypes            = split(uTypes,",")
    uWidths            = split(uWidths,",")
    uLenths            = split(uLenths,",")
    uLinkasp        = split(uLinkasp,",")
    uCols            = ubound(uFields)
    uEnterLine        = split(uEnterLine,",")

    uStr            = ""

    ‘’ 隐含域 theActionScript,保存当前页面的文件名,但不包括路径和扩展名 .asp
    uStr = uStr & "<input type=""hidden"" name=""theActionScript"" value=""" & theActionScript & """>" & vbCrLf
    ‘’ 隐含域 theScript,保存当前页面的完整文件名
    uStr = uStr & "<input type=""hidden"" name=""theScript"" value=""" & theScript & """>" & vbCrLf

    ‘’ 标题栏

    uStr = uStr & "<table width=‘’100%‘’ border=‘’0‘’ cellspacing=‘’1‘’ cellpadding=‘’3‘’ bgcolor=‘’#000000‘’>" & vbCrLf
    uStr = uStr & "  <tr bgcolor=‘’#FFFFFF‘’> " & vbCrLf
    uStr = uStr & "    <td>" & vbCrLf
    uStr = uStr & "      <table width=‘’100%‘’ border=‘’0‘’ cellspacing=‘’0‘’ cellpadding=‘’0‘’>" & vbCrLf
    uStr = uStr & "        <tr>" & vbCrLf

    if uCheckbox = 1 then
        uStr = uStr & "          <td width=" & uWidth_Checkbox & "> </td>" & vbCrLf
    end if
    
    for i = 0 to UBound(uFields)
        uStr = uStr & "          <td width=‘’" & uWidths(i) & "‘’>" & uTitles(i) & "</td>" & vbCrLf
    next

    uStr = uStr & "        </tr>" & vbCrLf
    uStr = uStr & "      </table>" & vbCrLf
    uStr = uStr & "    </td>" & vbCrLf
    uStr = uStr & "  </tr>" & vbCrLf
    uStr = uStr & "  <tr bgcolor=‘’#FFFFFF‘’> " & vbCrLf
    uStr = uStr & "    <td>" & vbCrLf
    uStr = uStr & "        <table width=100% border=0 cellspacing=0 cellpadding=0>" & vbCrLf

    if rs.recordcount = 0 then
        j = 1
        uStr = uStr & "<tr height=" & uHeight & "><td cols=" & uCols & ">没有记录。</td></tr>" & vbCrLf
    else
        ‘’ 内容列表
        uFirstLine = 1
        For j = 1 To rs.pagesize

            ‘’ 计算序号

            if ( j mod 2 ) = 0 then
                uBgColor = " bgcolor=#efefef"
            else
                uBgColor = ""
            end if

            if uIsAdd = 1 then
                ‘’升序排列
                uNum = ( theCurrentPageNum - 1 ) * rs.pagesize + j
            else
                ‘’倒序排列
                uMod = rs.recordcount Mod rs.pagesize
                If uMod = 0 Then
                    uMod = rs.pagesize
                End If

                uNum = (rs.pagecount - theCurrentPageNum) * rs.pagesize + uMod - j + 1
            end if

            ‘’ 行开始
            uStr = uStr & "<tr valign=top height=" & uHeight & uBgColor & ">" & vbCrLf

            if uFirstLine = 1 then
                uWidthStr    = " width=" & uWidth_Checkbox
            else
                uWidthStr    = ""
            end if

            ‘’ 是否显示 checkbox
            if uCheckbox = 1 then
                uStr = uStr & "<td " & uWidthStr & "><input type=checkbox name=chk value=" & rs(uFields(0)) & ">" & "</td>" & vbCrLf
            end if

            if uFirstLine = 1 then
                uWidthStr    = " width=" & uWidths(0)
            else
                uWidthStr    = ""
            end if

            uStr = uStr & "<td " & uWidthStr & ">" & uNum & "</td>" & vbCrLf

            ‘’ 显示指定字段

            For i = 1 to ubound(uFields)
                ‘’ 根据字段类型,返回不同的处理结果
                uStrTemp = getValue(rs(uFields(i)),uTypes(i))

                uLen = cint(uLenths(i))

                if uFirstLine = 1 then
                    uWidthStr    = " width=" & uWidths(i)
                else
                    uWidthStr    = ""
                end if

                if uAligns(i) <> "" then
                    uAlignStr = " align=" & getAlign(uAligns(i))
                else
                    uAlignStr = ""
                end if
                ‘’ 只显示指定长度的内容
                if uLen > 0 then
                    if lenX(uStrTemp) > uLen then
                        ‘’ 是否折行显示
                        if uEnterLine(i) = 1 then
                            uStrTemp = paragraph(uStrTemp,uLen,"<br>")
                        else
                            uStrTemp = leftX(uStrTemp,uLen) & "..."
                        end if
                    end if
                end if

                ‘’ 添加链接
                ‘’ 使用 return false ,当用户单击链接时,当前页面光标将保持原状,否则将回到页面起始位置

                If cint(uLinkasp(i)) = 1 Then
                    uStrTemp = "<a href=""#"" onclick=""javascript:" & uViewType & "(" & rs(uFields(0)) & "," & theCurrentPageNum & ");return false;"" >" & uStrTemp & "</a>"
                End If

                uStr = uStr & "<td" & uWidthStr & uAlignStr & ">" & uStrTemp & "</td>" & vbCrLf
            Next

            ‘’ 行结束
            uStr = uStr & "</tr>" & vbCrLf

            uFirstLine = 0
            rs.MoveNext

            If rs.EOF Then
                Exit For
                rsClose rs
                cnClose conn
            End If
        Next

    end if

    ‘’ 剩余的行数用空行填充
    uLeft = rs.pagesize - j - 1

    ‘’ 填充
    for i = 0 to uLeft
        uStr = uStr & "<tr height=" & uHeight & "><td cols=" & uCols & "></td></tr>" & vbCrLf
    next
    
    ‘’ 表结束
    uStr = uStr & "        </table>" & vbCrLf
    uStr = uStr & "    </td>" & vbCrLf
    uStr = uStr & "  </tr>" & vbCrLf
    uStr = uStr & "</table>" & vbCrLf

    ‘’ 显示表格
    response.write uStr
End Sub

%>



原文转自:http://www.ltesting.net