ASP模仿asp.net的DataGrid

2005-05-20 10:35:42  作者

自动生成表格,自动完成删除,编辑、填加、分页功能,自定义样式表头样式

代码用两个类来实现

一开始考虑得太多,功能想得太强大,通用性越强,asp类跑起来越慢,做到后来没爱好,还有很多功能没有完成,如字段类型验证,显示图片、控件等,帖出代码供大这一起学习研究,有爱好的可以将这些功能加上

示例

set a = new DataGrid
'a.Connstr="Provider=SQLOLEDB.1;User ID=sa;Password=servser;Initial Catalog=temp_blue;Data Source=server;Connect Timeout=30;Auto Translate=True;Packet Size=4096;"
a.Connstr="Provider=Microsoft.Jet.OLEDB.4.0;"%26amp;" Data Source="%26amp;server.mappath("test.mdb") '连接ACCSS字符串
a.SQLString="select * from table1" '生成datagrid所显示的记录集的sql语句
a.isAddnew = 1 '是否可以填加新记录
a.Table = "table1" 'datagrid控制的主表
a.UniqueField = "ID" '标志字段,所有记录不重复整型即可
a.PagePosition = "down" '分页显示位置,up上面,down下面 updown上下 ,其它为不显示
a.pagesize = 5 '每页显示记录数
a.Pagenumber = 10 '显示页数

a.BorderColor="#ff0000" '默认为效果图显示

a.BackGround="#00ff00" '默认为效果图显示

a.BorderWidth=1 默认为1

a.
set b1 = new column
b1.Field = "id" '此列所绑定的数据库字段
b1.Title = "标志" '标题
b1.Align = "center" ' 对齐方式
a.AddColumn(b1) '把此列插入到datagrid
set b2 = new column
b2.Field="firstname"
b2.Title="姓"
a.AddColumn(b2)

set b3 = new column
b3.Field = "lastname"
b3.Title = "名"
a.AddColumn(b3)

set b4 = new column
b4.Field = "logintimes"
b4.Title = "登陆次数"
b4.ReadOnly = true '设为只读,不会出现在编辑框中和新增记录中
a.AddColumn(b4)

set b5 = new column
b5.Title="编辑"
b5.Columntype ="edit" '编辑列
b5.EditCommandText = "编辑" '编辑按钮文本
a.AddColumn(b5)

set b6 = new column
b6.align = "center"
b6.Width = 200
b6.Columntype = "delete"
b6.DeleteCommandText = "删除按钮"
b6.Title ="删除"
a.AddColumn(b6)
a.CreateGrid()

set b1 = nothing
set b2 = nothing
set b3 = nothing
set b4 = nothing
set b5 = nothing
set b6 = nothing


类文件如下

%26lt;%Class DataGrid
Private pages
Private strSQLString
Public Connstr
Private Columns
Private index
Private strUniqueField,strTable
Private rs
Private strCellspacing,strCellpadding,strCssClass
Private strBorderColorDark,strBorderColorLight,strBackGroundColor
Private intBorderWidth
Private strHeadStyle,strHeadBackgroudColor
Private strStyle,strAlternateStyle
Private UniqueKey,dg_action,currPage
Private actionURL,pageURL,operationURL,formURL
Public PagePosition,Pagesize,Pagenumber

Public isAddnew

Private Sub Class_Initialize()
set Columns = Server.CreateObject("Scripting.Dictionary")
index = 0
Pagesize = 10
Pagenumber = 10
PagePosition = "updown"
strSQLString = Session("DSN")
uniquekey = Request("uniquekey")
dg_action = Request("dg_action")
currPage = Request("Page")
actionURL = Request.ServerVariables("Script_name") %26amp; "?page=" %26amp; currPage
if dg_action= "edit" then formURL = actionURL%26amp; "%26amp;dg_action=update%26amp;uniquekey="%26amp;uniquekey
operationURL = Request.ServerVariables("Script_name") %26amp; "?page=" %26amp; currPage%26amp; "%26amp;uniquekey=" %26amp; uniquekey
pageURL = Request.ServerVariables("Script_name")%26amp;"?1=1"
if currPage = "" or isnull(currPage) then currPage = 1

strBorderColorDark ="#f7f7f7"
strBorderColorLight = "#cccccc"
strBackgroundColor = "#f7f7f7"
strHeadBackgroudColor = "#F2F2F2"
intBorderWidth = 1
strAlternateStyle ="bgcolor=#f6f6f6"
isAddnew = 1
Set rs = Server.CreateObject("Adodb.Recordset")

End Sub

Private Sub Class_Terminate()
rs.close
set rs = nothing
set Columns = nothing
End Sub

Public Property Get SQLString()
SQLString = strSQLString
End Property

Public Property Let SQLString(Value)
strSQLString = Value
End Property


Public Property Let Style(Value)
strStyle = Value()
End Property

Public Property Get Style()
Style = strStyle
End Property

Public Property Let UniqueField(Value)
strUniqueField = lcase(Value)
End Property

Public Property Get UniqueField()
UniqueField = strUniqueField
End Property

Public Property Let Table(Value)
strTable = lcase(Value)
End Property

Public Property Get Table()
Table = strTable
End Property

Public Property Let DbConn(Value)
strConn = Value
End Property

Public Property Get Version()
Version = "1.0"
End Property

Public Property Let Cellspacing(Value)
strcellspacing = Value
End Property

Public Property Get Cellspacing()
Cellspacing = strcellspacing
End Property

Public Property Let cellpadding(Value)
strcellpadding = Value
End Property

Public Property Get cellpadding()
cellpadding = strCellspacing
End Property


Public Property Let CssClass(Value)
strCssClass = Value
End Property

Public Property Get CssClass()
CssClass = strCssClass
End Property

Public Property Let BorderColor(value)
strBorderColorDark = value
End Property

Public Property Get BorderColor()
BorderColor = strBorderColorDark
End Property

Public Property Let BackGround(value)
strBorderColorDark = value
strBackgroundColor = value
End Property

Public Property Get BackGround()
BackGround = strBorderColorLight
End Property

Public Property Let BorderWidth(value)
intBorderWidth = value
End Property

Public Property Get BorderWidth()
BorderWidth = intBorderWidth
End Property

Public Property Get nColumns(intIndex)
nkeys = Columns.Keys
nItems = Columns.Items
for i = 0 to Columns.Count - 1
if intIndex = nkeys(i) then
set tmp = nItems(i)
end if
next
set nColumns = tmp
End Property

Private Function page(totalpage,pagenumber,thisPage)
MinPage = thisPage - pagenumber/2
if MinPage %26lt;= 0 then MinPage = 1
'if MinPage + pagenumber/2 %26gt; totalpage then Maxpage = totalpage else Maxpage = MinPage + pagenumber
for i = MinPage to MinPage + pagenumber -1
if i %26lt;= totalpage then
if cint(thisPage)%26lt;%26gt; cint(i) then
strtemp = strtemp %26amp; "%26lt;a href="%26amp;pageURL%26amp;"%26amp;page=" %26amp; i %26amp;"%26gt;" %26amp; i %26amp; "%26lt;/a%26gt; "
else
strtemp = strtemp %26amp; i%26amp;" "
end if
else
page = strtemp
Exit Function
end if
Next
page = strtemp
End Function

Public Sub CreateGrid()
nkeys = Columns.Keys
nItems = Columns.Items
If dg_action="update" then
dim strsql
strsql = "update "%26amp; table %26amp; " set "
dim j
j=0
For i = 0 to index - 1
if nItems(i).Columntype%26lt;%26gt;"edit" and nItems(i).Columntype %26lt;%26gt; "delete" and not nItems(i).readonly and nItems(i).field%26lt;%26gt;uniquefield then
if j %26lt;%26gt; 0 then strsql = strsql %26amp; ","
 select case nItems(i).DataType
Case "text"
strsql = strsql %26amp; nItems(i).field %26amp; "='" %26amp; Request(nItems(i).field)%26amp;"' "
Case "number","int","bigint","tinyint"
strsql = strsql %26amp; nItems(i).field %26amp; "=" %26amp; Request(nItems(i).field) %26amp; " "
Case "date","time","datetime"
strsql = strsql %26amp; nItems(i).field %26amp; "=convert(datetime,'" %26amp; Request(nItems(i).field)%26amp;"',102) "
Case else
strsql = strsql %26amp; nItems(i).field %26amp; "='" %26amp; Request(nItems(i).field)%26amp;"' "
End select
j=j+1
End if

Next
strsql = strsql %26amp; " where " %26amp; UniqueField %26amp;" = "%26amp; uniquekey
set rst = Server.CreateObject("adodb.recordset")
rst.Open strsql,connstr
'rst.Close
set rst = nothing
set strsql = nothing
End if

If dg_action="delete" then
strsql = ""
strsql = "delete from " %26amp; table %26amp; " where " %26amp; UniqueField %26amp;" = "%26amp; uniquekey
response.Write strsql
set rst = Server.CreateObject("adodb.recordset")
rst.Open strsql,connstr
'rst.Close
set rst = nothing
End if

IF dg_action = "addnew" and isAddnew = 1 then
'dim strsql
set rst = Server.CreateObject("adodb.recordset")
rst.open table,connstr,1,3,2
rst.addnew
j=0
For i = 0 to index - 1
if nItems(i).Columntype%26lt;%26gt;"edit" and nItems(i).Columntype %26lt;%26gt; "delete" and not nItems(i).readonly and nItems(i).field%26lt;%26gt;uniquefield then
if j %26lt;%26gt; 0 then strsql = strsql %26amp; ","
 select case nItems(i).DataType
Case "text"
rst(nItems(i).field) = Request(nItems(i).field)
Case "number","int","bigint","tinyint"
rst(nItems(i).field) = Request(nItems(i).field)
Case "date","time","datetime"
rst(nItems(i).field) = Request(nItems(i).field)
Case else
rst(nItems(i).field) = Request(nItems(i).field)
End select
j=j+1
End if
Next
rst.update
set rst = nothing
End if


rs.Open strSQLString,connstr,1,1

strTable= "%26lt;table border=" %26amp; intBorderWidth %26amp; " bordercolordark=" %26amp; strBorderColorDark %26amp; " bordercolorlight=" %26amp; strbordercolorlight %26amp; "class=" %26amp;cssclass %26amp; " cellspacing=0%26gt;" '加样式
strTable = strTable %26amp; "%26lt;form action=" %26amp; formURL %26amp; " name=""gridform"" method=post%26gt;"
if PagePosition="up" or PagePosition="updown" then strTable = strTable %26amp; "%26lt;tr%26gt;%26lt;td colspan="%26amp; index %26amp;"%26gt;"%26amp; page(rs.PageCount,Pagenumber,currPage )%26amp;"%26lt;/td%26gt;%26lt;/tr%26gt;"
strTable = strTable %26amp; "%26lt;tr bgcolor=" %26amp; strHeadBackgroudColor %26amp; "%26gt;"
for i = 0 to index - 1
if nItems(i).Title%26lt;%26gt;"" then
strTable = strTable %26amp; "%26lt;td " %26amp; nItems(i).HTMLstr %26amp; "%26gt;" %26amp; nItems(i).Title %26amp;"%26lt;/td%26gt;"
else
strTable = strTable %26amp; "%26lt;td " %26amp; nItems(i).HTMLstr %26amp; "%26gt;" %26amp; rs.Fields(i).Item.Name %26amp;"%26lt;/td%26gt;"
end if

Next
strTable = strTable %26amp; "%26lt;/tr%26gt;"
if cint(currPage) %26gt; cint(rs.PageCount) then currPage = rs.PageCount
intPage = Pagesize
rs.PageSize = pagesize
rs.AbsolutePage = currPage

do while not rs.eof and intPage %26gt; 0
intPage = intPage - 1
dbuniquekey = rs(uniquefield)
If intPage mod 2 then
strTable = strTable %26amp; "%26lt;tr%26gt;"
Else
strTable = strTable %26amp; "%26lt;tr "%26amp; strAlternateStyle %26amp;"%26gt;"
End if
'response.Write len(dg_action)%26gt;0 and int(dbuniquekey) = int(uniquekey)
if dg_action ="edit" and int(dbuniquekey) = int(uniquekey) then
for i = 0 to index - 1
if nItems(i).Columntype%26lt;%26gt;"edit" and nItems(i).Columntype %26lt;%26gt; "delete" and not nItems(i).readonly and nItems(i).field%26lt;%26gt;uniquefield then
strTable = strTable %26amp; "%26lt;td " %26amp; nItems(i).HTMLstr %26amp;"%26gt;%26lt;input type=Text name=""" %26amp; rs.Fields(nItems(i).field).Name %26amp;""" value=""" %26amp; rs(nItems(i).field) %26amp;"""%26gt;%26lt;/td%26gt;"
else
 select case lcase(nItems(i).Columntype)
Case "label"
strTable = strTable %26amp; "%26lt;td "%26amp; nItems(i).HTMLstr %26amp;"%26gt;" %26amp; rs(nItems(i).field) %26amp;"%26lt;/td%26gt;"
Case "radio"

Case "image"

Case "checkbox"

Case "textbox"
strTable = strTable %26amp; "%26lt;td " %26amp; nItems(i).HTMLstr %26amp;"%26gt;%26lt;input type=text name=""" %26amp; rs.Fields(i).Name %26amp;""" value=" %26amp; rs(nItems(i).field) %26amp;"%26gt;%26lt;/td%26gt;"
Case "link"

Case "edit"
if UniqueField="" then ErrorMsg="UniqueField not set"
if dg_action = "edit" then
strTable = strTable %26amp; "%26lt;td "%26amp;nItems(i).HTMLstr %26amp;"%26gt;%26lt;a href=""JavaScript:document.gridform.submit()""%26gt;"%26amp;nItems(i).UpdateCommandText%26amp;"%26lt;/a%26gt; %26lt;a href="%26amp;actionURL%26amp;"%26gt;"%26amp;nItems(i).CancelCommandText%26amp;"%26lt;/a%26gt;%26lt;/td%26gt;"
else
strTable = strTable %26amp; "%26lt;td "%26amp;nItems(i).HTMLstr %26amp;"%26gt;%26lt;a href="%26amp;actionURL%26amp;"%26amp;dg_action=edit%26amp;uniquekey=" %26amp; rs(UniqueField) %26amp;"%26gt;"%26amp;nItems(i).EditCommandText%26amp;"%26lt;/a%26gt;%26lt;/td%26gt;"
end if
Case "delete"
if UniqueField="" then ErrorMsg="UniqueField not set"
strTable = strTable %26amp; "%26lt;td "%26amp;nItems(i).HTMLstr %26amp;"%26gt;%26lt;a href="%26amp;actionURL%26amp;"%26amp;dg_action=delete%26amp;uniquekey=" %26amp; rs(UniqueField) %26amp;"%26gt;"%26amp;nItems(i).DeleteCommandText%26amp;"%26lt;/a%26gt;%26lt;/td%26gt;"
Case "update"

Case else
strTable = strTable %26amp; "%26lt;td " %26amp; nItems(i).HTMLstr %26amp; "%26gt;" %26amp; rs(nItems(i).field) %26amp; "%26lt;/td%26gt;"
End select
end if

Next
else
for i = 0 to index - 1
 select case lcase(nItems(i).Columntype)
Case "label"
strTable = strTable %26amp; "%26lt;td " %26amp; nItems(i).HTMLstr %26amp; "%26gt;" %26amp; rs(nItems(i).field) %26amp;"%26lt;/td%26gt;"
Case "radio"

Case "image"

Case "checkbox"

Case "textbox"
strTable = strTable %26amp; "%26lt;td " %26amp; nItems(i).HTMLstr %26amp;"%26gt;%26lt;input type=text value=" %26amp; rs(nItems(i).field) %26amp;"%26gt;%26lt;/td%26gt;"
Case "link"

Case "edit"
if UniqueField="" then ErrorMsg="UniqueField not set"
strTable = strTable %26amp; "%26lt;td " %26amp; nItems(i).HTMLstr %26amp; "%26gt;%26lt;a href=" %26amp; actionURL %26amp; "%26amp;dg_action=edit%26amp;uniquekey=" %26amp; rs(UniqueField) %26amp; "%26gt;" %26amp; nItems(i).EditCommandText %26amp; "%26lt;/a%26gt;%26lt;/td%26gt;"
Case "delete"
if UniqueField="" then ErrorMsg="UniqueField not set"
strTable = strTable %26amp; "%26lt;td " %26amp; nItems(i).HTMLstr %26amp;"%26gt;%26lt;a href=" %26amp; actionURL %26amp; "%26amp;dg_action=delete%26amp;uniquekey=" %26amp; rs(UniqueField) %26amp;"%26gt;" %26amp; nItems(i).DeleteCommandText%26amp;"%26lt;/a%26gt;%26lt;/td%26gt;"
Case "update"

Case else
strTable = strTable %26amp; "%26lt;td " %26amp; nItems(i).HTMLstr %26amp;"%26gt;" %26amp; rs(nItems(i).Field) %26amp; "%26lt;/td%26gt;"
End select
Next
End if


'End if
rs.movenext
strTable = strTable %26amp; "%26lt;/tr%26gt;"%26amp; vbcrlf
loop

if PagePosition="down" or PagePosition="updown" then strTable = strTable %26amp; "%26lt;tr%26gt;%26lt;td colspan="%26amp; index %26amp;"%26gt;"%26amp; page(rs.PageCount,Pagenumber,currPage )
'strTable =strTable%26amp;"%26lt;tr%26gt;"
for i = 0 to index - 1
if nItems(i).Columntype%26lt;%26gt;"edit" and nItems(i).Columntype %26lt;%26gt; "delete" and not nItems(i).readonly and nItems(i).field%26lt;%26gt;uniquefield then
'strTable = strTable %26amp; "%26lt;td " %26amp; nItems(i).HTMLstr %26amp;"%26gt;%26lt;input type=Text name=""" %26amp; rs.Fields(nItems(i).field).Name %26amp;"""%26gt;%26lt;/td%26gt;"
else
'strTable = strTable %26amp; "%26lt;td " %26amp; nItems(i).HTMLstr %26amp;"%26gt;%26amp;nbsp;%26lt;/td%26gt;"
end if
next
strTable =strTable%26amp;"%26lt;/tr%26gt;"

strTable = strTable %26amp; "%26lt;/form%26gt;%26lt;/table%26gt;"
If isAddnew = 1 then
strTable = strTable %26amp; "%26lt;form action=""?dg_action=addnew"" name=""dgridadd"" method=""post""%26gt;%26lt;table border=" %26amp; intBorderWidth %26amp; " bordercolordark=" %26amp; strBorderColorDark %26amp; " bordercolorlight=" %26amp; strbordercolorlight %26amp; " cellspacing=0%26gt;%26lt;tr%26gt;"
for i = 0 to index - 1
if nItems(i).Columntype%26lt;%26gt;"edit" and nItems(i).Columntype %26lt;%26gt; "delete" and not nItems(i).readonly and nItems(i).field%26lt;%26gt;uniquefield then
strTable = strTable %26amp; "%26lt;td " %26amp; nItems(i).HTMLstr %26amp;"%26gt;" %26amp; rs.Fields(nItems(i).field).Name %26amp;"%26lt;/td%26gt;"
else
'strTable = strTable %26amp; "%26lt;td " %26amp; nItems(i).HTMLstr %26amp;"%26gt;"%26amp;nItems(i).Title%26amp;"%26lt;/td%26gt;"
end if
next
strTable = strTable %26amp; "%26lt;td rowspan=2%26gt;%26lt;a href=""JavaScript:document.dgridadd.submit()""%26gt;New%26lt;/a%26gt;%26lt;/td%26gt;%26lt;/tr%26gt;%26lt;tr%26gt;"

for i = 0 to index - 1
if nItems(i).Columntype%26lt;%26gt;"edit" and nItems(i).Columntype %26lt;%26gt; "delete" and not nItems(i).readonly and nItems(i).field%26lt;%26gt;uniquefield then
strTable = strTable %26amp; "%26lt;td " %26amp; nItems(i).HTMLstr %26amp;"%26gt;%26lt;input type=Text name=""" %26amp; rs.Fields(nItems(i).field).Name %26amp;"""%26gt;%26lt;/td%26gt;" %26amp; vbcrlf
else
'strTable = strTable %26amp; "%26lt;td " %26amp; nItems(i).HTMLstr %26amp;"%26gt;%26amp;nbsp;%26lt;/td%26gt;"
end if
next
strTable = strTable %26amp;"%26lt;/tr%26gt;%26lt;/table%26gt;%26lt;/form%26gt;"
End if
Response.Write strTable

End Sub

Public Sub AddColumn(cColumn)
'set tem = new Column
'tem = cColumn
'response.Write cColumn.ColumnHTMLstr
Columns.Add index,cColumn
index = index + 1
'Columns.Items(i)
End Sub

End Class

Class Column
Private strType
Private strAlign
Private strStyle
Private ColumnText
Private intWidth
Private intHight
Private strfield
Private strTitle
Private strEvent
Private strCssClass
Private strText
Private strEditCommandText,strUpdateCommandText,strCancelCommandText,strDeleteCommandText
Private strRegExp
Private strReadOnly
Private strDataType
Public MaxValue,MinValue,MaxLength,MinLength,IsEmpty,IsChar,IsNumber,isCharNumber,isDate,isEmail


Private Sub Class_Initialize()

ColumnType = "Text"
strEditCommandText = "EDIT"
strUpdateCommandText = "UPDATE"
strCancelCommandText = "CANCEL"
strDeleteCommandText = "DELETE"

End Sub

Private Sub Class_Terminate()

End Sub

Public Property Let Columntype(Value)
strType = lcase(Value)
End Property

Public Property Get Columntype()
ColumnType = strType
End Property

Public Property Let Para(Value)
Para = Value
End Property


Public Property Let ParaLink(Value)
ParaLink = Replace(Value,"{0}",Para)
End Property

Public Property Let ControlName(Value)
ControlName = Value
End Property

Public Property Let Style(Value)
strStyle = Value
End Property

Public Property Let Eventstr(Value)
strEvent = Value
End Property

Public Property Let Align(Value)
strAlign = Value
End Property

Public Property Get Align()
Align = strAlign
End Property

Public Property Get Eventstr()
Eventstr = strEvent
End Property

Public Property Let Width(Value)
intWidth = Value
End Property

Public Property Let Field(Value)
strField = lcase(Value)
End Property

Public Property Get Field()
Field = strField
End Property

Public Property Let Title(Value)
if value="" then strTitle = strField else strTitle = Value
End Property

Public Property Get Title()
if strTitle="" then Title = strField else Title = strTitle
End Property

Public Property Let CssClass(Value)
strCssClass = Value
End Property

Public Property Get CssClass()
CssClass = strCssClass
End Property

Public Property Let DataType(Value)
strDataType = lcase(Value)
End Property

Public Property Get DataType()
DataType = strDataType
End Property

Public Property Let Text(Value)
 select case value
case ""
strText = strType
case null
strText = strType
case else
strText = Value
end select
End Property

Public Property Get Text()
Text = strText
End Property

Public Property Let ReadOnly(Value)
if value="" or isnull(value) then strReadOnly = False else strReadOnly = value
End Property

Public Property Get ReadOnly()
ReadOnly = strReadOnly
End Property

Public Property Let EditCommandText(Value)
strEditCommandText = Value
End Property

Public Property Get EditCommandText()
EditCommandText = strEditCommandText
End Property


Public Property Let UpdateCommandText(Value)
strUpdateCommandText = Value
End Property

Public Property Get UpdateCommandText()
UpdateCommandText = strUpdateCommandText
End Property


Public Property Let CancelCommandText(Value)
strCancelCommandText = Value
End Property

Public Property Get CancelCommandText()
CancelCommandText = strCancelCommandText
End Property



Public Property Let DeleteCommandText(Value)
strDeleteCommandText = Value
End Property

Public Property Get DeleteCommandText()
DeleteCommandText = strDeleteCommandText
End Property

Public Property Let RegExp(Value)
strRegExp = Value
End Property

Public Property Get RegExp()
RegExp = strRegExp
End Property

Public Property Get HTMLstr()
tempstr = ""
if intWidth %26lt;%26gt; "" then tempstr = tempstr %26amp; " width=""" %26amp; intWidth %26amp; """"
if intHeight %26lt;%26gt; "" then tempstr = tempstr %26amp; " height =""" %26amp; intHeight %26amp; """"
if strStyle %26lt;%26gt; "" then tempstr = tempstr %26amp; " style=""" %26amp; strStyle %26amp; """"
if strEvent %26lt;%26gt; "" then tempstr = tempstr %26amp; " " %26amp; strEvent
if strAlign %26lt;%26gt; "" then tempstr = tempstr %26amp; " align=""" %26amp; strAlign %26amp; """"
HTMLstr = tempstr

End Property
End Class
%%26gt;