IsPostBack:
public
partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.username.Text = "";
this.userpwd.Text = "";
}
this.gb.Attributes.Add("onclick", "window.close()");
}
protected void dl_Click(object sender, EventArgs e)
{
if (username.Text == "admin" && userpwd.Text == "admin")
{
Response.Redirect("admin_index.aspx");
}
else
{
Response.Redirect("sb.aspx");
}
}
protected void qk_Click(object sender, EventArgs e)
{
this.username.Text = "";
this.userpwd.Text = "";
}
}
Request和Response:
public
partial class _Default : System.Web.UI.Page
{
protected void Button1_Click1(object sender, EventArgs e)
{
int aa = int.Parse(Request.Form["pf"].ToString());
Response.Write("
求平方的数是:"
+ aa + "
");
");
Response.Write(aa + "
的平方是:"
+ aa * aa);
}
}
获取客户端的相关信息:
public
partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string ie=Request.UserAgent;//
获取客户端浏览器
string ip = Request.UserHostAddress;//
换取客户端IP地址
string ser = Request.PhysicalApplicationPath;//
获取当前文件服务器物理路径
Response.Write(ie+"
"+ip+"
"+ser);
"+ip+"
"+ser);
}
}
----
•
public partial class Default2 : System.Web.UI.Page
•
{
•
protected void Page_Load(object sender, EventArgs e)
•
{
•
Application["hygl"] = "呵呵你来了啊难的啊";
•
Response.Write(Application["hygl"]); //输出
•
}
•
}
聊天室应用:
•
protected void Button1_Click(object sender, EventArgs e)
•
{
•
string mywords = Request["mywords "];
•
Application.Lock(); //锁定
•
Application["chat"] = Application["chat"] + "
" + mywords ;
" + mywords ;
•
Response.Write(Application["chat"]);
•
Application.UnLock(); //解锁
•
}
网页计数器:
protected void Page_Load(object sender, EventArgs e)
{
Application.Lock();
Application["count"] = Convert.ToInt32(Application["count"]) + 1; //每次加1
Application.UnLock();
Response.Write("您是本站第" + Application["count"] + "位贵宾!");// 输出你是第多少位来×××!!!
}
--
取物理路径:
•
•
protected void Page_Load(object sender, EventArgs e)
•
{
•
Response.Write("
传回当前文件所在的物理路径:
");
");
•
Response.Write(Server.MapPath("."));
•
}
为每一位用户分配一个
ID:
•
protected void Page_Load(object sender, EventArgs e)
•
{
•
Response.Write("
您的随即编号为:
"+Session.SessionID);
•
}
自定义属性:
•
protected void Page_Load(object sender, EventArgs e)
•
{
•
Session["h"] = "
欢迎!
";
•
Response.Write(Session["h"]);
•
}
•
以上代码为页面
1
中代码
自定义属性
(第二步):
•
protected void Page_Load(object sender, EventArgs e)
•
{
•
Response.Write(Session["h"]);
•
}
•
以上代码为页面
2
中代码
设置有效期和使
Session
失效:
•
protected void Page_Load(object sender, EventArgs e)
•
{
•
Session.Timeout = 1;
•
Session["h"] = "
欢迎!
";
•
Response.Write(Session["h"]);
•
Session.Abandon();
•
}
Cookie对象:
•
Cookie对象也可以保存客户信息,与Session 对象相似,分别保存不同用户的信息。
•
和Session的区别是:Session对象所有信息保存在服务器上,Cookie对象所有信息保存在客户端的浏览器上
将信息保存到浏览器(第一步):
•
protected void Page_Load(object sender, EventArgs e)
•
{
•
HttpCookie mycookie = new HttpCookie("user");
•
mycookie.Value = "asp.net入门";
•
Response.Cookies.Add(mycookie);
•
}
读取保存的信息(第二步):
•
protected void Page_Load(object sender, EventArgs e)
•
{
•
string mycook = Request.Cookies["user"].Value;
•
Response.Write(mycook);
•
}
•
在新页面中实现以上代码
•
•