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 + "<br>");
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+"<br>"+ip+"<br>"+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"] + "<br>" +
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("
傳回當前文件所在的物理路徑:
<BR>");
•
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"]);
•
}
•
<div>
<a href=Default3.aspx>
在另一個頁面查看
</a>
</div>
•
以上代碼爲頁面
1
中代碼
自定義屬性
(第二步):
•
protected void Page_Load(object sender, EventArgs e)
•
{
•
Response.Write(Session["h"]);
•
}
•
以上代碼爲頁面
2
中代碼
•
protected void Page_Load(object sender, EventArgs e)
•
{
•
Session.Timeout = 1;
•
Session["h"] = "
歡迎!
";
•
Response.Write(Session["h"]);
•
Session.Abandon();
•
}
•
<div><a href=Default3.aspx>
在另一個頁面查看
</a></div>
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);
•
}
•
<div> <a href=Default3.aspx>在另一個頁面查看</a> </div>
讀取保存的信息(第二步):
•
protected void Page_Load(object sender, EventArgs e)
•
{
•
string mycook = Request.Cookies["user"].Value;
•
Response.Write(mycook);
•
}
•
在新頁面中實現以上代碼