<!--#include file="te_config.asp"-->
<!--#include file="te_header.asp"-->
<%
	lConnID = request("cid")
	sTableName = request("tablename")
	sQuery = request("q")
	
	if sQuery <> "" then
		bQuery = True
		sTableName = replace(sTableName, """", "'")
	end if
%>
<table border=0 cellspacing=1 cellpadding=2 bgcolor = "#ffe4b5" width=100%>
	<tr>
		<td class="smallertext">
			<a href="index.asp">管理首页</a> <a href="te_admin.asp">编辑页面</a> <a href="te_listtables.asp?cid=<%=request("cid")%>"><%=arrDesc(request("cid"))%></a> <%if bQuery then response.write "Query" else response.write "[表格:" & sTableName & "]"%>
		</td>
		<td class="smallerheader" width=130 align=right>
		<%
		if bProtected then 
			response.write session("teFullName")
			response.write "<a href=""te_logout.asp""> (退出)</a>" 
		end if
		%>
		</td>
	</tr>
</table>
<br>
<%

	OpenRS arrConn(lConnID)

	if request("cmdsearch") <> "" then
		bQuery = True

		if request("chkSubstring") <> "" then
			bSubstring = True
		end if
		
		sSQL = "SELECT * FROM [" & sTableName & "]"
		on error resume next
		rs.Open sSQL,,,adCmdTable
		
		for each fld in rs.fields
			if request.form(fld.name) <> "" then
				sOP = " = "
				select case fld.type
					case adBoolean
						'BUG: What if the user dont want to perform a distinction on the boolean field?
						if len(request(fld.name))>0 then sFieldVal = "True" else sFieldVal = "False"
					case adLongVarBinary
						'no search on OLE fields
					case adDate
						if isDate(request(fld.name)) then sFieldVal = "#" & request(fld.name) & "#"
					case adSmallInt, adInteger, adCurrency, adUnsignedTinyInt
						if isNumeric(request(fld.name)) then sFieldVal = request(fld.name)
					case else
						sFieldVal = "'" & replace(request(fld.name),"'", "") & "'"
		                if bSubstring then
							sOp = " LIKE "
							sFieldVal = "'%" & request(fld.name) & "%'"
						else
							sFieldVal = "'" & request(fld.name) & "'"
		            	end if

				end select
				
				iFieldCount = iFieldCount + 1
				
				if iFieldCount = 1 then
					sWhere = " WHERE [" & fld.name & "]" & sOp & sFieldVal
				else
					sWhere = sWhere & " AND [" & fld.name & "]" & sOp & sFieldVal
				end if
				
			end if
		next

		sTableName = "SELECT * FROM [" & sTableName & "]" & sWhere
		rs.close
	end if


	if request("orderby") <> "" then
		sOrderBy = " ORDER BY [" & request("orderby") & "]"
		sOrderByLink = "&orderby=" & request("orderby")
		select case request("dir") 
			case "desc"
				sOrderBy = sOrderBy & " DESC"
				sOrderByLink = sOrderByLink & "&dir=desc"
			case "asc"
				sOrderBy = sOrderBy & " ASC"
				sOrderByLink = sOrderByLink & "&dir=asc"
			case else
				sOrderBy = sOrderBy & " ASC"
				sOrderByLink = sOrderByLink & "&dir=asc"
		end select
	end if

	
	if bQuery then
		if instr(sTableName, "order by") = 0 then
			sSQL = sTableName & sOrderBy
		else
			sSQL = sTableName
		end if
	else
		sSQL = "SELECT * FROM [" & sTableName & "]" & sOrderBy
	end if

	on error resume next
	rs.Open sSQL, , , adCmdTable
	if err <> 0 then
		response.write "出错了: " & err.description & "<br><br>"
		if bQuery then
			response.write "SQL : " & sSQL & "<br><br>"
		end if
		response.write "点击此处 <a href=""javascript:history.back()"">返回</a><br><br>"
		CloseRS
		%><!--#include file="te_footer.asp"--><%
		response.end
	end if
	on error goto 0
	
	lRecs = rs.RecordCount
	lFields = rs.Fields.Count
	
	if isNumeric(request("ipage")) then iPage = CLng(request("ipage"))
	rs.PageSize = cPerPage
	iPageCount = rs.PageCount

	if iPage < 1 then iPage = 1
	if lRecs > 0 then rs.AbsolutePage = iPage
	
	if bQuery then
		response.write sSQL & "<br><br>"
	end if


%>

<table border=0 cellspacing=1 cellpadding=2 bgcolor = "#ffdead" width=100%>
	<tr>
		<td width=10></td>
		<td class="smallerheader"><%if bQuery then response.write "Query" else response.write sTableName%></td>
		<td class="smallertext" width=100><%=lRecs%> records</td>
		
		<% if bRecAdd then %>
		<td class="smallertext" width=100>
			<a href="te_showrecord.asp?cid=<%=lConnID%>&tablename=<%=server.UrlEncode(sTableName)%>&add=1&ipage=<%=iPage%>">添加记录</a>
		</td>
		<% end if %>
		
		<td class="smallertext">
		<%
			'Build navigation bar
			if iPage <> 1 then response.write "<a href=""te_showtable.asp?cid=" & lConnID & "&tablename=" & server.UrlEncode(sTableName) & "&q=" & bQuery & "&ipage=1"& sOrderByLink & """>first</a> :: " else response.write "第一个 :: "
			if iPage > 1 then response.write "<a href=""te_showtable.asp?cid=" & lConnID & "&tablename=" & server.UrlEncode(sTableName) & "&q=" & bQuery & "&ipage=" & iPage - 1 & sOrderByLink & """>previous</a> :: " else response.write "返回 :: "
			if iPage < iPageCount then response.write "<a href=""te_showtable.asp?cid=" & lConnID & "&tablename=" & server.UrlEncode(sTableName) & "&q=" & bQuery & "&ipage=" & iPage + 1 & sOrderByLink & """>next</a> :: " else response.write "下一个 :: "
			if iPage <> iPageCount then response.write "<a href=""te_showtable.asp?cid=" & lConnID & "&tablename=" & server.UrlEncode(sTableName) & "&q=" & bQuery & "&ipage=" & iPageCount & sOrderByLink & """>last</a>" else response.write "最后"
		%>
		</td>
		<td class="smallertext" align=right>
			第<%=iPage%>页 共<%=iPageCount%>页
		</td>
	</tr>
</table>

<table border=0 cellspacing=1 cellpadding=2 bgcolor = "#ffe4b5" width=100%>
<tr bgcolor="#fffaf0"><td class="smallertext" width=10></td>
	<%
	response.write "<td class=""smallerheader"" width=30>操作</td>"
	for each fld in rs.fields
		if fld.type <> adLongVarBinary then
			if request("orderby") = fld.name then
				if request("dir") = "asc" then
					sDirection = "desc"
				else
					sDirection = "asc"
				end if
			else
				sDirection = "asc"
			end if
			response.write "<td class=""smallerheader"">"
			response.write "<a href=""te_showtable.asp?cid=" & lConnID & "&tablename=" & server.UrlEncode(sTableName) & "&q=" & bQuery & "&ipage=" & iPage & "&orderby=" & fld.name & "&dir=" & sDirection &  """>"
			response.write fld.name
			response.write "</a>"
			response.write "</td>"
		else
			response.write "<td class=""smallerheader"">"
			response.write fld.name
			response.write "</td>"
		end if
	next
	response.write "<td width=10></td>"
	response.write "</tr>"

	do while not (rs.eof or (rs.AbsolutePage <> iPage))
		response.write "<tr bgcolor=""#fffaf0"">"
		response.write "<td width=10></td>"
		set fld = rs.fields(0)
		response.write "<td>"
		if bRecEdit then response.write "<a href=""te_showrecord.asp?cid=" & lConnID & "&q=" & bQuery & "&tablename=" & server.UrlEncode(sTableName) & "&fld=" & fld.name & "&val=" & rs(fld.name) & "&fldtype=" & fld.type &  "&ipage=" & iPage &"""><img src=""images/edit.gif"" width=9 height=11 alt=""编辑"" border=0></a>&nbsp;"
		if bRecDel then response.write "<a href=""te_deleterecord.asp?cid=" & lConnID & "&q="& bQuery & "&tablename=" & server.UrlEncode(sTableName) & "&fld=" & fld.name & "&val=" & rs(fld.name) & "&fldtype=" & fld.type & "&ipage=" & iPage & """><img src=""images/del.gif"" width=9 height=11 alt=""删除"" border=0></a>"
		response.write "</td>"
		iFieldCount = 0
		for each fld in rs.fields
			iFieldCount = iFieldCount + 1
			response.write "<td class=""smallertext"">"
			if iFieldCount = 1 then
				response.write "<a href=""te_showrecord.asp?cid=" & lConnID & "&q=" & bQuery & "&tablename=" & server.UrlEncode(sTableName) & "&fld=" & fld.name & "&val=" & rs(fld.name) & "&fldtype=" & fld.type & "&ipage=" & iPage & """>" & rs(fld.name) & "</a>"
			else
				select case fld.type
					case adSmallInt, adInteger
						response.write rs(fld.name)
					case adDate
						if isdate(rs(fld.name)) then
							response.write rs(fld.name)
						end if
					case adBoolean
						response.write "<input type=""checkbox"" name=""chk"""
						if rs(fld.name)=true then
							response.write " checked>"
						else
							response.write ">"
						end if
					case adVarWChar, adLongVarWChar		'Text, Memo
						if lMaxShowLen > 0 then
							'If max # of chars is specified
							sVal = left(rs(fld.name), lMaxShowLen)
						else
							sVal = rs(fld.name)
						end if
						if (bEncodeHTML) and (len(sVal) > 0)then
							response.write server.htmlencode(sVal)
						else
							response.write sVal
						end if
					case else
						response.write rs(fld.name)
				end select
			end if
			response.write "</td>"
		next
		response.write "<td width=10></td>"
		response.write "</tr>"
		rs.movenext
	loop
		
	CloseRS

%>
</table>

<!--#include file="te_footer.asp"-->
 

