现在的位置: 首页 > 综合 > 正文

基于ASP的Web开发中常用代码

2012年09月23日 ⁄ 综合 ⁄ 共 6080字 ⁄ 字号 评论关闭

<!--使表格带有连接功能--->
<html>
<head>
<title>HTML document</title>
</head>
<body>
<table border="1" cellpadding="0" cellspacing="0" width="200" height="200" bgcolor="Gray"><tr><td onmouseover=this.bgColor="#800000" onclick=window.location.href="http://www.google.com">google</td></tr><tr><td onmouseover=this.bgColor="#008000" onclick=window.location.href="http://www.blueidea.com">bluidea.c ... 0" onclick=window.location.href="http://www.blueidea.com/bbs">bbs</td></tr></table>
</body>
</html>

<%
'获取URL值,跳转到新网址
dim a,b
a=request.querystring("boardID")
b=request.querystring("id")
response.redirect("http://bbs.anquan.com.cn/dispbbs.asp?boardid ... "&id="&b)
%>

<%@ Language=VBScript %>
<html>
<title>No.2根据远程主机地址来进行判断,如果为本地地址则进入欢迎页面,否则显示出错信息.</title>
<body>

本地ip
<%=Request.ServerVariables("REMOTE_ADDR")%>
服务器IP
<%=Request.ServerVariables("LOCAL_ADDR")%>
<%
dim address
address = request.servervariables("REMOTE_ADDR")
response.write address
if address="127.0.0.1" then
'如果为本地,则显示欢迎页面.
response.write "你好,欢迎进入本站点."
'response.redirect "http://localhost/100asp/001.asp"
else
'否则显示出错信息.
response.write "对不起,你无权查看内部站点."
end if
%>
</body>
</html>

'按时间判断,显示最新标志
<%
mytime=rs("time") '数据库中保存时间
nowtime=now() '当前时间
if datediff("d",mytime,nowtime)<1 then
'比较当前时间和数据库中时间,如相差小于1,即显示图片new.gif
%>
<img src="images/new.gif">
<%end if%>

'图片大时,自动缩小的写法.
<img border="0" src="<%=trs("e_simg")%>" onload="javascript:if(this.width>120)this.width=120" alt="<%=trs("e_trem")%>">

'几个条件语句的写法
while ...........wend
do while .................loop
有判断是
if........else end if
if........elseif end if

'长标题截取的写法1
<% if len(rs("showname"))>13 then%>
<%=left(rs("showname"),12)%>..
<% else%>
<%=rs("showname")%>
<%end if%>
'写法2
<%if len(rs("quizTitle"))>13 then response.write left(rs("quizTitle"),12)&".." else response.write rs("quizTitle") end if%>

'出错或完成操作后提示语句.
<%
response.write "<script language=javascript>alert('请留言!');window.location.href='liuyan.asp';</script>"
%>
<%
response.write "<script language=JavaScript>" & chr(13) & "alert('请检查您填写的内容是否完整!');" & "history.back()" & "</script>"
%>

'把表中有限的几个分类转换成"汉字"
<%
dim quizClass,quizOption
'quizClass=rs("quizLib.quizClass")
quizClass=rs("quizClass")
select case quizClass
case "radio1"
response.Write("判断题")
case "radio2"
response.Write("单选题")
case "checkbox"
response.Write("多选题")
case "text"
response.Write("填空题")
end select
%>

'确定删除一条记录
<a href="del.asp?info_id=<%=rs("info_id")%>" onclick=return(confirm("确定要删除第<%=rs("info_id")%>条记录吗?"))>删除</a>

'动态显示下拉菜单
'如果记录集为空就指向主页
'value值为表subject中的ID
<select name="booktype" class="button1" id="booktype" style="width:250">
<option value=all selected>试题学科</option>
<%
sql="SELECT * FROM subject"
set rs=conn.execute(sql)
if rs.bof or rs.eof then response.Redirect("index.asp")
while not(rs.bof or rs.eof)
response.Write "<option value="&rs("id")&">"&rs("subjName")&"</option>"
rs.movenext
wend
%>
</select>

'获取数据库里的
<select name="mem_sex" id="mem_sex">
<option value="男" <%if rs("mem_sex")="男" then response.write "selected"%>>男</option>
<option value="女" <%if rs("mem_sex")="女" then response.write "selected"%>>女</option>
</select>
<!--限制文本框不能输入空格-->
<script language="javascript">
function id_keyup(txtinput)
{
txtinput.value=txtinput.value.replace(/(^/s*)|(/s*$)/g, "");
}
</script>
<input type="text" name="id" onkeyup="id_keyup(this)">

9.随机背景音乐:
<%randomize%>
<bgsound src="mids/<%=(int(rnd()*60)+1)%>.mid" loop="-1">

可以修改数字,限制调用个数,我这里是60个.

状态栏文字的显示,这个简单实用!
<BASE onmouseover="window.status='欢迎光临!-我的邮件:mxsky{at}126.com';return true">

打开的一个迷你窗口

『复制代码』『运行代码』

<script language="javascript">
function mini_window(page,w,h)
//打开一个小窗口
/*
page 页面名称
w 宽度
h  高度
*/
{
var arr = showModalDialog(page, "", "dialogWidth:"+w+"px; dialogHeight:"+h+"px; status:0;help:0");
}
//打开一个小窗口
</script>
<body style="overflow:scroll;overflow-x:hidden;overflow-y:hidden">
<a style=cursor:hand ; onclick=mini_window('default.asp','650','515')>打开新窗口</a>
</body>

加标题的电子邮件:
<a href="mailto:admin@anquan.com.cn?subject=关于考试系统的问题">flashboy</a>

iframe调用时如何让背景透明
<Iframe src="transparent.htm" allowtransparency="true"></Iframe>
transparent.htm
<body style="background-color:transparent">

按钮在原窗口打开
<input type="button" name="button2" value="注册" onclick="location.href='http://www.google.com'">

按钮在新窗口打开
<input type="button" name="button2" value="注册" onclick="window.open('http://www.google.com')">

让英文字符串超出表格宽度自动换行
<td style="word-wrap:break-word; word-break:break-all;">

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

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

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

删除时确认
<a href='javascript:if(confirm("确实要删除吗?"))location="del.asp"'>删除</a>

不要滚动条
让竖条没有:
<body style='overflow:scroll;overflow-y:hidden'>
</body>
让横条没有:
<body style='overflow:scroll;overflow-x:hidden'>
</body>
两个都去掉?更简单了:
<body scroll="no">
</body>

鼠标划过,表格变色
<tr onmouseover="this.bgColor='red'" onmouseout="this.bgColor=''">

屏蔽右键
<body oncontextmenu="return false;">

<script language="JavaScript">
<!--
if (window.Event)
document.captureEvents(Event.MOUSEUP);
function nocontextmenu()
{
event.cancelBubble = true
event.returnValue = false;
return false;
}
function norightclick(e)
{
if (window.Event)
{
if (e.which == 2 || e.which == 3)
return false;
}
else
if (event.button == 2 || event.button == 3)
{
event.cancelBubble = true
event.returnValue = false;
return false;
}
}
document.oncontextmenu = nocontextmenu; // for IE5+
document.onmousedown = norightclick; // for all others
//-->
</script>

用正则强制输入数字
<input name="id2" type="text" onkeyup='this.value=this.value.replace(//D/gi,"")'>

禁止输入中文
<input type=text style="ime-mode: disabled ">

不允许文本框自动完成
<input autocomplete=off>

图片变灰,掠过恢复
<img src="http://community.csdn.net/images/csdn.gif" style="filter:gray" onmo ... useout="this.style.filter='gray'">

定时跳转到其它页面
<meta http-equiv="refresh" content="5;URL=http://54caizi.com">

TITLE换行
<a href=# title="第一行第二行第三行">title分行测试</a>

行背景色渐变
<table width="100%" height="100%">
<tr><td style="FILTER: progid:DXImageTransform.Microsoft.Gradient(endColorstr='#EFF3FF', startColorstr='#94B2F7', gradientType='1')" ></td></tr>
</table>
说明:
功能从startcolorstr渐变到endcolorstr
gardientType 是填充样式,具体值请自己尝试

调用其它页面
<object type="text/x-scriptlet" width="800" height="1000" data="a.htm"></object>

Iframe的常用用法
<iframe WIDTH=100% HEIGHT=240 MARGINWIDTH=0 MARGINHEIGHT=0 HSPACE=0 VSPACE=0 FRAMEBORDER=0 allowTransparency="true" SCROLLING=no SRC='Cay/blog.asp'></iframe>

抱歉!评论已关闭.