ASP 精華源碼收集(五年總結(jié))
更新時(shí)間:2008年10月20日 22:58:16 作者:
在5年的開(kāi)發(fā)中,總結(jié)出來(lái)的一些源碼
具有edit功能的combobox
<HTML>
<HEAD>
<PUBLIC>
<COMPONENT TAGNAME="COMBOBOX">
<PROPERTY NAME="Text" GET="get_Text" PUT="put_Text">
<PROPERTY NAME="Width" GET="get_Width" PUT="put_Width">
<METHOD NAME="SetFocus" INTERNALNAME="htcFocus">
<METHOD NAME="AddItems" INTERNALNAME="htcAddItems">
<EVENT NAME="onYYCenter" ID="idEnter">
<EVENT NAME="onYYCChoose" ID="idChoose">
<ATTACH EVENT="oncontentready" ONEVENT="htcInit()">
</COMPONENT>
</PUBLIC>
<SCRIPT LANGUAGE="javascript">
function htcInit()
{
defaults.viewLink=document;
defaults.viewInheritStyle=false;
Body_Init();
}
function htcAddItems(items)
{
var i,len;
len=pCombo.options.length;
for(i=0;i<len;i++)
{pCombo.remove(0);}
for(i=0;i<items.length;i++)
{
var o;
if((typeof items[i])=='string')
{
if(!HasTheValue(items,i))
{
o=document.createElement('OPTION');
o.text=items[i];
pCombo.add(o);
}
}
}
}
function htcFocus()
{
pText.focus();
}
function get_Text()
{
return pText.value;
}
function put_Text(Value)
{
pText.value=Value;
}
function get_Width()
{
return pCombo.style.width;
}
function put_Width(Value)
{
pCombo.style.width=Value;
}
</SCRIPT>
<SCRIPT LANGUAGE="javascript">
function Body_Init()
{
var iRight=pCombo.clientWidth;
var iBottom=pCombo.clientHeight;
var iLeft=(pCombo.clientWidth-18);
pCombo.style.clip='rect(0,'+iRight+','+iBottom+','+iLeft+')';
pText.style.width=(pCombo.clientWidth);
pText.style.height=(pCombo.clientHeight);
pText.style.top=0;
pText.style.left=0;
}
function Combo_Select()
{
pText.value=pCombo.options[pCombo.selectedIndex].text;
}
function Text_ChkKey()
{
if(event.keyCode==13)
{
idEnter.fire();
}
}
function HasTheValue(sitems,i)
{
var ii;
for(ii=0;ii<i;ii++)
{
if(sitems[ii]==sitems[i])
return true;
}
return false;
}
</SCRIPT>
</HEAD>
<BODY>
<SELECT STYLE="position:absolute;left:0;top:0;" ONCHANGE="Combo_Select()" NAME="pCombo">
</SELECT>
<INPUT STYLE="position:absolute;left:0;top:0;z-index:4000" onKeyPress="Text_ChkKey()" TYPE="TEXT" NAME="pText">
</BODY>
</HTML>
var txtVal = theTxtObj.value;
if (comVal == txtVal)
{
theComObj.selectedIndex = i;
return;
}
}
}
function doResize()
{
if (!this.beResizing)
{
this.beResizing = true;
this.txtObj.style.display="none";
this.comObj.style.position="static";
this.txtObj.style.posLeft = getLeftPostion(this.comObj);
this.txtObj.style.posTop = getTopPostion(this.comObj) + 1;
this.txtObj.style.posWidth = this.comObj.offsetWidth - 16;
this.txtObj.style.posHeight = this.comObj.offsetHeight;
this.comObj.style.position ="absolute";
this.comObj.style.posLeft = this.txtObj.style.posLeft;
this.comObj.style.posTop = this.txtObj.style.posTop;
this.offWidth = this.comObj.offsetWidth;
var strRect = "rect(0 "+(this.comObj.offsetWidth)+" "+ this.comObj.offsetHeight + " "+(this.txtObj.style.posWidth - 2 )+")";
this.comObj.style.clip = strRect;
this.txtObj.style.display="";
this.beResizing = false;
}
}
function doChange()
{
var idx = this.comObj.selectedIndex;
var opt = this.comObj.options[idx];
this.txtObj.value = opt.text;
this.txtObj.focus();
this.txtObj.select();
this.comObj.selectedIndex=-1;
}
function getValue()
{
return this.txtObj.value;
}
function doSelectIdx(i)
{
var optLen = this.comObj.options.length;
if ((i >=0) && (i < optLen))
{
this.comObj.selectedIndex = i;
this.txtObj.value = this.comObj.options[i].text;
return;
}
this.txtObj.value = "";
}
function focus()
{
this.txtObj.focus();
}
/*resize all combobox when window be resized*/
function resetAllSize()
{
var i;
for (i=0; i < theArray.length; i++)
{
theArray[i].doResize();
}
}
////////////////定位函數(shù)開(kāi)始,我加的///////////////
function keyPress()
{
var txtStr;
var comStr;
var maxInt = 0;
var defInt = 0;
var defJ;
txtStr = this.txtObj.value;
//document.form1.test.value=txtStr;
var j;
for(j=0;j<this.comObj.options.length;j++)
{
comStr = this.comObj.options[j].text;
var m;
for(m=0;m<txtStr.length+1;m++)
{
if(txtStr.charCodeAt(m) != comStr.charCodeAt(m))
{
maxInt = m;
break;
}
}
if (defInt < maxInt)
{
defInt = maxInt;
defJ = j;
}
}
this.comObj.selectedIndex = defJ;
}
使用asp 結(jié)合數(shù)據(jù)庫(kù)實(shí)現(xiàn)不限級(jí)數(shù)的彈出菜單(原創(chuàng))——流行技術(shù)
不死鳥(niǎo)
下面是我寫的一個(gè)程序,因?yàn)橛X(jué)得比較好,值得借鑒,
所以放了上來(lái),
如果看的不清楚,請(qǐng)點(diǎn)擊引用
如果轉(zhuǎn)載,請(qǐng)注明出自考網(wǎng)流行技術(shù)論壇(不死鳥(niǎo) QQ 33054474)
謝謝
< !-- #include virtual='Include/database.asp' -->
< %
set rs=Server.CreateObject("ADODB.RecordSet")
sql="SELECT menu_name, menu_link, menu_bgcolor, menu_color,id FROM enter_individual WHERE (parent_id = (SELECT id FROM enter_individual WHERE menu_flag = 'root' )) " '查詢得到根節(jié)點(diǎn)
rs.Open sql,conn,1,1
Response.Write "< table width=100% border=0 cellspacing=1 cellpadding=0 align=center>< tr bgcolor=#3399CC valign=bottom align=center>"
sumnum=rs.RecordCount
myArray=rs.GetRows()
rs.Close ()
widd=780/sumnum '從一級(jí)子菜單數(shù)目判斷彈出菜單x坐標(biāo)的遞增像素
dim i
i=0
defaultbgcolor="#3399cc" '指定默認(rèn)底色
defaultcolor="#ffffff" '指定默認(rèn)字體顏色
defaultlink="#" '指定默認(rèn)鏈接
posit_x=0 'x位置
flag=1 '標(biāo)志,作為菜單彈出方向 1表示向右,0表示向左
'Response.Write myArray(4,6)
while i Response.Write "< td height=20 bgcolor="&myArray(2,i)&" width="&widd&">< a href="../../&myArray(1,i)&" onmouseover=java script:a"&myArray(4,i)&".style.display='block' onmouseout=java script:a"&myArray(4,i)&".style.display='none' >< font color="&myArray(3,i)&" >"&myArray(0,i)&"< /a>< /td>"
'-----------------------------------------------
i=i+1
Wend
Response.Write "< /tr>< /table>"
i=0
while i posit_y=100 'y位置回到原位
If i>=(sumnum/2) Then '如果菜單進(jìn)入右半部分,則彈出轉(zhuǎn)向
flag=0
End If
'調(diào)用GetSubMenu 函數(shù) 設(shè)置該項(xiàng)一級(jí)菜單的下級(jí)菜單,以myArray(4,i) 即菜單id作為下級(jí)菜單所在div 的id
GetSubMenu myArray(4,i),posit_x,posit_y
posit_x=posit_x+widd '下一個(gè)一級(jí)菜單的子菜單的 x坐標(biāo)值增加一個(gè)單位
i=i+1
Wend
'使用遞規(guī)算法的到下級(jí)菜單的函數(shù)
'parent_id 父 id; posit_x 彈出層的左邊位置; posit_y 彈出層的離上面位置;
Function GetSubMenu(parent_id,posit_x,posit_y)
dim myArray
dim sumnum
dim i
'查詢子菜單的下級(jí)菜單
sql="SELECT menu_name, menu_link, menu_bgcolor, menu_color,id FROM enter_individual WHERE parent_id = "&parent_id&" AND user_id = '"&userid&"'"
rs.Open sql,conn,1,1
'如果下級(jí)菜單不存在,則層數(shù)減一 ,關(guān)閉數(shù)據(jù)庫(kù)鏈接,建立一個(gè)以父id為div id的空層,然后返回
If rs.EOF=true Then
level=level-1
rs.Close ()
'Response.Write parent_id
Response.Write "< div id='a"&parent_id&"' style='position: absolute; top: 4; left: -1; display: none; width: 0; height: 0'>< /div>"
Else
'如果存在取到數(shù)據(jù)庫(kù)數(shù)據(jù),并調(diào)用SetSubMenu顯示菜單
sumnum=rs.RecordCount
myArray=rs.GetRows()
rs.Close ()
SetSubMenu myArray,sumnum,parent_id,posit_x,posit_y
'對(duì)數(shù)據(jù)進(jìn)行循環(huán),遞規(guī)調(diào)用GetSubMenu
i=0
while i< sumnum
posit_y=posit_y*1+20 '遞規(guī)一次posit_y 加一個(gè)單位,
if level=0 Then '如果級(jí)數(shù)減到0 則回到1
level=1
End If
'If flag=1 Then
'GetSubMenu myArray(4,i),posit_x+level*widd,posit_y-level*20 '遞規(guī)調(diào)用GetSubMenu x,y坐標(biāo)延伸 level 個(gè)單位
'End If
If flag=0 Then
GetSubMenu myArray(4,i),posit_x-level*widd,posit_y-level*20 '遞規(guī)調(diào)用GetSubMenu x,y坐標(biāo)延伸 level 個(gè)單位
Else
GetSubMenu myArray(4,i),posit_x+level*widd,posit_y-level*20 '遞規(guī)調(diào)用GetSubMenu x,y坐標(biāo)延伸 level 個(gè)單位
End If
i=i+1
Wend
End If
End Function
'設(shè)置子菜單函數(shù)
'myArray 菜單數(shù)據(jù) ,sumnum 數(shù)組大小 ,parent_id 層的id ;
'posit_x 彈出層的左邊位置; posit_y 彈出層的離上面位置;
Function SetSubMenu (myArray,sumnum,parent_id,posit_x,posit_y)
dim i
parent_id="a"&parent_id '父菜單id前面加上a 作為層的id
hh=sumnum*20 '數(shù)組大小乘以20作為層的高度
Response.Write "< DIV onmouseover=java script:"&parent_id&".style.display='block' onmouseout=java script:"&parent_id&".style.display='none' ID='"&parent_id&"' STYLE='position: absolute; top:"&posit_y&"; left:"&posit_x&"; height:"&hh*1&"; width: "&widd&"; display:none;vertical-align: top'>< table width=100% border=0 cellspacing=1 cellpadding=0 >"
i=0
While i
myArray(0,i)=Trim(myArray(0,i))
myArray(1,i)=Trim(myArray(1,i))
myArray(2,i)=Trim(myArray(2,i))
myArray(3,i)=Trim(myArray(3,i))
If myArray(2,i)="" Then
myArray(2,i)=defaultbgcolor
End If
If myArray(3,i)="" Then
myArray(3,i)=defaultcolor
End If
If myArray(1,i)="" Then
myArray(1,i)=defaultlink
End If
Response.Write "< tr align=center >< td width=100% height=20 bgcolor="&myArray(2,i)&" onmouseover=java script:"&parent_id&".style.display='block';a"&myArray(4,i)&".style.display='block' onmouseout=java script:a"&myArray(4,i)&".style.display='none'>< a href='"&myArray(1,i)&"'>< font color="&myArray(3,i)&" >"&myArray(0,i)&"< /font>< /a>< /td>< /tr>"
i=i+1
Wend
Response.Write " < /table> < /DIV>"
End Function
Set rs=nothing
conn.Close ()
Set conn=nothing
% >
------自動(dòng)選定文本框中的文本
<input type="text" name="mtext" size="100" width=20 value="待選中的文本,把鼠標(biāo)移上去看看:)" onmouseover="focus();select();">
強(qiáng)制ie下載代碼:
示例:
下載source/aaa.zip文件
<a href="download.asp?n=aaa.zip">
download.asp:
<%
Response.Buffer = true
Response.Clear
dim url
Dim fso,fl,flsize
dim Dname
Dim objStream,ContentType,flName,isre,url1
'*********************************************調(diào)用時(shí)傳入的下載文件名
Dname=trim(request("n"))
‘******************************************************************
If Dname<>"" Then
'******************************下載文件存放的服務(wù)端目錄
url=server.MapPath("../source")&"\"&Dname
‘***************************************************
End If
Set fso=Server.CreateObject("Scripting.FileSystemObject")
Set fl=fso.getfile(url)
flsize=fl.size
flName=fl.name
Set fl=Nothing
Set fso=Nothing
%>
<%
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Open
objStream.Type = 1
objStream.LoadFromFile url
Select Case lcase(Right(flName, 4))
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 ".txt"
ContentType = "text/plain"
Case Else
ContentType = "application/octet-stream"
End Select
Response.AddHeader "Content-Disposition", "attachment; filename=" & flName
Response.AddHeader "Content-Length", flsize
Response.Charset = "UTF-8"
Response.ContentType = ContentType
Response.BinaryWrite objStream.Read
Response.Flush
response.Clear()
objStream.Close
Set objStream = Nothing
%>
JS判斷輸入日期的正確性
<script language=javascript>
function strDateTime(str){
var reg = /^(\d{1,4})(-\/)(\d{1,2})\2(\d{1,2})$/;
var r = str.match(reg);
if(r==null)return false;
var d= new Date(r[1], r[3]-1,r[4]);
var newStr=d.getFullYear()+r[2]+(d.getMonth()+1)+r[2]+d.getDate()
return newStr==str
}
alert(strDateTime("2002-1-31"))
alert(strDateTime("2002-1-41"))
</script>
如果alert(strDateTime("2002-01-31"))
則判斷為非法
所以我是這樣寫的
function isDate(str){
if (str.length==0)return true;
var reg = /^(\d{4})(-\/)(0?[1-9]1[0-2])(-\/)(0?[1-9][12][0-9]3[01])$/g;
var r = reg.exec(str);
if(r==null)return false;
var d = new Date(r[1], r[3]-1,r[5]);
var newStr=d.getFullYear()+r[2]+(d.getMonth()+1)+r[2]+d.getDate()
var reg=/(\/-)(0*)( *)([1-9])/g //(容許空格﹐這個(gè)可取消)
var t=s.value.replace(reg,'$1$4')
if (newStr==t)return true;
return false;
}
外國(guó)人的系統(tǒng),從數(shù)據(jù)庫(kù)中提取的數(shù)據(jù)顯示為?,加入如下語(yǔ)句即可
<%@ Language=VBScript codepage=936 %>
二十八條改善 ASP 性能和外觀的技巧
http://www.microsoft.com/china/technet/iis/tips/ASPTIPS.asp
相關(guān)文章
ASP把長(zhǎng)的數(shù)字用逗號(hào)隔開(kāi)顯示的代碼
對(duì)于一些比較長(zhǎng)的數(shù)字,我們可以用asp實(shí)現(xiàn)隔開(kāi)顯示,尤其是一些金錢相關(guān)的東西2008-06-06asp連接SQL和Access數(shù)據(jù)代碼(asp里的隨機(jī)函數(shù))
asp連接SQL和Access數(shù)據(jù)代碼,asp里的隨機(jī)函數(shù),需要的朋友可以參考下2012-09-09asp下實(shí)現(xiàn)對(duì)HTML代碼進(jìn)行轉(zhuǎn)換的函數(shù)
asp下實(shí)現(xiàn)對(duì)HTML代碼進(jìn)行轉(zhuǎn)換的函數(shù)...2007-08-08循環(huán)取值Request.QueryString的用法
2008-01-01asp+Access通用的自動(dòng)替換數(shù)據(jù)庫(kù)中的字符串
幾年前寫的一個(gè)腳本,可以自動(dòng)進(jìn)行整庫(kù)的數(shù)據(jù)替換。2008-06-06asp下實(shí)現(xiàn) 重新排序數(shù)字?jǐn)?shù)組的代碼
asp下實(shí)現(xiàn) 重新排序數(shù)字?jǐn)?shù)組的代碼...2007-08-08