一.用SESSION保存登录值,如果在打开新页面时,该值为空,则自动跳转至登陆页面
1.1 登陆时保存userID 信息;
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
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](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
protected void Page_Load(object sender, EventArgs e)
{
if (this.Session["userID"] == null)
{
Response.Redirect("Login.aspx");
}
}
1.3 用户离开时,销毁userID
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/// <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](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
让最顶层的框架跳转,就是整个页面
window.top.location = "…… "
让当前页面的父框架跳转
window.parent.location = "…… "
让指定框架跳转
window.top.框架名.location = "…… "
让当前页面的父框架跳转,不弹出
<a href="#" onclick="window.parent.location='
![](https://www.cnblogs.com/Images/dot.gif)
如: mainFrom.aspx 中的html代码:
该窗体中含有两个框架,framea,frameb;
framea 加载 formA.aspx;
frameB 加载 formB.aspx;
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<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](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
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](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<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](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<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](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<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](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<body onload="hello(0)">
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
</body>
5.3 innerText , innerHtml ,outText,outHtml 的区别
可通过下面的方法查看:
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<script>
function hello(number) {
number+=10;
var a=document.getElementById("Label1").outerHTML ;
alert(a);
}
</script>