分享
 
 
 

ASP实现的一个DataGrid类α版

王朝c#·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

<%@LANGUAGE="VBSCRIPT" %>

<%Option Explicit %>

<\!--#include file="../includes/library.asp"-->

<\!--#include file="../includes/runtime.asp"-->

<\!--#include file="../Manager/char.asp"-->

<\%=lan_getText(session("userLanguage"),0)%>

<script>

function selectAll(obj)

{

var top=obj;

while(true)

{

var top=top.parentElement;

if(top==null)

{

break;

}

if(top=="undefined")

{

top=null;

break;

}

if(top.tagName=="TABLE")

{

break;

}

}

var i;

var j;

if(top!=null)

{

for(i=0;i<top.rows.length;i++)

{

for(j=0;j<top.rows[i].cells[0].children.length;j++)

{

var temp=top.rows[i].cells[0].children[j];

if(temp.tagName=="INPUT")

{

if(temp.type=="checkbox")

{

temp.checked=obj.checked;

}

}

}

}

}

}

</script>

<%

Dim nbdg, errLog

Set nbdg = new NewsBriefDataGrid

Dim pageStringTemplate, pageStringTemplate0, pageStringTemplatePrevious0, pageStringTemplateNext0, pageStringTemplatePrevious1, pageStringTemplateNext1

pageStringTemplate = "<table width=""100%""><tr><td width=""80%"">{0}</td><td>{1}</td></tr></table>"

pageStringTemplate0 = "合计<font color=""red"">{RowCount}</font>条记录第<font color=""red"">{CurrentPage}</font>页/合计<font color=""red"">{PageCount}</font>页"

pageStringTemplatePrevious0 = "上一页&nbsp;"

pageStringTemplateNext0 = "下一页&nbsp;"

pageStringTemplatePrevious1 = "<a href=""?page={Previous}"">上一页</a>&nbsp;"

pageStringTemplateNext1 = "<a href=""?page={Next}"">下一页</a>&nbsp;"

nbdg.listNewsBriefs Response, pageStringTemplate, pageStringTemplate0, pageStringTemplatePrevious0, pageStringTemplateNext0, pageStringTemplatePrevious1, pageStringTemplateNext1, "page", -1, session("userLanguage"), errLog

%>

<%

Class NewsBriefDataGrid

'@param Res Response

'@param newsType 新闻类型

'@param languageId

'@errLog

Public Function listNewsBriefs(Res, pageStringTemplate, pageStringTemplate0, pageStringTemplatePrevious0, pageStringTemplateNext0, pageStringTemplatePrevious1, pageStringTemplateNext1, PAGE_PARAM_NAME, newsType, lang, errLog)

Dim ret

Dim currentPage '当前页码,从1开始计数

Dim pageSize '页大小

currentPage = Request.QueryString(PAGE_PARAM_NAME)

If currentPage < 1 Then

currentPage = 1

End if

pageSize = 10

'定义新闻操作句柄

dim objNews

set objNews = Server.CreateObject("SanguineRecore.clsNews")

dim count

objNews.getNewsCount count, -1, "zh_CN", errLog

ret = ret & (page(pageStringTemplate, pageStringTemplate0, pageStringTemplatePrevious0, pageStringTemplateNext0, pageStringTemplatePrevious1, pageStringTemplateNext1, count, pageSize, PAGE_PARAM_NAME, currentPage))

'获取新闻并打印

Dim rows

Dim isOK

'isOK = objNews.getNewsBriefs(CInt(startRow), CInt(pageSize), newsType, rows, "0",errLog)

isOK = objNews.getNewsBriefs ((currentPage - 1) * pageSize, pageSize, newsType, rows, lang, errLog)

'Res.Write(isOK)

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

'' 一个使用数据网格的例子

'' 下列属性为必选属性

'' DataSource 数据源

'' RowCount 行数

'' ColumnCount 列数

'' Columns 列绑定

'' HeaderTexts 表头文本

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

dim dg

'初始化数据网格

set dg = new DataGrid

'设置网格属性

dg.tableAttribute = "cellspacing=""0"" rules=""all"" border=""1"" style=""border-collapse:collapse;"""

'设置表格头属性

dg.thAttribute = "bgcolor=""#EEEEEE"" style=""font:normal"""

'设置表行属性

dg.TrAttribute = "onmouseover=""this.style.backgroundColor='#EFEFEF'"" onmouseout=""this.style.backgroundColor=''"""

'设置数据源,为一二维数组

dg.DataSource = rows

'设置行数,默认为数据源的行数

If IsArray(rows) Then

dg.RowCount = UBound(rows)

Else

dg.RowCount = 0

End if

'列数,默认为Columns属性的长度

dg.ColumnCount = 6

'设置列绑定

dg.Columns = array( _

"<input type=""checkbox"" name=""id"" value=""{TEXT[0]}"">", _

"{TEXT[0]}", _

"{HTML[2]}", _

"{TEXT[4]}", _

"{TEXT[3]}", _

"<a href=""edit.asp?id={TEXT[0]}"">编辑</a>"_

)

'Response.Write(rows(0)(1))

'设置列宽度

dg.ColumnWidths = array("5%","5%", "20%", "30%", "20%", "")

'设置列文字对齐方式

dg.ColumnAligns = array("center", "center", "center", "center", "center", "center")

'设置表头文字,支持HTML格式

''请把下面的汉字修改成类似:lan_getText(languageId,32)

dg.HeaderTexts = array("<input type=""checkbox"" onclick=""selectAll(this)"">", "ID", "新闻标题", "新闻摘要", "输入时间", "操作")

'设置表头文字对齐属性,默认对齐方式为center

dg.HeaderTextAligns = array("center", "center", "center", "center", "center", "center")

ret = ret & dg.ToTable()

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

ret = ret & "<input type=""submit"" value=""删除"">"

ret = ret & "</form>"

Res.Write(ret)

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

End Function

'

'@param rowCount 总行数

'@param pageSize 每页显示的行数

'@param PAGE_PARAM_NAME page的参数名,用在QueryString中

'@param currentPage 当前页码

'@return 分页字符串

Public Function page(pageStringTemplate, pageStringTemplate0, pageStringTemplatePrevious0, pageStringTemplateNext0, pageStringTemplatePrevious1, pageStringTemplateNext1, rowCount, pageSize, PAGE_PARAM_NAME, currentPage)

rowCount = CInt(rowCount)

pageSize = CInt(pageSize)

currentPage = CInt(currentPage)

Dim ret

Dim pageCount, i, tmp

tmp = rowCount / pageSize

pageCount = Fix(tmp)

If(tmp > pageCount) Then

pageCount = pageCount + 1

End If

Dim p1, p2

p1 = ""

'p1 = p1 & "合计<font color=""red"">" & rowCount & "</font>条记录" _

' & "第<font color=""red"">" & currentPage & "</font>页/" _

' & "合计<font color=""red"">" & pageCount & "</font>页"

p1 = pageStringTemplate0

p1 = Replace(p1, "{RowCount}", rowCount)

p1 = Replace(p1, "{CurrentPage}", currentPage)

p1 = Replace(p1, "{PageCount}", pageCount)

''''''''''''''''''''

''''''''''''''''''''

if currentPage > 1 then

'p2 = p2 & "<a href=""?" & PAGE_PARAM_NAME & "=" & (currentPage-1) & """>" & "上一页" & "</a>&nbsp;"

p2 = p2 & Replace(pageStringTemplatePrevious1, "{Previous}", currentPage -1)

else

'p2 = p2 & "上一页&nbsp;"

p2 = p2 & pageStringTemplatePrevious0

end if

for i = 1 to pageCount

if i = currentPage then

p2 = p2 & i & "&nbsp;"

else

p2 = p2 & "<a href=""?" & PAGE_PARAM_NAME & "=" & i & """>" & (i) & "</a>&nbsp;"

end if

next

if currentPage < pageCount then

'p2 = p2 & "<a href=""?" & PAGE_PARAM_NAME & "=" & (currentPage+1) & """>" & "下一页" & "</a>&nbsp;"

p2 = p2 & Replace(pageStringTemplateNext1, "{Next}", currentPage + 1)

else

'p2 = p2 & "下一页&nbsp;"

p2 = p2 & pageStringTemplateNext0

end if

'p = "<table width=""100%""><tr><td width=""80%"">" & p1 & "</td><td>" & p2 & "</td></tr></table>"

ret = pageStringTemplate

ret = Replace(ret, "{0}", p1)

ret = Replace(ret, "{1}", p2)

page = ret

End Function

End Class

%>

<%

'Class DataGrid

'根据给定的列模板和数据源(二维数组)来显示数据表格

'

Class DataGrid

Private m_dataSource

'行数

Private m_rowCount

'列数

Private m_columnCount

Private m_columns

'表格属性

Private m_tableAttribute

'表头公共属性

Private m_thAttribute

'表行公共属性

Private m_trAttribute

'单元格属性

Private m_tdAttribute

'列宽

Private m_columnWidths

'列对齐格式

Private m_columnAligns

'表头文字

Private m_headerTexts

'表头文字对齐属性

Private m_headerTextAligns

'Private m_rows

'Private m_r_columns

Public Property Let DataSource(ds)

m_dataSource = ds

End Property

Public Property Let RowCount(n)

m_rowCount = n

End Property

Public Property Let ColumnCount(n)

m_columnCount = n

End Property

Public Property Let Columns(cols)

m_columns = cols

End Property

Public Property Let TableAttribute(s)

m_tableAttribute = s

End Property

Public Property Let ThAttribute(s)

m_thAttribute = s

End Property

Public Property Let TrAttribute(s)

m_trAttribute = s

End Property

Public Property Let TdAttribute(s)

m_tdAttribute = s

End Property

Public Property Let ColumnWidths(arrayWidth)

m_columnWidths = arrayWidth

End Property

Public Property Let ColumnAligns(aligns)

m_columnAligns = aligns

End Property

Public Property Let HeaderTexts(arrayText)

m_headerTexts = arrayText

End Property

Public Property Let HeaderTextAligns(aligns)

m_headerTextAligns = aligns

End Property

'将二维数组转换成HTML表格

'@param rows 二维数组

'@return HTML表格源码

function ToTable()

dim ret

ret = ""

dim row, i, j

ret = ret & "<table " & m_tableAttribute & ">" & vblf

ret = ret & "<tr>" & thead() & "</tr>" & vblf

if IsArray(m_dataSource) then

for i = 0 to m_rowCount - 1

row = m_dataSource(i)

ret = ret & "<tr " & m_trAttribute & ">" & vblf

for j = 0 to m_columnCount - 1

ret = ret & "<td " & m_tdAttribute

If IsArray(m_columnAligns) Then

ret = ret & " align=""" & m_columnAligns(j) & """"

End If

ret = ret & ">" & bind(i, j) & "</td>"

next

'

'ret = ret & "<td " & m_tdAttribute & ">" & "<input type=""checkbox"" name=""id"" value=""" & Server.HTMLEncode(row(m_r_columns(0))) & """>" & "</td>" & vblf

'

'for j = 0 to UBound(m_r_columns)

' ret = ret & "<td " & m_tdAttribute & ">" & Server.HTMLEncode(row(m_r_columns(j))) & "&nbsp;</td>" & vblf

'next

'

'ret = ret & "<td " & m_tdAttribute & ">" & "<a href=""edit.asp?id=" & Server.HTMLEncode(row(m_r_columns(0))) & """>Edit</a>" & "</td>" & vblf

'

ret = ret & "</tr>" & vblf

next

end if

ret = ret & tfoot() & vblf

ret = ret & "</table>" & vblf

ToTable = ret

end function

Private Function bind(row, col)

dim format, find, replacement, index, myVarType

dim openB, closeB

format = m_columns(col)

openB = InStr(format, "{")

dim var

if openB < 0 then

bind = format

else

closeB = InStr(format, "}")

var = Mid(format, openB + 1,closeB - openB - 1)

openB = InStr(var, "[")

closeB = InStr(var, "]")

myVarType = Mid(var, 1, openB - 1)

'Response.Write(myVarType)

index = CInt(Mid(var, openB+1, closeB - openB -1))

'Response.Write(index)

find = "{" & myVarType & "[" & index & "]}"

If "HTML" = myVarType Then

replacement = m_dataSource(row)(index)

ElseIf "TEXT" = myVarType Then

replacement = Server.HTMLEncode(m_dataSource(row)(index))

End If

bind = Replace(format, find, replacement)

end if

End Function

'显示表头

'@param 表头数组

'@return 表头HTML源码

private function thead()

dim ret, i

ret = ""

for i = 0 to UBound(m_headerTexts)

ret = ret & "<th " & m_thAttribute

If IsArray(m_columnWidths) Then

ret = ret & " width=""" & m_columnWidths(i) & """"

End If

If IsArray(m_headerTextAligns) Then

ret = ret & " align=""" & m_headerTextAligns(i) & """"

End if

ret = ret & ">" & m_headerTexts(i) & "</th>" & vblf

next

thead = ret

end function

private function tfoot()

end function

end Class

%>

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有