ASP.NET 2.0 学习笔记 1: session 与 script 应用

一.用SESSION保存登录值,如果在打开新页面时,该值为空,则自动跳转至登陆页面

   1.1 登陆时保存userID 信息;

ContractedBlock.gif ExpandedBlockStart.gif Code
public 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信息;

ContractedBlock.gif ExpandedBlockStart.gif Code
protected void Page_Load(object sender, EventArgs e)
    {
        
if (this.Session["userID"== null)
        {
            Response.Redirect(
"Login.aspx");
        }
    }

 

   1.3 用户离开时,销毁userID

ContractedBlock.gif ExpandedBlockStart.gif 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:
   =========================
   测试前提:一个页面中含有两个或两个以上框架;

ContractedBlock.gif ExpandedBlockStart.gif Code
  让最顶层的框架跳转,就是整个页面 
  window.top.location   =   "…… " 
  让当前页面的父框架跳转 
  window.parent.location   =   "…… " 
  让指定框架跳转 
  window.top.框架名.location   =   "…… " 
  让当前页面的父框架跳转,不弹出 
  
<href="#" onclick="window.parent.location='?..'>xxx</a>

 
    如: mainFrom.aspx 中的html代码:
    该窗体中含有两个框架,framea,frameb;
    framea 加载 formA.aspx;
    frameB 加载 formB.aspx;

ContractedBlock.gif ExpandedBlockStart.gif Code
<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>

 


二.关闭当前窗口

ContractedBlock.gif ExpandedBlockStart.gif Code
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 脚本实现:

ContractedBlock.gif ExpandedBlockStart.gif Code
<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 " />

ContractedBlock.gif ExpandedBlockStart.gif Code
<head>
    
<title>测试自动跳转页面</title>
    
<link href="Styles/Style.css" type="text/css" rel="stylesheet" />
    
<meta http-equiv="refresh" content="5" />
</head> 


五.页面上控件的访问

  如: 用Label1显示用户打开该页面的时间(秒)

  5.1 编写一段记时代码:

ContractedBlock.gif ExpandedBlockStart.gif 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 添加在窗体加载时,自动运行该函数

ContractedBlock.gif ExpandedBlockStart.gif Code
<body onload="hello(0)">

</body>

 

   5.3 innerText , innerHtml ,outText,outHtml 的区别 

可通过下面的方法查看:

ContractedBlock.gif ExpandedBlockStart.gif Code
<script> 
 
function hello(number) {
 number
+=10;
 
var a=document.getElementById("Label1").outerHTML ;
 alert(a); 
 }
</script>

示例程序下载

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值