javascript|技巧
(一).確認刪除用法:
1. BtnDel.Attributes.Add("onclick","return confirm('"+"確認刪除?"+"')");
2. linktempDelete.Attributes["onclick"]="javascript:return confirm('"+"確認刪除?"+"');";
3. private void grdProject_ItemDataBound(object sender, DataGridItemEventArgs e)
4.
{
if ((e.Item.ItemType == ListItemType.Item) | (e.Item.ItemType == ListItemType.AlternatingItem))
{
// 刪除按鈕上的提示部分
e.Item.Cells[10].Attributes.Add("onclick", "return confirm('確定刪除嗎?');");
}
}
5.
<script language="JavaScript" type="text/JavaScript">
function delete_y(e)
{
if(event.srcElement.outerText == "刪除")
event.returnValue=confirm("確定刪除?");
}
document.onclick=delete_y;
</script>
(二).跨語言字串替換
System.Text.RegularExpressions.Regex.Replace(str1,@"<{1}[^<>]*>{1}","").Replace(" ","").Replace("\","\\").Replace("-","-").Replace("&","&").Replace("<","<").Replace(">",">").Replace("br","\n");
(三).關閉表單
1.
this.btnClose.Attributes.Add("onclick", "window.close();return false;");
2.關閉本表單間跳轉到另一個頁面
this.HyperLink1.NavigateUrl = "javascript:onclick=window.opener.location.assign
('" + "index.aspx"+ "?&Func=Edit&AutoID=" + intAutoid + ');window.close();";
3.關閉父表單:
<INPUT type="reset" value='<%=this._Cancel%>' Class="Button">
(四).Web MessageBox
1.
Response.Write("<script language=javascript> window.alert('儲存成功1');</script>");
2.
Response.Write("<script>alert('"+"儲存成功!"+"');</script>");
3.
this.Page.RegisterStartupScript("ChiName","<script language='javascript'>alert('" + "儲存成功!" + "')</script>");
(五).DataGrid中加CheckBox控制項範本列.
請看: http://blog.csdn.net/chengking/archive/2005/10/08/497520.aspx
(六). window.open() 方法
文法:window.open(pageurl,name,parameters);
window對象的open()方法用於建立一個新的視窗執行個體,新建立的視窗的外觀由參數:parameters指定。新視窗中開啟的文檔由參數:
pageurl指定。系統能夠根據參數:name確定的名稱訪問該視窗。
下表為parameters參數表:
參數 衩始值 說明
alwaysLowered yes/no 指定視窗隱藏在所有視窗之下。
alwaysRaised yes/no 指定視窗浮在所有視窗之上。
dependent yes/no 指定開啟的視窗為父視窗的一個了視窗。並隨父視窗的關閉而關閉。
directions yes/no 指定Navigator 2和3的目錄欄是否在新視窗中可見。
height pixel value 設定新視窗的像素高度。
hotkeys yes/no 在沒有功能表列的新視窗設定安全退出熱鍵。
innerHeight pixel value 設定新視窗中文檔的像素高度。
innerWidth pixel value 設定新視窗中文檔的像素寬度。
location yes/ no 指明位置欄在新視窗中是否可見。
menubar yes /no 指明功能表列在新視窗中是否可見。
outerHeight pixel value 設定視窗(包括裝飾邊框)的像素高度。
outerWidth pixel value 設定視窗(包括裝飾邊框)的像素寬度。
resizable yes /no 指明新視窗是否可以調整。
screenX pixel value 設定新視窗離螢幕邊界的像素長度。
screenY pixel value 設定新視窗離螢幕上邊界的像素長度。
scrollbars yes /no 指明滾動欄在新視窗中是否可見。
titlebar yes /no 指明菜單題目欄在新視窗是否可見。
toolbar yes /no 指明工具列在新視窗中是否可見。
Width pixel value 設定視窗的像素寬度。
z-look yes /no 在文檔中包含各個<pplet>標籤的數組。
fullscreen yes / no 開啟的表單是否進行全螢幕顯示
left pixel value 設定新視窗距螢幕左方的距離
top pixel value 設定新視窗距螢幕上方的距離
例子:
<html>
<head>
<title>window.open函數</title>
</head>
<body>
<script language="javascript">
<!--
window.open("","name1","width=100,height=200,toolbar=no,scrollbars=no,menubar=no,screenX=100,screenY=100");
//-->
</script>
</body>
</html>
(七).location對象
href 整個URL字串.
protocol 含有URL第一部分的字串,如http:
host 包含有URL中主機名稱:連接埠號碼部分的字串.如//www.cenpok.net/server/
hostname 包含URL中主機名稱的字串.如http://www.cenpok.net
port 包含URL中可能存在的連接埠號碼字串.
pathname URL中"/"以後的部分.如~list/index.htm
hash "#"號(CGI參數)之後的字串.
search "?"號(CGI參數)之後的字串.
(八).按鍵捕捉
1.Ctrl+Enter按鍵捕捉方法
<body >
<script language="JavaScript">
<!--
function doKeyDown()
{
if (event.ctrlKey && event.keyCode == 13)
{
alert("You pressed the Ctrl + Enter")
}
}
//-->
</script>
</body>
2.Alt加快速鍵: Alt+A
<button accessKey=A title="Alt+A" >Alt+A</button>
(九).控制輸入,非法字元不能輸入到TextBox.
<asp:textbox class="Text"
onkeypress="if (event.keyCode < 48 || event.keyCode >57) event.returnValue = false;"
id="txtY_Revenue" style="TEXT-ALIGN: right" runat="server" Width="90%" MaxLength="12">
</asp:textbox>
說明: 此方法控制TextBox只收數字:0~9 , 也自可以定義其它可輸入字元,如改成: 65~123,只允許輸入: a~z和A~Z 等.