自从用贯了.Net的DataGrid就再也懒得去用ASP画表格了,于是想了一个折中的办法,访照DataGrid的功能写了一个TBGrid 类,这样可以轻松的重用代码.比起每次都得重复劳动方便多了.希望能给用得到的人带去一些方便.用法很简单,看后面的例子便一目了然了.有什么不完善的地方希望大家有和我讨论.
在ASP 中实现ASP.Net 的DataGrid 功能<%
在ASP 中实现ASP.Net 的DataGrid 功能
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
在ASP 中实现ASP.Net 的DataGrid 功能'
            Programming By Smartpig                              '
在ASP 中实现ASP.Net 的DataGrid 功能'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
在ASP 中实现ASP.Net 的DataGrid 功能

在ASP 中实现ASP.Net 的DataGrid 功能Class TBGrid
在ASP 中实现ASP.Net 的DataGrid 功能
在ASP 中实现ASP.Net 的DataGrid 功能    
public DataSource                '数据源
在ASP 中实现ASP.Net 的DataGrid 功能
    public style                    '表格总风格
在ASP 中实现ASP.Net 的DataGrid 功能
    public HeadStyle                '表头风格
在ASP 中实现ASP.Net 的DataGrid 功能
    public HeadItemStyle            '表头单独风格
在ASP 中实现ASP.Net 的DataGrid 功能
    public itemStyle                '单元格独立网络
在ASP 中实现ASP.Net 的DataGrid 功能
    public Columns                    '需要显示的列元素
在ASP 中实现ASP.Net 的DataGrid 功能
    public Alternate                '是否交替风格
在ASP 中实现ASP.Net 的DataGrid 功能
    public AlternateStyle            '偶数行风格
在ASP 中实现ASP.Net 的DataGrid 功能
    public NormalStyle                '正常风格
在ASP 中实现ASP.Net 的DataGrid 功能
    public DefaultStyle                '默认风格簇
在ASP 中实现ASP.Net 的DataGrid 功能
    public PageSize                    '页大小
在ASP 中实现ASP.Net 的DataGrid 功能
    public CurPage                    '当前页
在ASP 中实现ASP.Net 的DataGrid 功能
    public AllowPageing                '是否分页
在ASP 中实现ASP.Net 的DataGrid 功能
    public PageingStyle                '页数风格
在ASP 中实现ASP.Net 的DataGrid 功能
    
在ASP 中实现ASP.Net 的DataGrid 功能    
Private Templates                '自定义单元项
在ASP 中实现ASP.Net 的DataGrid 功能
    
在ASP 中实现ASP.Net 的DataGrid 功能    
'内容之间的关系
在ASP 中实现ASP.Net 的DataGrid 功能
    'Columns.add "Field","HeadText"
在ASP 中实现ASP.Net 的DataGrid 功能
    'AddTemplate("HeadText",Template)
在ASP 中实现ASP.Net 的DataGrid 功能
    'itemStyle.add "Field","style:adsasd"
在ASP 中实现ASP.Net 的DataGrid 功能
    'DataSource(Columns.Keys(i))
在ASP 中实现ASP.Net 的DataGrid 功能
    
在ASP 中实现ASP.Net 的DataGrid 功能    
Private Sub Class_Initialize   ' 设置 Initialize 事件。
在ASP 中实现ASP.Net 的DataGrid 功能
        Set itemStyle     = CreateObject("Scripting.Dictionary")
在ASP 中实现ASP.Net 的DataGrid 功能        
Set HeadItemStyle= CreateObject("Scripting.Dictionary")
在ASP 中实现ASP.Net 的DataGrid 功能        
Set Columns         = CreateObject("Scripting.Dictionary")
在ASP 中实现ASP.Net 的DataGrid 功能        
Set Templates     = CreateObject("Scripting.Dictionary")
在ASP 中实现ASP.Net 的DataGrid 功能        
Set DataSource     = CreateObject("ADODB.Recordset")
在ASP 中实现ASP.Net 的DataGrid 功能        Alternate     
= 0
在ASP 中实现ASP.Net 的DataGrid 功能    
End Sub
在ASP 中实现ASP.Net 的DataGrid 功能    
在ASP 中实现ASP.Net 的DataGrid 功能    
Private Sub Class_Terminate   ' 设置 Terminate 事件。
在ASP 中实现ASP.Net 的DataGrid 功能
        Set itemStyle    = Nothing
在ASP 中实现ASP.Net 的DataGrid 功能        
Set HeadItemStyle = Nothing
在ASP 中实现ASP.Net 的DataGrid 功能        
Set Columns        = Nothing
在ASP 中实现ASP.Net 的DataGrid 功能        
Set DataSource    = Nothing
在ASP 中实现ASP.Net 的DataGrid 功能    
End Sub
在ASP 中实现ASP.Net 的DataGrid 功能    
在ASP 中实现ASP.Net 的DataGrid 功能    
Private Sub InitTable()
在ASP 中实现ASP.Net 的DataGrid 功能        
'Set FieldsNum    = DataSource.Fields.Count
在ASP 中实现ASP.Net 的DataGrid 功能
        'Set RowsNum     = DataSource.RecordCount
在ASP 中实现ASP.Net 的DataGrid 功能
        if Columns.Count = 0 then
在ASP 中实现ASP.Net 的DataGrid 功能            
For i = 0 to DataSource.Fields.Count -1
在ASP 中实现ASP.Net 的DataGrid 功能                Columns.add DataSource.Fields(i).Name,DataSource.Fields(i).Name
在ASP 中实现ASP.Net 的DataGrid 功能                response.Write(DataSource.Fields(i).Name)
在ASP 中实现ASP.Net 的DataGrid 功能            
Next
在ASP 中实现ASP.Net 的DataGrid 功能        
end if
在ASP 中实现ASP.Net 的DataGrid 功能        
在ASP 中实现ASP.Net 的DataGrid 功能        
if IsEmpty(Style) and IsEmpty(NormalStyle) then
在ASP 中实现ASP.Net 的DataGrid 功能            DefaultStyle 
= 1
在ASP 中实现ASP.Net 的DataGrid 功能        
end if
在ASP 中实现ASP.Net 的DataGrid 功能        
在ASP 中实现ASP.Net 的DataGrid 功能        
if PageSize = Empty then
在ASP 中实现ASP.Net 的DataGrid 功能            PageSize 
= 10
在ASP 中实现ASP.Net 的DataGrid 功能        
end if
在ASP 中实现ASP.Net 的DataGrid 功能        
在ASP 中实现ASP.Net 的DataGrid 功能        
select Case DefaultStyle
在ASP 中实现ASP.Net 的DataGrid 功能            
Case 1
在ASP 中实现ASP.Net 的DataGrid 功能                Style                
="border=1 cellpadding=2 cellspaccing=0 borderColor=#000000 style=""Border-collapse:collapse;font-size:12px"""
在ASP 中实现ASP.Net 的DataGrid 功能
                Alternate            = 1
在ASP 中实现ASP.Net 的DataGrid 功能                HeadStyle            
= "Height=25 bgColor=#CCCCCC"
在ASP 中实现ASP.Net 的DataGrid 功能
                AlternateStyle        = "bgColor=#EEEEEE height=20"
在ASP 中实现ASP.Net 的DataGrid 功能
                NormalStyle            = "height=20"
在ASP 中实现ASP.Net 的DataGrid 功能
                
在ASP 中实现ASP.Net 的DataGrid 功能            
Case Else
在ASP 中实现ASP.Net 的DataGrid 功能        
End Select
在ASP 中实现ASP.Net 的DataGrid 功能                
在ASP 中实现ASP.Net 的DataGrid 功能    
End sub
在ASP 中实现ASP.Net 的DataGrid 功能    
在ASP 中实现ASP.Net 的DataGrid 功能    
public Sub AddTemplate(ByVal ColumnName,ByVal Template)
在ASP 中实现ASP.Net 的DataGrid 功能        Columns.add ColumnName,ColumnName
在ASP 中实现ASP.Net 的DataGrid 功能        Templates.add ColumnName,Template
在ASP 中实现ASP.Net 的DataGrid 功能    
End Sub
在ASP 中实现ASP.Net 的DataGrid 功能
在ASP 中实现ASP.Net 的DataGrid 功能    
public Sub Show()
在ASP 中实现ASP.Net 的DataGrid 功能        InitTable()
在ASP 中实现ASP.Net 的DataGrid 功能        
在ASP 中实现ASP.Net 的DataGrid 功能        
Dim tableStr
在ASP 中实现ASP.Net 的DataGrid 功能        
Dim tdStart,tdEnd,tbStyle,tbContent
在ASP 中实现ASP.Net 的DataGrid 功能        
Dim curRow
在ASP 中实现ASP.Net 的DataGrid 功能        
Dim clm
在ASP 中实现ASP.Net 的DataGrid 功能        
Dim regEx,Match,Matches
在ASP 中实现ASP.Net 的DataGrid 功能           
在ASP 中实现ASP.Net 的DataGrid 功能        tableStr 
= "<table " & style & ">" & vbCrLF
在ASP 中实现ASP.Net 的DataGrid 功能        
在ASP 中实现ASP.Net 的DataGrid 功能        
'Draw Table Head
在ASP 中实现ASP.Net 的DataGrid 功能
        Response.Write(tableStr)
在ASP 中实现ASP.Net 的DataGrid 功能        Response.Write(
"<tr>")
在ASP 中实现ASP.Net 的DataGrid 功能        
for Each clm in Columns.Keys()
在ASP 中实现ASP.Net 的DataGrid 功能            tbStyle 
= HeadStyle & " " & HeadItemStyle(clm)
在ASP 中实现ASP.Net 的DataGrid 功能            tdStart 
= "<td " & tbStyle & ">"
在ASP 中实现ASP.Net 的DataGrid 功能
            tdEnd = "</td>"
在ASP 中实现ASP.Net 的DataGrid 功能
            
在ASP 中实现ASP.Net 的DataGrid 功能            Response.Write(tdStart)
在ASP 中实现ASP.Net 的DataGrid 功能            Response.Write(Columns(clm))
在ASP 中实现ASP.Net 的DataGrid 功能            Response.Write(tdEnd)
在ASP 中实现ASP.Net 的DataGrid 功能        
Next
在ASP 中实现ASP.Net 的DataGrid 功能        Response.Write(
"</tr>" & vbCrLF)
在ASP 中实现ASP.Net 的DataGrid 功能        
在ASP 中实现ASP.Net 的DataGrid 功能        
'Draw Table items
在ASP 中实现ASP.Net 的DataGrid 功能
        curRow = 1
在ASP 中实现ASP.Net 的DataGrid 功能        
if AllowPageing <> Empty then
在ASP 中实现ASP.Net 的DataGrid 功能            DataSource.PageSize 
= PageSize
在ASP 中实现ASP.Net 的DataGrid 功能        
else
在ASP 中实现ASP.Net 的DataGrid 功能            DataSource.PageSize 
= DataSource.RecordCount
在ASP 中实现ASP.Net 的DataGrid 功能        
end if
在ASP 中实现ASP.Net 的DataGrid 功能        
在ASP 中实现ASP.Net 的DataGrid 功能        
if CurPage = Empty then
在ASP 中实现ASP.Net 的DataGrid 功能            CurPage 
= 1
在ASP 中实现ASP.Net 的DataGrid 功能        
end if
在ASP 中实现ASP.Net 的DataGrid 功能        
在ASP 中实现ASP.Net 的DataGrid 功能        
if CurPage < 1 then
在ASP 中实现ASP.Net 的DataGrid 功能            DataSource.AbsolutePage 
= 1
在ASP 中实现ASP.Net 的DataGrid 功能        
end if
在ASP 中实现ASP.Net 的DataGrid 功能        
在ASP 中实现ASP.Net 的DataGrid 功能         
if CurPage >= DataSource.PageCount then
在ASP 中实现ASP.Net 的DataGrid 功能            DataSource.AbsolutePage 
= DataSource.PageCount
在ASP 中实现ASP.Net 的DataGrid 功能        
end if
在ASP 中实现ASP.Net 的DataGrid 功能        
在ASP 中实现ASP.Net 的DataGrid 功能        
if CurPage >= 1 and CurPage <= DataSource.PageCount then
在ASP 中实现ASP.Net 的DataGrid 功能            DataSource.AbsolutePage 
= CurPage
在ASP 中实现ASP.Net 的DataGrid 功能        
end if
在ASP 中实现ASP.Net 的DataGrid 功能
在ASP 中实现ASP.Net 的DataGrid 功能        
for curRow = 1 to DataSource.PageSize
在ASP 中实现ASP.Net 的DataGrid 功能            
if DataSource.EOF then
在ASP 中实现ASP.Net 的DataGrid 功能                
Exit For
在ASP 中实现ASP.Net 的DataGrid 功能            
end if
在ASP 中实现ASP.Net 的DataGrid 功能                        
在ASP 中实现ASP.Net 的DataGrid 功能            Response.Write(
"<tr>")
在ASP 中实现ASP.Net 的DataGrid 功能            
for Each clm in Columns.Keys()
在ASP 中实现ASP.Net 的DataGrid 功能                
if Alternate = 0 then
在ASP 中实现ASP.Net 的DataGrid 功能                    tbStyle 
= NormalStyle & " " & ItemStyle(clm)
在ASP 中实现ASP.Net 的DataGrid 功能                
else
在ASP 中实现ASP.Net 的DataGrid 功能                    
if curRow mod 2 = 0 then
在ASP 中实现ASP.Net 的DataGrid 功能                        tbStyle 
= AlternateStyle & " " & ItemStyle(clm)
在ASP 中实现ASP.Net 的DataGrid 功能                    
else
在ASP 中实现ASP.Net 的DataGrid 功能                        tbStyle 
= NormalStyle & " " & ItemStyle(clm)
在ASP 中实现ASP.Net 的DataGrid 功能                    
end if
在ASP 中实现ASP.Net 的DataGrid 功能                
end if
在ASP 中实现ASP.Net 的DataGrid 功能                
在ASP 中实现ASP.Net 的DataGrid 功能                tdStart 
= "<td " & tbStyle & ">"
在ASP 中实现ASP.Net 的DataGrid 功能
                tdEnd = "</td>"
在ASP 中实现ASP.Net 的DataGrid 功能
                
在ASP 中实现ASP.Net 的DataGrid 功能                
if Templates(clm) = Empty then
在ASP 中实现ASP.Net 的DataGrid 功能                    tbContent 
= DataSource(clm)
在ASP 中实现ASP.Net 的DataGrid 功能                
else
在ASP 中实现ASP.Net 的DataGrid 功能                    tbContent 
= Templates(clm)
在ASP 中实现ASP.Net 的DataGrid 功能                    
Set regEx = New RegExp
在ASP 中实现ASP.Net 的DataGrid 功能                    regEx.Pattern
= "{[A-Za-z0-9_-]+}"
在ASP 中实现ASP.Net 的DataGrid 功能
                    regEx.IgnoreCase = True
在ASP 中实现ASP.Net 的DataGrid 功能                    regEx.Global 
= True
在ASP 中实现ASP.Net 的DataGrid 功能                    
Set Matches=regEx.Execute(Templates(clm))
在ASP 中实现ASP.Net 的DataGrid 功能                    
For each match in matches
在ASP 中实现ASP.Net 的DataGrid 功能                        
On Error Resume Next
在ASP 中实现ASP.Net 的DataGrid 功能                        tbContent 
= Replace(tbContent,Match.Value, _DataSource(Mid(Match.Value,2,Len(Match.Value)-2)),1
在ASP 中实现ASP.Net 的DataGrid 功能                    
Next
在ASP 中实现ASP.Net 的DataGrid 功能                                        
在ASP 中实现ASP.Net 的DataGrid 功能                
end if
在ASP 中实现ASP.Net 的DataGrid 功能            
在ASP 中实现ASP.Net 的DataGrid 功能                Response.Write(tdStart)
在ASP 中实现ASP.Net 的DataGrid 功能                Response.Write(tbContent)
在ASP 中实现ASP.Net 的DataGrid 功能                Response.Write(tdEnd)
在ASP 中实现ASP.Net 的DataGrid 功能            
Next
在ASP 中实现ASP.Net 的DataGrid 功能            Response.Write(
"</tr>" & vbCrLF)
在ASP 中实现ASP.Net 的DataGrid 功能            
在ASP 中实现ASP.Net 的DataGrid 功能            DataSource.MoveNext
在ASP 中实现ASP.Net 的DataGrid 功能        
Next
在ASP 中实现ASP.Net 的DataGrid 功能        
在ASP 中实现ASP.Net 的DataGrid 功能        
'Draw Pageing Row
在ASP 中实现ASP.Net 的DataGrid 功能
        if DataSource.PageCount > 1 and LCase(pageingStyle) <> "none" then
在ASP 中实现ASP.Net 的DataGrid 功能            
Dim i
在ASP 中实现ASP.Net 的DataGrid 功能            response.write(
"<tr>")
在ASP 中实现ASP.Net 的DataGrid 功能            response.write(
"<td colspan=" & Columns.Count & " " & PageingStyle & ">")
在ASP 中实现ASP.Net 的DataGrid 功能            
for i=1 to DataSource.PageCount
在ASP 中实现ASP.Net 的DataGrid 功能                
if i <> CurPage then
在ASP 中实现ASP.Net 的DataGrid 功能                    response.write(
"<a href='" & Request.ServerVariables("SCRIPT_NAME"& "?page=" & i & "'>" )
在ASP 中实现ASP.Net 的DataGrid 功能                
end if
在ASP 中实现ASP.Net 的DataGrid 功能                response.write(i)
在ASP 中实现ASP.Net 的DataGrid 功能                
if i <> CurPage then
在ASP 中实现ASP.Net 的DataGrid 功能                    response.write(
"</a>")
在ASP 中实现ASP.Net 的DataGrid 功能                
end if
在ASP 中实现ASP.Net 的DataGrid 功能                response.write(
" ")
在ASP 中实现ASP.Net 的DataGrid 功能            
next
在ASP 中实现ASP.Net 的DataGrid 功能            response.write(
"</td></tr>" & vbCrLf)
在ASP 中实现ASP.Net 的DataGrid 功能        
end if
在ASP 中实现ASP.Net 的DataGrid 功能        
在ASP 中实现ASP.Net 的DataGrid 功能        
'Draw Table end
在ASP 中实现ASP.Net 的DataGrid 功能
        Response.Write("</table>")
在ASP 中实现ASP.Net 的DataGrid 功能        
在ASP 中实现ASP.Net 的DataGrid 功能    
end sub
在ASP 中实现ASP.Net 的DataGrid 功能
在ASP 中实现ASP.Net 的DataGrid 功能
End Class
在ASP 中实现ASP.Net 的DataGrid 功能
在ASP 中实现ASP.Net 的DataGrid 功能
'users Like { UserID,LoginName,Password,RealName,Age,Gender,在ASP 中实现ASP.Net 的DataGrid 功能}
在ASP 中实现ASP.Net 的DataGrid 功能'
initDB
在ASP 中实现ASP.Net 的DataGrid 功能'
Rs.Open "Select * from users",Cn
在ASP 中实现ASP.Net 的DataGrid 功能'
Dim tbGrid1
在ASP 中实现ASP.Net 的DataGrid 功能'
Set tbGrid1 = New TBGrid
在ASP 中实现ASP.Net 的DataGrid 功能'
Set tbGrid1.DataSource = Rs
在ASP 中实现ASP.Net 的DataGrid 功能'
tbGrid1.Columns.add "LoginName","用户名"
在ASP 中实现ASP.Net 的DataGrid 功能'
tbGrid1.ItemStyle.add "Password","align=right"
在ASP 中实现ASP.Net 的DataGrid 功能'
tbGrid1.ItemStyle.add "修改","width=100"
在ASP 中实现ASP.Net 的DataGrid 功能'
tbGrid1.AddTemplate "修改","<a href='aaa.asp?id={UserID}'><font color=red>{RealName}</font></a>"
在ASP 中实现ASP.Net 的DataGrid 功能'
tbGrid1.Columns.add "Password","密码"
在ASP 中实现ASP.Net 的DataGrid 功能'
tbGrid1.PageSize = 5
在ASP 中实现ASP.Net 的DataGrid 功能'
tbGrid1.AllowPageing = true
在ASP 中实现ASP.Net 的DataGrid 功能'
tbGrid1.PageingStyle = "align=right"
在ASP 中实现ASP.Net 的DataGrid 功能'
tbGrid1.CurPage = CInt(Request("page"))
在ASP 中实现ASP.Net 的DataGrid 功能'
tbGrid1.Show()
在ASP 中实现ASP.Net 的DataGrid 功能'
CloseDB
在ASP 中实现ASP.Net 的DataGrid 功能

在ASP 中实现ASP.Net 的DataGrid 功能%
>

相关文章: