ASP.NET 2.0 学习笔记 1: session 与 script 应用
一.用SESSION保存登錄值,如果在打開新頁面時,該值為空,則自動跳轉至登陸頁面
?? 1.1 登陸時保存userID 信息;
Codepublic?partial?class?Login?:?System.Web.UI.Page
{
????protected?void?Page_Load(object?sender,?EventArgs?e)
????{
????}
????protected?void?ButtonLogin_Click(object?sender,?EventArgs?e)
????{
????????????????if?(userPwd?==?txtPassword.Text)//密碼正確
????????????????{
????????????????????
????????????????????Session["userID"]?=?txtUserName.Text.Trim();//存儲用戶名
????????????????????Response.Redirect("LoginSystem.aspx");??//進入系統
????????????????}
????????????????else
????????????????{
????????????????????Response.Write("<script?language=javascript>alert('您輸入的密碼錯誤!')</script>");
????????????????}
????}?
}
?? 1.2 打開其它頁面時,驗證userID信息;
protected?void?Page_Load(object?sender,?EventArgs?e)
????{
????????if?(this.Session["userID"]?==?null)
????????{
????????????Response.Redirect("Login.aspx");
????????}
????}
?
?? 1.3 用戶離開時,銷毀userID
Code///?<summary>
?///?用戶單擊“離開”時的事件
?///?</summary>
?///?<param?name="sender"></param>
?///?<param?name="e"></param>
?protected?void?ButtonExit_Click(object?sender,?System.EventArgs?e)
?{
?????Session["userID"]=null;???//銷毀Session中的用戶信息
?????Response.Write("<Script?Language=JavaScript>window.top.location=\"Login.aspx\";</Script>");
?}
?? 說明:
?? 跳轉的時候如何指明targer:
? ?=========================
?? 測試前提:一個頁面中含有兩個或兩個以上框架;
??讓最頂層的框架跳轉,就是整個頁面?
??window.top.location???=???"……?"?
??讓當前頁面的父框架跳轉?
??window.parent.location???=???"……?"?
??讓指定框架跳轉?
??window.top.框架名.location???=???"……?"?
??讓當前頁面的父框架跳轉,不彈出?
??<a?href="#"?onclick="window.parent.location='?..'>xxx</a>
?
??? 如: mainFrom.aspx 中的html代碼:
??? 該窗體中含有兩個框架,framea,frameb;
??? framea 加載 formA.aspx;
??? frameB 加載 formB.aspx;
<form?id="mainFrom"?method="post"?runat="server">
????<table?align="center">
?????<tr>
??????<td?style="width:?500px"><iframe?id?="framea"?src="formA.aspx"?width="100%"?height="100%"></iframe>
??????</td>
?????</tr>
?????<tr>
??????<td?style="width:?500px"><iframe?id?="frameb"?src="formB.aspx"?width="100%"?height="100%"></iframe>
??????</td>
?????</tr>
????</table>
??</form>
?
二.關閉當前窗口
javascript:window.close()
Response.Write("<script?language=javascript>javascript:window.close()?</script>");
?
三.與用戶交互,對話框
? 3.1 通過添加控件的屬性實現:?
? imgBtnSubmit.Attributes.Add("OnClick", "javascript:return confirm('確實要提交嗎?')");??????? //給出提示
?
? 如果用戶點擊確定,則執行OnClick事件里的動作;
? imgBtnSubmit 為 System.Web.UI.WebControls.ImageButton
?
? 3.2 用script 腳本實現:
<script?language=”JavaScript”>
???var?userChoice?=?window.confirm(“Click?OK?or?Cancel”);
???if?(userChoice)?{
???document.write(“You?chose?OK”);
???}?else?{
???document.write(“You?chose?Cancel”);
???}
???</script>
?
四.頁面自動刷新技術
?? 通過瀏覽器的client pull 技術來實現;
?? Client pull 利用HTML的META標記,結合使用"http-equiv=Refresh" 屬性來控制頁面的自動刷新,間隔時間由content的屬性決定;
?? 實現方法:
?? 在HTML的<HEAD> 與</HEAD>標簽之間加上如下語句:
?? <meta http-equiv="refresh" content="秒數; url= 跳轉的文件或地址" />
? 如:?
? 讓頁面每隔20秒自動刷新一次;
? <meta http-equiv="refresh" content="20" />
? 當用戶來到該頁面20秒后,自動跳轉至main.aspx 頁面;
? <meta http-equiv="refresh" content="20; url=main.aspx " />
<head>
????<title>測試自動跳轉頁面</title>
????<link?href="Styles/Style.css"?type="text/css"?rel="stylesheet"?/>
????<meta?http-equiv="refresh"?content="5"?/>
</head>?
五.頁面上控件的訪問
? 如: 用Label1顯示用戶打開該頁面的時間(秒)
? 5.1 編寫一段記時代碼:
Code<script>
?function?hello(number)?{
?number+=10;
?document.getElementById("Label1").style.backgroundColor????=?"yellow";
?document.getElementById("Label1").innerText=number;
?window.setTimeout("hello("+number+")",1000);??
?}
</script>
?
?? 5.2 添加在窗體加載時,自動運行該函數
Code<body?onload="hello(0)">
</body>
?
?? 5.3 innerText?, innerHtml ,outText,outHtml?的區別?
可通過下面的方法查看:
Code<script>?
?function?hello(number)?{
?number+=10;
?var?a=document.getElementById("Label1").outerHTML?;
?alert(a);?
?}
</script>
示例程序下載
?
?
總結
以上是生活随笔為你收集整理的ASP.NET 2.0 学习笔记 1: session 与 script 应用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 正则表达式分割数组
- 下一篇: asp.net ajax控件工具集 Au