高飞

学习 总结 创新
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

ASP开发收集(ZT)

Posted on 2005-11-30 17:18  高飞博客  阅读(543)  评论(1编辑  收藏  举报
*******************************************************************
'取得IP地址
'*******************************************************************
Function Userip()
Dim GetClientIP
'如果客户端用了代理服务器,则应该用ServerVariables("HTTP_X_FORWARDED_FOR")方法
GetClientIP = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
If GetClientIP = "" or isnull(GetClientIP) or isempty(GetClientIP) Then
'如果客户端没用代理,应该用Request.ServerVariables("REMOTE_ADDR")方法
GetClientIP = Request.ServerVariables("REMOTE_ADDR")
end if
Userip = GetClientIP
End function

'*******************************************************************
' 弹出对话框
'*******************************************************************
Sub alert(message)
message = replace(message,"'","\'")
Response.Write ("<script>alert('" & message & "')</script>")
End Sub

'*******************************************************************
' 返回上一页,一般用在判断信息提交是否完全之后
'*******************************************************************
Sub GoBack()
Response.write ("<script>history.go(-1)</script>")
End Sub

'*******************************************************************
' 重定向另外的连接
'*******************************************************************
Sub Go(url)
Response.write ("<script>location.href('" & url & "')</script>")
End Sub

'*******************************************************************
' 指定秒数重定向另外的连接
'*******************************************************************
sub GoPage(url,s)
s=s*1000
Response.Write "<SCRIPT LANGUAGE=JavaScript>"
Response.Write "window.setTimeout("&chr(34)&"window.navigate('"&url&"')"&chr(34)&","&s&")"
Response.Write "</script>"
end sub

'*******************************************************************
' 判断数字是否整形
'*******************************************************************
function isInteger(para)
on error resume next
dim str
dim l,i
if isNUll(para) then
isInteger=false
exit function
end if
str=cstr(para)
if trim(str)="" then
isInteger=false
exit function
end if
l=len(str)
for i=1 to l
if mid(str,i,1)>"9" or mid(str,i,1)<"0" then
isInteger=false
exit function
end if
next
isInteger=true
if err.number<>0 then err.clear
end function

'*******************************************************************
' 获得文件扩展名
'*******************************************************************
function GetExtend(filename)
dim tmp
if filename<>"" then
tmp=mid(filename,instrrev(filename,".")+1,len(filename)-instrrev(filename,"."))
tmp=LCase(tmp)
if instr(1,tmp,"asp")>0 or instr(1,tmp,"php")>0 or instr(1,tmp,"php3")>0 or instr(1,tmp,"aspx")>0 then
getextend="txt"
else
getextend=tmp
end if
else
getextend=""
end if
end function

' *----------------------------------------------------------------------------
' * 函数:CheckIn
' * 描述:检测参数是否有SQL危险字符
' * 参数:str要检测的数据
' * 返回:FALSE:安全 TRUE:不安全
' * 作者:
' * 日期:
' *----------------------------------------------------------------------------
function CheckIn(str)
if instr(1,str,chr(39))>0 or instr(1,str,chr(34))>0 or instr(1,str,chr(59))>0 then
CheckIn=true
else
CheckIn=false
end if
end function

' *----------------------------------------------------------------------------
' * 函数:HTMLEncode
' * 描述:过滤HTML代码
' * 参数:--
' * 返回:--
' * 作者:
' * 日期:
' *----------------------------------------------------------------------------
function HTMLEncode(fString)
if not isnull(fString) then
fString = replace(fString, ">", "&gt;")
fString = replace(fString, "<", "&lt;")

fString = Replace(fString, CHR(32), "&nbsp;")
fString = Replace(fString, CHR(9), "&nbsp;")
fString = Replace(fString, CHR(34), "&quot;")
fString = Replace(fString, CHR(39), "&#39;")
fString = Replace(fString, CHR(13), "")
fString = Replace(fString, CHR(10) & CHR(10), "</P><P> ")
fString = Replace(fString, CHR(10), "<BR> ")

HTMLEncode = fString
end if
end function

' *----------------------------------------------------------------------------
' * 函数:HTMLcode
' * 描述:过滤表单字符
' * 参数:--
' * 返回:--
' * 作者:
' * 日期:
' *----------------------------------------------------------------------------
function HTMLcode(fString)
if not isnull(fString) then
fString = Replace(fString, CHR(13), "")
fString = Replace(fString, CHR(10) & CHR(10), "</P><P>")
fString = Replace(fString, CHR(34), "")
fString = Replace(fString, CHR(10), "<BR>")
HTMLcode = fString
end if
end function
By [frank] at 14:41:41 | Comments[0] | 186 views
 几个经典的ASP应用  [2004-12-14]
个人认为第4条比较实用,以前一直以为ASP回车(vbCrLf)是/n的,在php里才是/n.


1. 下面的代码演示了如何在服务端获取来自客户端浏览器中某一个图片的x,y坐标,注意input控件的类型是image类型。

<form>
<Input Name="ImageMap" Type="Image" Src="ImageMap.jpg" Alt="Click Anywhere">
</form>

<%ImageMap.x = <%=Request("ImageMap.x")
ImageMap.y = <%=Request("ImageMap.y")%>


2. 利用ADODB.Stream对象,在IE浏览器中下载服务端上的各类文件。

即直接提示用户下载而不是由浏览器打开某些文件。注意,下面的代码拷贝到ASP文件中后,不要再添加一些非ASP代码在页面中:如HTML和Javascript客户端的代码。


<%
'--------------------------------------------
Response.Buffer = True
Dim strFilePath, strFileSize, strFileName
Const adTypeBinary = 1

strFilePath = "文件路径 "
strFileSize = ... 文件大小,可选
strFileName = "文件名"

Response.Clear

'8*******************************************8
' 需要在你的服务器上安装 MDAC 2.6 或MDAC2.7
'8*******************************************8
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Open
objStream.Type = adTypeBinary
objStream.LoadFromFile strFilePath

strFileType = lcase(Right(strFileName, 4)) '文件扩展名

' 通过文件扩展名判断 Content-Types
Select Case strFileType
Case ".asf"
ContentType = "video/x-ms-asf"
Case ".avi"
ContentType = "video/avi"
Case ".doc"
ContentType = "application/msword"
Case ".zip"
ContentType = "application/zip"
Case ".xls"
ContentType = "application/vnd.ms-excel"
Case ".gif"
ContentType = "image/gif"

Case ".jpg", "jpeg"
ContentType = "image/jpeg"
Case ".wav"
ContentType = "audio/wav"
Case ".mp3"
ContentType = "audio/mpeg3"
Case ".mpg", "mpeg"
ContentType = "video/mpeg"
Case ".rtf"
ContentType = "application/rtf"
Case ".htm", "html"
ContentType = "text/html"
Case ".asp"
ContentType = "text/asp"
Case Else
'Handle All Other Files
ContentType = "application/octet-stream"
End Select

Response.AddHeader "Content-Disposition", "attachment; filename= strFileName
Response.AddHeader "Content-Length", strFileSize
Response.Charset = "UTF-8" ' 客户端浏览器的字符集UTF-8
Response.ContentType = ContentType

Response.BinaryWrite objStream.Read
Response.Flush

objStream.Close
Set objStream = Nothing

%>


3.提升ASP页面的响应速率

在你的ASP页面的第一行加入:

<% ENABLESESSIONSTATE = False %>

这会关闭session对象,提升你的服务器响应速率,比较常见的问题是一个html页面包含了两个框架页面(至少有一个是ASP页面,并使用了session),这将使得必须等待某一个框架页(当然这个框架页中使用了session)加载完后,另一个框架页面才会显示。

如果你使用代理访问, 默认情况下,许多代理服务器不会动态缓存ASP页面内容,加入下面的代码:

<%
Response.CacheControl = "Public"
%>


这行代码会将ASP页面缓存在代理服务器上,从而加快客户端请求动态页面的响应速率,一些不经常变化的ASP页面将直接从代理服务器上取得。

4. 要知道浏览器(IE为例)不会解析回车和换行字符,如果你用Response.write方法写一行包含了回车和换行字符的字符串到动态页面中,其结果可想而知,你需要做的是:


<%
Response.Write(Replace(body, vbCrLf,"<br>"))
%>


用<br>来代替回车和换行。注意:如果回车和换行字符出现在form中的input/textarea等控件中,可以不必这么做。

5. 用ASP代码写IIS日志


<%
Response.AppendToLog "数据库正在被访问"
%>


执行这段代码后,在你的IIS日志中可能会出现下面的字符串:
127.0.0.1, -, 01/01/00, 12:00:34, W3SVC1,WEBSERVER,
127.0.0.1, 161342, 485, 228, 200, 0, get, /somefile.asp, 数据库正在被访问

注意:由于日志文件中的内容是按逗号分隔,所以写入的日志内容应避免使用逗号。

6. 如何访问远程计算机上MDB数据库文件

如果你用ODBC连接(DSN方式或其它方式)到远程计算机的MDB文件,这将产生一个错误:
Microsoft OLE DB Provider for ODBC Drivers error '80004005'
大致意思是该文件可能被其他用户访问或无足够的权限访问。

下面有两种方式,避免这个错误:

方式a. 使用DAO引擎访问


Dim File, Conn, RS
Const ReadOnly = False

File = "\\server\share\file.mdb"
Set Conn = CreateObject("DAO.DBEngine.35").Workspaces(0).OpenDatabase(File,,ReadOnly)
Set RS = Conn.OpenRecordset(SQL)


方式b. ADO + Jet OLE DB provider方式


Dim Conn, RS
Set Conn = CreateObject("ADODB.Connection")
Conn.Provider = "Microsoft.Jet.OLEDB.4.0"
Conn.Open "\\server\share\file.mdb"
Set RS = Conn.Execute(SQL)


确定在运行ASP页面时有足够的访问权限以访问远程计算机上的MDB文件,在访问MDB文件前需要先

登录到远程计算机,添加下面的代码


Set UM = CreateObject("UserManager.Server")
UM.LogonUser "帐号", "口令", "域"
...
open database
...
UM.RevertToSelf



By [leon] at 13:23:55 | Comments[0] | 408 views
 rs.open sql,conn,1,1全接触  [2004-9-20]
经常会在连接DB的时候用到,就是不知道代表什么意思。。。

RS.OPEN SQL,CONN,A,B

A:

ADOPENFORWARDONLY(=0)
只读,且当前数据记录只能向下移动

ADOPENKEYSET(=1)
只读,当前数据记录可自由移动

ADOPENDYNAMIC(=2)
可读写,当前数据记录可自由移动

ADOPENSTATIC(=3)
可读写,当前数据记录可自由移动,可看到新增记录

B:

ADLOCKREADONLY(=1)
缺省锁定类型,记录集是只读的,不能修改记录

ADLOCKPESSIMISTIC(=2)
悲观锁定,当修改记录时,数据提供者将尝试锁定记录以确保成功地编辑记录。只要编辑一开始,则立即锁住记录。

ADLOCKOPTIMISTIC(=3)
乐观锁定 ,直到用Update方法提交更新记录时才锁定记录。

ADLOCKBATCHOPTIMISTIC(=4)
批量乐观锁定,允许修改多个记录,只有调用UpdateBatch方法后才锁定记录。

当不需要改动任何记录时,应该使用只读的记录集,这样提供者不用做任何检测。
对于一般的使用,乐观的锁定可能是最好的选择,因为记录只被锁定一小段时间,
数据在这段时间被更新。这减少了资源的使用。

Via Blueidea
By [frank] at 13:07:11 | Comments[2] | 382 views
 提供很多DW组件的网站  [2004-8-9]
http://dreamlettes.net/extensions/我觉得还是挺有用的:)
By [frank] at 17:30:36 | Comments[0] | 121 views
 一些另类的ASP方法  [2004-6-29]
1. oncontextmenu="window.event.returnvalue=false" 将彻底屏蔽鼠标右键
<table border oncontextmenu=return(false)><td>no</table> 可用于Table


2. <body onselectstart="return false"> 取消选取、防止复制

3. onpaste="return false" 不准粘贴

4. oncopy="return false;" oncut="return false;" 防止复制

5. <link rel="Shortcut Icon" href="favicon.ico"> IE地址栏前换成自己的图标

6. <link rel="Bookmark" href="favicon.ico"> 可以在收藏夹中显示出你的图标

7. <input style="ime-mode:disabled"> 关闭输入法

8. 永远都会带着框架
<script language="javascript"><!--
if (window == top)top.location.href = "frames.htm"; //frames.htm为框架网页
// --></script>

9. 防止被人frame
<SCRIPT LANGUAGE=javascript><!--
if (top.location != self.location)top.location=self.location;
// --></SCRIPT>

10. <noscript><iframe src=*.html></iframe></noscript> 网页将不能被另存为

11. <input type=button value=查看网页源代码
onclick="window.location = view-source:+ http://www.csdn.net/'">

12. 怎样通过asp的手段来检查来访者是否用了代理
<% if Request.ServerVariables("HTTP_X_FORWARDED_FOR")<>"" then
response.write "<font color=#FF0000>您通过了代理服务器,"& _
"真实的IP为"&Request.ServerVariables("HTTP_X_FORWARDED_FOR")
end if
%>

13. 取得控件的绝对位置

//javascript
<script language="javascript">
function getIE(e){
var t=e.offsetTop;
var l=e.offsetLeft;
while(e=e.offsetParent){
t+=e.offsetTop;
l+=e.offsetLeft;
}
alert("top="+t+"\nleft="+l);
}
</script>

//VBScript
<script language="VBScript"><!--
function getIE()
dim t,l,a,b
set a=document.all.img1
t=document.all.img1.offsetTop
l=document.all.img1.offsetLeft
while a.tagName<>"BODY"
set a = a.offsetParent
t=t+a.offsetTop
l=l+a.offsetLeft
wend
msgbox "top="&t&chr(13)&"left="&l,64,"得到控件的位置"
end function
--></script>

14. 光标是停在文本框文字的最后
<script language="javascript">
function cc()
{
var e = event.srcElement;
var r =e.createTextRange();
r.moveStart(character,e.value.length);
r.collapse(true);
r.select();
}
</script>
<input type=text name=text1 value="123" onfocus="cc()">

15. 判断上一页的来源
asp:
request.servervariables("HTTP_REFERER")

javascript :
document.referrer

16. 最小化、最大化、关闭窗口
<object id=hh1 classid="clsid:ADB880A6-D8FF-11CF-9377-00AA003B7A11">
<param name="Command" value="Minimize"></object>
<object id=hh2 classid="clsid:ADB880A6-D8FF-11CF-9377-00AA003B7A11">
<param name="Command" value="Maximize"></object>
<OBJECT id=hh3 classid="clsid:adb880a6-d8ff-11cf-9377-00aa003b7a11">
<PARAM NAME="Command" value="Close"></OBJECT>

<input type=button value=最小化 onclick=hh1.Click()>
<input type=button value=最大化 onclick=hh2.Click()>
<input type=button value=关闭 onclick=hh3.Click()>
本例适用于IE

17.
<%
定义数据库连接的一些常量
Const adOpenForwardOnly = 0 游标只向前浏览记录,不支持分页、Recordset、BookMark
Const adOpenKeyset = 1 键集游标,其他用户对记录说做的修改将反映到记录集中,但其他用户增加或删除记录不会反映到记录集中。支持分页、Recordset、BookMark
Const adOpenDynamic = 2 动态游标功能最强,但耗资源也最多。用户对记录说做的修改,增加或删除记录都将反映到记录集中。支持全功能浏览(ACCESS不支持)。
Const adOpenStatic = 3 静态游标,只是数据的一个快照,用户对记录说做的修改,增加或删除记录都不会反映到记录集中。支持向前或向后移动

Const adLockReadOnly = 1 锁定类型,默认的,只读,不能作任何修改
Const adLockPessimistic = 2 当编辑时立即锁定记录,最安全的方式
Const adLockOptimistic = 3 只有在调用Update方法时才锁定记录集,而在此前的其他操作仍可对当前记录进行更改、插入和删除等
Const adLockBatchOptimistic = 4 当编辑时记录不会被锁定,而更改、插入和删除是在批处理方式下完成的

Const adCmdText = &H0001
Const adCmdTable = &H0002
%>

18. 网页不会被缓存
HTM网页
<META HTTP-EQUIV="pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache, must-revalidate">
<META HTTP-EQUIV="expires" CONTENT="Wed, 26 Feb 1997 08:21:57 GMT">
或者<META HTTP-EQUIV="expires" CONTENT="0">
ASP网页
Response.Expires = -1
Response.ExpiresAbsolute = Now() - 1
Response.cachecontrol = "no-cache"
PHP网页
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");


19. 检查一段字符串是否全由数字组成
<script language="javascript"><!--
function checkNum(str){return str.match(/\D/)==null}
alert(checkNum("1232142141"))
alert(checkNum("123214214a1"))
// --></script>

20. 获得一个窗口的大小
document.body.clientWidth,document.body.clientHeight

21. 怎么判断是否是字符
if (/[^\x00-\xff]/g.test(s)) alert("含有汉字");
else alert("全是字符");

22.TEXTAREA自适应文字行数的多少
<textarea rows=1 name=s1 cols=27 onpropertychange="this.style.posHeight=this.scrollHeight">
</textarea>

23. 日期减去天数等于第二个日期
<script language=javascript>
function cc(dd,dadd)
{
//可以加上错误处理
var a = new Date(dd)
a = a.valueOf()
a = a - dadd * 24 * 60 * 60 * 1000
a = new Date(a)
alert(a.getFullYear() + "年" + (a.getMonth() + 1) + "月" + a.getDate() + "日")
}
cc("12/23/2002",2)
</script>

24. 选择了哪一个Radio
<HTML><script language="vbscript">
function checkme()
for each ob in radio1
if ob.checked then window.alert ob.value
next
end function
</script><BODY>
<INPUT name="radio1" type="radio" value="style" checked>Style
<INPUT name="radio1" type="radio" value="barcode">Barcode
<INPUT type="button" value="check" onclick="checkme()">
</BODY></HTML>

25.获得本页url的request.servervariables("")集合
Response.Write "<TABLE border=1><!-- Table Header --><TR><TD><B>Variables</B></TD><TD><B>value</B></TD></TR>"
for each ob in Request.ServerVariables
Response.Write "<TR><TD>"&ob&"</TD><TD>"&Request.ServerVariables(ob)&"</TD></TR>"
next
Response.Write "</TABLE>"

26.
本机ip<%=request.servervariables("remote_addr")%>
服务器名<%=Request.ServerVariables("SERVER_NAME")%>
服务器IP<%=Request.ServerVariables("LOCAL_ADDR")%>
服务器端口<%=Request.ServerVariables("SERVER_PORT")%>
服务器时间<%=now%>
IIS版本<%=Request.ServerVariables"SERVER_SOFTWARE")%>
脚本超时时间<%=Server.ScriptTimeout%>
本文件路径<%=server.mappath(Request.ServerVariables("SCRIPT_NAME"))%>
服务器CPU数量<%=Request.ServerVariables("NUMBER_OF_PROCESSORS")%>
服务器解译引擎<%=ScriptEngine & "/"& ScriptEngineMajorVersion &"."&ScriptEngineMinorVersion&"."& ScriptEngineBuildVersion %>
服务器操作系统<%=Request.ServerVariables("OS")%>

27.ENTER键可以让光标移到下一个输入框
<input onkeydown="if(event.keyCode==13)event.keyCode=9">

28. 检测某个网站的链接速度:
把如下代码加入<body>区域中:
<script language=javascript>
tim=1
setInterval("tim++",100)
b=1

var autourl=new Array()
autourl[1]="www.njcatv.net"
autourl[2]="javacool.3322.net"
autourl[3]="www.sina.com.cn"
autourl[4]="www.nuaa.edu.cn"
autourl[5]="www.cctv.com"

function butt(){
document.write("<form name=autof>")
for(var i=1;i<autourl.length;i++)
document.write("<input type=text name=txt"+i+" size=10 value=测试中……> =》<input type=text name=url"+i+" size=40> =》<input type=button value=GO onclick=window.open(this.form.url"+i+".value)><br/>")
document.write("<input type=submit value=刷新></form>")
}
butt()
function auto(url){
document.forms[0]["url"+b].value=url
if(tim>200)
{document.forms[0]["txt"+b].value="链接超时"}
else
{document.forms[0]["txt"+b].value="时间"+tim/10+"秒"}
b++
}
function run(){for(var i=1;i<autourl.length;i++)document.write("<img src=http://"+autourl+"/"+Math.random()+" width=1 height=1 onerror=auto(http://";;;+autourl+")>")}
run()</script>


29. 各种样式的光标
auto :标准光标
default :标准箭头
hand :手形光标
wait :等待光标
text :I形光标
vertical-text :水平I形光标
no-drop :不可拖动光标
not-allowed :无效光标
help :?帮助光标
all-scroll :三角方向标
move :移动标
crosshair :十字标
e-resize
n-resize
nw-resize
w-resize
s-resize
se-resize
sw-resize

1.本地无缓存,每次自动刷新
response.expires=0
response.addHeader "pragma" , "no-cache"
response.addHeader "cache-control" , "private"

2.修改contentType并下载gif等格式
<%
function dl(f,n)
on error resume next

set s=CreateObject("Adodb.Stream")
S.Mode=3
S.Type=1
S.Open
s.LoadFromFile(server.mappath(f))
if err.number>0 then
response.write err.number & ":" & err.description
else
response.contentType="application/x-gzip"
response.addheader "Content-Disposition:","attachment; filename=" & n
response.binarywrite(s.Read(s.size))
end if
end function

call dl("012922501.gif","t1.gif")
%>


19. 检查一段字符串是否全由数字组成
<script language="javascript"><!--
function checkNum(str){return !/\D/.test(str)}
alert(checkNum("1232142141"))
alert(checkNum("123214214a1"))
// --></script>

20. 获得一个窗口的大小
document.body.clientWidth,document.body.clientHeight
document.body.offsetWidth,document.body.offsetHeight
有时还需要知道window.screenTop,window.screenLeft

21. 怎么判断是否含有汉字
if (escape(str).indexOf("%u")!=-1) alert("含有汉字");
else alert("全是字符");

22.TEXTAREA自适应文字行数的多少
IE 5.5+ 可以用 overflow-y:visible
<textarea rows=1 name=s1 cols=27 style="overflow-y:visible">
</textarea>

23. 日期减去天数等于第二个日期
<script language=javascript>
function cc(dd,dadd)
{
//可以加上错误处理
var d = new Date(dd.replace("-","/"))
d.setDate(d.getDate()+dadd)
alert(d.getFullYear() + "年" + (d.getMonth() + 1) + "月" + d.getDate() + "日")
}
cc("2002-2-28",2)
</script>
By [leon] at 16:23:07 | Comments[2] | 332 views
 ASP.NET常用功能源码--1  [2004-6-22]
风沙网友整理发表
 
引用:
XmlTextReader

<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="gb2312" %>
<%@ Import Namespace="System.Xml"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd";>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head>
<script runat="server">
Sub Button1_Click(sender as object,e as EventArgs)

Dim Reader as XmlTextReader
Reader = New XmlTextReader(Request.MapPath("Simple1.xml"))
Label1.text=""

While(Reader.Read())
If(Reader.NodeType = XmlNodeType.text)
Label1.text += (Reader.value + "<br>")
End If
End While
End Sub
</script>
<body>
<form runat="server">
<asp:Label id="Label1" runat="server"></asp:Label><br><br>
<asp:Button id="Button1" text="XmlReader" runat="server" onClick="Button1_Click"></asp:Button>
</form>
</body>
</html>


simple.xml
<?xml version="1.0" encoding="gb2312"?>
<library>
<book>
<author>Ernest Hemingway</author>
<title>For Whom the Bell Tolls</title>
</book>
<book>
<author>Dean Koontz</author>
<title>Watchers</title>
</book>
<book>
<author>Tom Clancy</author>
<title>The Cardinal of The Kremlin</title>
</took>



 
引用:
XmlTextWriter

<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="gb2312" %>
<%@ Import Namespace="System.IO"%>
<%@ Import Namespace="System.Xml"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd";>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>cp8xmlTextWriter</title>
</head>
<script runat="server">
Sub Button1_Click(sender As Object,e As EventArgs)
Dim Writer As XmlTextWriter
try
Writer = New XmlTextWriter(console.out)
Writer.Formatting = Formatting.Indented

Writer.WriteStartDocument()
Writer.WriteComment("Special Edition:Using ASP.NET is a great book!")
Writer.WriteProcessingInstruction("Read","Information")
Writer.WriteStartElement("i","Info","urn:Info")

Writer.WriteStartElement("Name","")
Writer.WriteString(TextBox1.text)
Writer.WriteEndElement()

Writer.WriteStartElement("Address","")
Writer.WriteString(TextBox2.text)
Writer.WriteEndElement()

Writer.WriteStartElement("City","")
Writer.WriteString(TextBox3.text)
Writer.WriteEndElement()

Writer.WriteStartElement("State","")
Writer.WriteString(TextBox4.text)
Writer.WriteEndElement()

Writer.WriteStartElement("Zip","")
Writer.WriteString(TextBox5.text)
Writer.WriteEndElement()

Writer.WriteEndDocument()
Writer.Close()

Label1.text="Done!"
catch ex As Exception
Label1.text=ex.Message
End Try
End Sub

Sub Button2_Click(sender As Object,e As EventArgs)
Dim reader As StreamReader
Label2.text=""
try
reader = File.OpenText(Request.MapPath("simple1.xml"))
while reader.Peek()<>-1
Label2.text+=(Server.HtmlEncode(reader.ReadLine())+"<br>")
End while
reader.Close()
catch ex As Exception
Label2.text=ex.Message
End try
End Sub
</script>
<body>
<form runat="server">
<table>
<tr>
<td>Name:</td>
<td><asp:TextBox id="TextBox1" runat="server" size=20></asp:TextBox></td>
</tr>
<tr>
<td>Address:</td>
<td><asp:TextBox id="TextBox2" runat="server" size=20></asp:TextBox></td>
</tr>
<tr>
<td>City:</td>
<td><asp:TextBox id="TextBox3" runat="server" size=20></asp:TextBox></td>
</tr>
<tr>
<td>State:</td>
<td><asp:TextBox id="TextBox4" runat="server" size=20></asp:TextBox></td>
</tr>
<tr>
<td>Zip:</td>
<td><asp:TextBox id="TextBox5" runat="server" size=20></asp:TextBox></td>
</tr>
<tr>
<td><asp:Button id="Button1" text="Button1" runat="server" onClick="Button1_Click"></asp:Button></td>
<td><asp:Button id="Button2" text="Button2" runat="server" onClick="Button2_Click"></asp:Button></td>
</tr>
</table>
<asp:Label id="Label1" runat="server"></asp:Label><br>
<asp:Label id="Label2" runat="server"></asp:Label>
</form>
</body>
</html>

simple1.xml

<?xml version="1.0" encoding="gb2312"?>
<library>
<book>
<author>Ernest Hemingway</author>
<title>For Whom the Bell Tolls</title>
</book>
<book>
<author>Dean Koontz</author>
<title>Watchers</title>
</book>
<book>
<author>Tom Clancy</author>
<title>The Cardinal of The Kremlin</title>
</book>
</library>



 
引用:
StringJoin

<%@ Page Language="VB"%>

<script runat="server">
Sub Page_Load(Obj As Object,E As EventArgs)
Dim Result As String
Dim strA() As String={"VB.NET","ASP.NET","C#"}
Result=String.Join("*",strA)
message.Text = Result
End SUb
</script>

<html>
<head>
<title></title>
</head>
<body>
<asp:Label id="message" runat="server" />
</body>
</html>



 
引用:
StringSplit

<%@ Page Language="VB"%>

<script runat="server">
Sub Page_Load(Obj As Object,E As EventArgs)


Dim strA() As String
Dim i As Integer
strA = "VB.NET is good!".Split(" ")
For i=0 To strA.Length-1
response.write(strA(i))
response.write("<br>")
Next
End Sub
</script>

<html>
<head>
<title></title>
</head>
<body>

</body>
</html>



 
引用:
Browser

<%@ Page Language="VB"%>

<script runat="server">
Sub Page_Load(Sender As Object,E As EventArgs)
Dim strMessage As String
strMessage="浏览器类型:" & Request.Browser("Browser")
strMessage & ="<br>浏览器版本:" & Request.Browser("Version")
strMessage & ="<br>支持Cookies否:" & Request.Browser("Cookies")
strMessage & ="<br>支持背景音乐否:" & Request.Browser("BackGroundSounds")
message.Text=strMessage
End Sub
</script>

<html>
<head>
<title></title>
</head>
<body>
<h4 align="center">客户端浏览器特性</h4>
<form runat="server">
<asp:Label id="message" runat="server" />
</form>
</body>
</html>



 
引用:
Calendar

<%@ Page Language="VB"%>

<script runat="server">
Sub Date_Selected(sender As Object,E As EventArgs)
message.Text = "选定日期为:" & Calendar1.SelectedDate.ToLongDateString
End Sub
</script>

<html>
<head>
<title></title>
</head>
<body>
<h4 align="center">日历示例</h4>
<form runat="server">
<asp:Calendar id="Calendar1" onSelectionChanged="Date_Selected"
TodayDayStyle-BackColor="gray" SelectedDayStyle-BackColor="#6666CC" runat="server" />
<p><asp:Label id="message" runat="server" />
</body>
</form>
</html>



 
引用:
CreateText

<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="gb2312" %>
<%@ Import NameSpace="System.IO"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd";>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>CreateText</title>
</head>
<script language="vb" runat="server">
Sub SendButton_Click(Sender As Object,e As EventArgs)
Dim StrWriteObj As StreamWriter '定义一个StreamWrite对象
Dim File_Name As String
Dim Contxt As String
File_Name=FileName.text '创建一个文件,并把此文件作为对象分派给StreamWrite对象
Contxt=Body.text
StrWriteObj=File.CreateText(File_Name)
StrWriteObj.WriteLine(contxt) '把文件内容写入文件中
StrWriteObj.Close '关闭对象
Response.Write("成功的进行了文件写操作!")
panel1.visible=False
End Sub
</script>
<body>
<asp:panel id="panel1" runat="server" />
<form id="form1" runat="server">
<p>欢迎在ASP.NET页面对文件进行写操作</p>
请填入文件名称:<asp:TextBox id="FileName" size="40" runat="server" /><br>
请填入文件内容:<asp:TextBox id="Body" TextMode="MultiLine" Columns="40" Rows="10" runat="server" /><br>
<asp:Button runat="server" id="Write" Text="写入文件" onClick="SendButton_Click" />
</form>
</body>
</html>