天天看點

ASP.NET

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:

    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);

擷取用戶端的相關資訊:

      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"]); //輸出

•    }

•}

聊天室應用:
ASP.NET

• 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"] + "位貴賓!");// 輸出你是第多少位來×××!!!

ASP.NET
ASP.NET
-- 取實體路徑:

•protected void Page_Load(object sender, EventArgs e)

•        Response.Write("傳回目前檔案所在的實體路徑:<BR>");

•        Response.Write(Server.MapPath("."));

ASP.NET
ASP.NET

//是不規則的編号

為每一位使用者配置設定一個ID:

• protected void Page_Load(object sender, EventArgs e)

•        Response.Write("您的随即編号為:"+Session.SessionID);

自定義屬性:

•        Session["h"] = "歡迎!";

•        Response.Write(Session["h"]); 

• <div> <a href=Default3.aspx>在另外一個頁面檢視</a> </div>

•以上代碼為頁面1中代碼

ASP.NET
自定義屬性 (第二步):

•        Response.Write(Session["h"]);

•以上代碼為頁面2中代碼

ASP.NET
設定有效期和使 Session 失效:

•        Session.Timeout = 1;

•        Session.Abandon();

• <div><a href=Default3.aspx>在另外一個頁面檢視</a></div>

Cookie對象:

•Cookie對象也可以儲存客戶資訊,與Session 對象相似,分别儲存不同使用者的資訊。

•和Session的差別是:Session對象所有資訊儲存在伺服器上,Cookie對象所有資訊儲存在用戶端的浏覽器上

将資訊儲存到浏覽器(第一步):

•        HttpCookie mycookie = new HttpCookie("user");

•        mycookie.Value = "asp.net入門";

•        Response.Cookies.Add(mycookie);

• <div> <a href=Default3.aspx>在另外一個頁面檢視</a> </div>

ASP.NET
ASP.NET
讀取儲存的資訊(第二步):

•        string mycook = Request.Cookies["user"].Value;

•        Response.Write(mycook);

•在新頁面中實作以上代碼

• 

• 

繼續閱讀