Bài giảng Phát triển web nâng cao - Bài 2: Các lớp sử dụng trong ASP.NET

52 9 0
Bài giảng Phát triển web nâng cao - Bài 2: Các lớp sử dụng trong ASP.NET

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Bài giảng Phát triển web nâng cao - Bài 2: Các lớp sử dụng trong ASP.NET được biên soạn nhằm trang bị cho các bạn những kiến thức về các lớp sử dụng trong ASP.NET như Request, Response, Cookies, Server, Session, Application.

Bài 2: Các lớp sử dụng ASP.NET 2.1 2.2 2.3 2.4 2.5 2.6 2.7 Request Response Cookies Server Session Application Chu trình sống trang web 2.1 Lớp Http Request   Khái niệm Các thuộc tính Khái niệm  Request đối tượng ASP.NET, cho phép đọc thơng tin trang khác gửi (Submit) đến Các thuộc tính     RequestType Request.Form Request.QueryString Cookies RequestType  Trả phương thức truyền liệu từ Client đến Server Gồm phương thức POST GET Namespace: System.Web Assembly: System.Web (in System.Web.dll) Type Value: System.String  Cú pháp:     public string RequestType { get; set; } Request.Form  Form collection sử dụng để tập hợp liệu chứa phần tử form chuyển từ Client đến Server phương thức POST  Cú pháp: Request.Form( element )[(index)|.Count]; Request.Form.Keys[i]; Ví dụ - Request.Form( element ) Default.aspx UserName: Password: CheckUser.aspx UserName: Password:  Request.Form.Count;   Trả số phần tử form chuyển từ Client đến Server Request.Form.Keys[i];   Trả tên phần tử vị trí thứ i, i có giá trị từ đến n-1 với n=Request.Form.Count 10 Thực    Thiết kế Login.aspx :  Trang Login.aspx  Trang Login.aspx.cs Thiết kế Home.aspx :  Thiết kế Home.aspx  Thiết kế Home.aspx.cs Thiết kế Golbal.asax : 38 Trang Login.aspx  Trang Login.aspx.cs Login  public partial class Login : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) User name: { } Password: protected void cmdLogin_Click(object sender, EventArgs e) { if (txtUserID.Text == "asp.net" && txtPassword.Text == "123456") { Session.Contents["TrangThai"] = "DaDangNhap"; Response.Redirect("Home.aspx"); } } } 39 Thiết kế Home.aspx  Thiết kế Home.aspx.cs Home  public partial class Home : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (Session.Contents["trangthai"].ToString()=="chuadangnhap") bạn đăng nhập thành công { Response.Redirect ("Login.aspx"); } } } 40 Thiết kế Golbal.asax void Application_Start(object sender, EventArgs e) { // Code that runs on application startup } void Application_End(object sender, EventArgs e) { // Code that runs on application shutdown } void Application_Error(object sender, EventArgs e) { // Code that runs when an unhandled error occurs } void Session_Start(object sender, EventArgs e) { // Code that runs when a new session is started Session.Add("TrangThai", "chuadangnhap"); } void Session_End(object sender, EventArgs e) { // Code that runs when a session ends // Note: The Session_End event is raised only when the sessionstate mode // is set to InProc in the Web.config file If session mode is set to StateServer // or SQLServer, the event is not raised } 41 2.6 Application   Khái niệm Thuộc tính 42 Khái niệm   Đối tượng Application sử dụng để quản lý tất thông tin ứng dụng web Thông tin lưu trữ đối tượng Application xử lý trang aspx suốt chu kỳ sống ứng dụng 43  Tạo biến Application: Application.Add(“Tên_Biến”, );  Truy xuất đến biến Application: Application.Contents[“Tên_Biến”] ; số: Application.Contents[i]; string s = (string) Application[“chuoi”] ; int count = (int) Application[“count”] ; 44  Ngoài ra, đối tượng Application cịn có phương thức thường dùng :    Application.Lock(): Để khóa khơng cho người khác sửa đổi biến tồn cục Application.UnLock() để mở khóa Đối tượng Application có kiện :    Application_OnStart : kích hoạt lần yêu cầu phát sinh Application_OnEND : kích hoạt dịch vụ web dừng (unload) Mã lệnh viết cho kiện đặt file Global.asax 45 Ví dụ:  Đếm số lượng khách truy cập website  Giải quyết:   Tạo website index.aspx Sử dụng Application 46 Index.aspx Index Chào mừng bạn đến website chúng tôi 47 Index.aspx.cs public partial class Index : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { lblSLKhach.Text = "Bạn vị khách thứ : " + Application.Contents["SLTruyCap"].ToString(); } } 48 Golbal.asax void Application_Start(object sender, EventArgs e) { // Code that runs on application startup //Tạo biến Applciation SLTruyCap khởi tạo giá trị Application.Add("SLTruyCap", 0); } void Session_Start(object sender, EventArgs e) { // Code that runs when a new session is started //Tăng số lượng người truy cập lên có người thăm Application.Contents["SLTruyCap"] = int.Parse(Application.Contents["SLTruyCap"].ToString()) + 1; } 49 2.7 Chu trình sống trang web   Chu kỳ sống bắt đầu trình duyệt yêu cầu trang web gọi Session Chu kỳ sống tiếp tục nếu:      Session hoạt động Người sử dụng tương tác với giao diện web kích hoạt kiện Dữ liệu trang (View State) wed gửi cho Server Server nhận View State trả lại yêu cầu từ View State Chu kỳ sống kết thúc khi: Người dùng kết thúc trình duyệt  Session kết thúc (timeout) Mỗi người dùng duyệt web có kiện ứng dụng sau:  Application_Start: Người dùng duyệt trang web Application_End: Khi khơng cịn người dùng duyệt trang web  Application_Error: Khi có lỗi xảy ứng dụng  Session_Start: Khi người dùng duyệt trang web   Session_End: Khi người dùng đóng trình duyệt Session kết thúc (time out)   50 file Global.asax void Application_Start(object sender, EventArgs e) { //Kiểm tra chưa tồn file tạo file Count_Visited.txt if (!File.Exists(Server.MapPath("Count_Visited.txt"))) File.WriteAllText(Server.MapPath("Count_Visited.txt"), "0"); Application["DaTruyCap"] =int.Parse(File.ReadAllText(Server.MapPath("Count_Visited.txt"))); } void Application_End(object sender, EventArgs e) { } void Application_Error(object sender, EventArgs e) { } void Session_Start(object sender, EventArgs e) { // Tăng số truy cập lên có khách truy cập if (Application["DangTruyCap"] == null) Application["DangTruyCap"] = 1; else Application["DangTruyCap"] = (int)Application["DangTruyCap"] + 1; // Tăng số truy cập lên có khách truy cập Application["DaTruyCap"] = (int)Application["DaTruyCap"] + 1; File.WriteAllText(Server.MapPath("Count_Visited.txt"), Application["DaTruyCap"].ToString()); } void Session_End(object sender, EventArgs e) { //Khi hết session người dùng thoát khỏi website giảm số người truy cập Application["DangTruyCap"] = (int)Application["DangTruyCap"] - 1; } 51 Cảm ơn 52 ... từ đến n-1 Với n=Request.QueryString.Count 19 2.2 Lớp Http Response    Khái niệm Các thuộc tính Các phương thức 20 Khái niệm Http Response sử dụng để truyền liệu từ webserver đến webBrowser... boolean); • • Dùng để ghi liệu web; Cú pháp: Response.Write(strname); Hoặc 2.3 Lớp HttpCookies    Được sử dụng để ghi Cookies Cookies asp.Net lớp Httpcookies Ví dụ:  Khởi tạo... Namespace: System .Web Assembly: System .Web (in System .Web. dll) Type Value: System.String  Cú pháp:     public string RequestType { get; set; } Request.Form  Form collection sử dụng để tập hợp

Ngày đăng: 08/05/2021, 12:30

Tài liệu cùng người dùng

Tài liệu liên quan