此次网站开发设计主要是练习Session和Application两个对象的使用方法,下面简单介绍以下这两个对象。
一、Session对象
Session对象主要用来记载特定用户信息,用户在对页面进行访问时,ASP.NET应用程序会为每一个用户分配一个Session对象,也就是说不同的用户拥有各自不同的Session对象,就好像我们在学校刚入学的时候学校会给每个不同的学生分配一个学号一样,每个人拥有的学号都是不同的。基于这种特性,Session对象可以在网站的任意一个页面进行访问,所以常用于存储需要跨页面使用的信息。Session对象可以为每一个用户的会话存储信息,Session对象中的信息只能被用户自己使用,而不能被网站的其他用户访问,因此可以在不同的页面中共享数据,但不能在用户之间共享数据。这个就像我们的使用某些应用软件时需要注册用户一样,一个注册账号只能一个用户使用。
其基本语法如下:
- 保存对象信息:
Session["键名"]=值;
Session.Add("键名",值);
2.删除会话状态集合中的项 :
Session.Remove("键名")
3.按名称获取会话状态中的值:
变量=Session["键名"];
4.清除会话状态中的所有值:
Session.RemoveAll();
Session.Clear();
5.取消当前对话:
Session.Abandon();
6.设置会话状态的超时期限,以分钟为单位:
Session.TimeOut=数值
二、Application对象
Application对象主要用于存储网站的共享信息。但与Session对象不同的是,它里面的信息是可以被网站的所有页访问,因此可以在不同用户之间共享数据。其基本语法如下:
1,保存对象信息:
Application["键名"]=值;
Application.Add("键名",值);
2.获取对象信息:
变量名=Application["键名"];
变量名=Application.Get("键名");
3.更新对象值:
Application.Set("键名",值);
Application["键名"]=值;
网页效果及代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="实验4-1.aspx.cs" Inherits="实验4_1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>去成都</title>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
#wrap {
height: 412px;
width: 1318px;
background-image:url('1.jpeg');
background-repeat: no-repeat;
background-position: center center;
position: relative;
top: 6px;
left: -12px;
}
#head {
height: 104px;
width: 1310px;
background-color: #66CCCC;
text-align: center;
position: relative;
top: 0px;
left: 0px;
}
#foot {
width: 1324px;
height: 197px;
background-color: #CC9933;
position: relative;
top: 12px;
left: -13px;
}
#wrap .logGet {
height: 343px;
width: 368px;
position: absolute;
background-color: #FFFFFF;
top: 4%;
right: 14%;
}
.logC a button {
width: 100%;
height: 45px;
background-color: #ee7700;
border: none;
color: white;
font-size: 18px;
}
.logGet .logD.logDtip .p1 {
display: inline-block;
font-size: 28px;
margin-top: 30px;
width: 86%;
}
#wrap .logGet .logD.logDtip {
width: 86%;
border-bottom: 1px solid #ee7700;
margin-bottom: 60px;
margin-top: 0px;
margin-right: auto;
margin-left: auto;
}
.logGet .lgD img {
position: absolute;
top: 12px;
left: 8px;
}
.logGet .lgD input {
text-indent: 2.5rem;
}
#wrap .logGet .lgD {
margin: 30px auto;
width: 91%;
position: relative;
top: -9px;
left: -2px;
height: 47px;
}
#wrap .logGet .logC {
width: 86%;
margin-top: 0px;
margin-right: auto;
margin-bottom: 0px;
margin-left: auto;
}
.title {
font-family: "宋体";
color: #FFFFFF;
position: absolute;
top: 50%;
left: 47%;
transform: translate(-50%, -50%); /* 使用css3的transform来实现 */
font-size: 36px;
height: 41px;
width: 40%;
}
.title1 {
height: 111px;
width: 123px;
background-image:url('lg.jpg');
background-repeat: no-repeat;
background-position: center center;
position: relative;
top: -17px;
left: 398px;
}
.copyright {
font-family: "宋体";
color: #FFFFFF;
position: absolute;
top: 21%;
left: 623px;
transform: translate(-50%, -50%); /* 使用css3的transform来实现 */
height: 47px;
width: 49%;
text-align:center;
}
#foot .copyright .img {
width: 100%;
height: 24px;
position: relative;
}
.copyright .img .icon {
display: inline-block;
width: 24px;
height: 24px;
margin-left: 22px;
vertical-align: middle;
background-image: url(%E7%94%B5%E5%AD%90%E9%82%AE%E4%BB%B6.png);
background-repeat: no-repeat;
vertical-align: middle;
margin-right: 5px;
}
.copyright .img .icon1 {
display: inline-block;
width: 24px;
height: 24px;
margin-left: 22px;
vertical-align: middle;
background-image: url(%E5%9C%B0%E5%9D%80.png);
background-repeat: no-repeat;
vertical-align: middle;
margin-right: 5px;
}
.copyright .img .icon2 {
display: inline-block;
width: 24px;
height: 24px;
margin-left: 22px;
vertical-align: middle;
background-image: url(%E8%81%94%E7%B3%BB%E6%96%B9%E5%BC%8F.png);
background-repeat: no-repeat;
vertical-align: middle;
margin-right: 5px;
}
#foot .copyright p {
height: 24px;
width: 100%;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div class="header" id="head">
<div class="title"> 去成都旅游网站</div>
<div class="title1"></div>
</div>
<div class="wrap" id="wrap">
<div class="logGet">
<!-- 头部提示信息 -->
<div class="logD logDtip">
<p class="p1">登录</p>
</div>
<div class="lgD">
<img src="img/logName.png" width="20" height="20" alt=""/>
<asp:Label ID="Label1" runat="server" Text="用户名:"></asp:Label>
<asp:TextBox ID="txtname" runat="server" BackColor="White" BorderColor="#CCCCCC" Height="37px" Width="236px" >用户名</asp:TextBox>
</div>
<div class="lgD">
<img src="img/logPwd.png" width="20" height="20" alt=""/>
<asp:Label ID="Label2" runat="server" Text="密码:"></asp:Label>
<asp:TextBox ID="txtpwd" runat="server" TextMode="Password" BackColor="White" BorderColor="#CCCCCC" Height="34p