1记录登陆人员个数
2完成聊天室(具体要求见课件)
aspx:
#ltmessage
{
height: 189px;
}
<div>
<table>
<tr><td colspan="4">在线聊天:人</td></tr>
<tr><td colspan="4"><div id="ltmessage" runat="server"></div></td></tr>
<tr><td colspan="3">请输入你的姓名:</td><td></td></tr>
<tr><td>请选择表情:</td>
<td>
悲伤
晕
高兴
</td></tr>
<tr><td colspan="3">请输入你的消息:</td><td>
</td></tr>
<tr><td colspan="3"></td><td>
</td></tr>
</table>
</div>
aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace _10_25
{
public partial class 聊天室 : System.Web.UI.Page
{
Dictionary img;
protected void Page_Load(object sender, EventArgs e)
{
img= new Dictionary();
if (!IsPostBack)
{
ltmessage.InnerHtml = Application["ms"].ToString() + "";
}
}
protected void Button1_Click(object sender, EventArgs e)
{
switch (DropDownList1.SelectedValue)
{
case "高兴":
img["图片"] = "<img src="img/高兴.gif/" width="20px">";
break;
case "悲伤":
img["图片"] = "<img src="img/悲伤.gif/" width="20px">";
break;
case "晕":
img["图片"] = "<img src="img/晕.gif/" width="20px">";
break;
}
Application.Lock();
Application["ms"] = Application["ms"] + "" + name.Text + ":" + message.Text + img["图片"];
Application.UnLock();
string msg = Application["ms"].ToString();
ltmessage.InnerHtml = msg;
if (msg.Length >= 500)
{
Application["ms"] = "";
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void Button2_Click(object sender, EventArgs e)
{
ltmessage.InnerHtml = "";
}
}
}
global:
using System;
using System.Collections.Generic;
1Cookie实习登陆次数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace _10月10号作业.Cookies
{
public partial class CookieDemot3 : System.Web.UI.Page
{
public static int number;这里这个number就可以把登陆次数记录下来,不需要使用cookie了。
protected void Page_Load(object sender, EventArgs e)
{
//HttpCookie NumCookie = new HttpCookie("IntVisit",number.ToString());
//Response.SetCookie(NumCookie);
//number =Convert.ToInt32(Request.Cookies["IntVisit"].Value);
if (number == 0)
{
number = 1;
}
else
{
number = number + 1;
}
Response.Write("你是第"+number.ToString()+"次,访问本站");
//Response.SetCookie(new HttpCookie("IntVisit", number.ToString()));
//HttpCookie NumCookie = new HttpCookie("IntVisit",number.ToString());
//Response.SetCookie(NumCookie);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace _10月10号作业.Cookies
{
public partial class CookieDemot : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Response.SetCookie(new HttpCookie("Color",TextBox1.Text));
string ip = Request.UserHostAddress;//获取访问主机的ip
int number = 1;
//想让number来进行运算,每登陆一次就让number加1-----加1后结果报存起来,下次再这个结果上再加1
//每次把number的结果放在一个cookie中如Cookie["IntVisit"]=number; request.Cookies["IntVisit"]
if (Request.Cookies["IntVisit"] == null)//第一次访问网页时,key值为IntVisit的cookie还不存在。如果cookies中没有此ip
{
HttpCookie cookie = new HttpCookie("IntVisit", number.ToString());//不存在就声明一个key为IntVisit的cookie
cookie.Expires = DateTime.Now.AddYears(1);//设置cookie的有效时间
Response.Cookies.Add(cookie);//设置一个可以保存在浏览器硬盘中的cookie
}
else
{
HttpCookie cookie = Request.Cookies["IntVisit"];
number = Convert.ToInt32(cookie.Value);//获取当前ip的访问次数
number++;//次数加1
cookie.Value = number.ToString();
cookie.Expires = DateTime.Now.AddYears(1);
Response.SetCookie(cookie);
}
Response.Write("您的ip为:" + ip + "访问次数为:" + number);
Response.End();
}
}
}
2session控制必须登陆
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void TextBox2_TextChanged(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text == "admin" && TextBox2.Text == "123")
{
Session["login"] = true;
Response.Redirect("hello.aspx");
}
else
{
Response.Write("用户名和密码不正确");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class hello : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!(bool)Session["login"])
{
Response.Redirect("Login.aspx");
}
Response.Write(Request["name"]);
}
protected void Page_UnLoad(object sender, EventArgs e)
{
Session["login"] = false;
}
}
3验证码的session应用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class sessionYZM : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text == Session["Code"].ToString())
{
Response.Write("正确");
}
else
{
Response.Write("不正确");
}
}
}
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
namespace _10_25
{
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
Application["count"] = 0;
Application["ms"] = "";
}
protected void Session_Start(object sender, EventArgs e)
{
Application["count"] = Convert.ToInt32(Application["count"]) + 1;
}
protected void Application_BeginRequest(object sender, EventArgs e)
{
}
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
}
protected void Application_Error(object sender, EventArgs e)
{
}
protected void Session_End(object sender, EventArgs e)
{
Application["count"] = Convert.ToInt32(Application["count"]) - 1;
}
protected void Application_End(object sender, EventArgs e)
{
}
}
}