Professional Documents
Culture Documents
Sách Uml
Sách Uml
CC LP S DNG
TRONG ASP.NET
L thuyt : 3 tit
Thc hnh : 3 tit
Lp Http Request
Request l mt th hin ca lp
HttpRequest, Browser dng Request
object gi thng tin cn thit ti Server
Data
Lp Http Request
Lp Http Request
Cc thuc tnh
Lp Http Request
1.
Lp Http Request
V d:
txtUser
txtPwd
Default.aspx
Lp Http Request
V d (tt):
Ti trang CheckUser.aspx, ly gi tr ca UserName
v Password trn form
<b>UserName:</b>
<%=Request.Form["txtUser"]%><br />
<b>Password</b>
<%=Request.Form["txtPwd"]%>
Lp Http Request
Lp Http Request
Keys[i]: tr v tn ca phn t ti v tr
th i, trong i c gi tr t 0 n n-1
vi n=Request.Form.Count
C php:
Varfield=Request.Form.Keys[i];
Lp Http Request
V d:
<% int i, num;
string varField, varName;
num = Request.Form.Count;
for (i = 0; i < num; i++)
{
varField = Request.Form.Keys[i];
varName = Request.Form[varField];
}
%>
10
Lp Http Request
2.
Tp hp QueryString
(QueryStringCollection)
Request.QueryString dng ly d liu
c gi t client n server bng phng
thc GET hoc truyn d liu c cha tag
lin kt <a> Hyperlink.
QueryString c nh ngha l chui nm
sau du ? trong chui URL (Uniform
Resource Locator) trn phn Address ca
trnh duyt.
11
Lp Http Request
C 3 cch truyn tham s v gi file x l
Cch 1 : dng action , tham s truyn qua
phn t ca form
<Form method=Get
action=FileName.aspx>
Cc phn t ca form
</Form>
12
Lp Http Request
Cch 2 : dng action , tham s ghi sau
du ?
<Form
<Form method=Get
method=Get or
or Post
Post
action=FileName.aspx?
action=FileName.aspx?
var1=value1&var2=value2&>
var1=value1&var2=value2&>
Cc
Cc phn
phn t
t ca
ca form
form
</Form>
</Form>
13
Lp Http Request
Cch 3 : dng chui link , tham s ghi sau
du ?
<a
<a href=FileName.aspx?
href=FileName.aspx?
var1=value1&var2=value2&>
var1=value1&var2=value2&>
Text
Text hoc
hoc image
image
</a>
</a>
Lp Http Request
Nu tham s khng c gi tr th gi tr
tr v ca tham s khi s dng
Request.QueryString l NULL.
15
Lp Http Request
16
Lp Http Request
Cc thuc tnh ca
QueryStringCollection:
Count: tr v s phn t ca form chuyn
t client n server
C php:
Numelement=Request.QueryString.Cou
nt;
17
Lp Http Request
Keys[i]: tr v tn ca phn t ti v tr
th i, trong i c gi tr t 0 n n-1 vi
n=Request.QueryString.Count
C php:
Varfield=Request.QueryString.Keys
[i];
V d:
18
Lp Http Request
txtUser
txtPwd
Lp Http Request
Lp Http Request
<a href=ReadNews.aspx?
typeNews=edu&NewsID=98708>
Scholaship of IBM
</a>
Lp Http Request
3.
Tp hp Cookies (CookiesCollection)
Cookies l mt mu thng tin nh dng lu
tr thng tin ca ngi dng trn my client
Cookies c 2 dng:
Cookies[varcookies].Value: tr v gi
tr cha bin cookies l varcookies (1 chiu)
Cookies[varcookies]
[Subvarcookies]: tr v gi tr cha bin
cookies l subvarcookies (nhiu chiu)
Cookies[varcookies]
[Subvarcookies][hhhhhh]:
22
Lp Http Request
Thuc tnh ca Cookies:
23
Lp Http Request
Keys[i]: tr v tn ca phn t ti v tr
th i, trong i c gi tr t 0 n n-1 vi
n=Request.Cookies.Count
C php:
Varfield=Request.Cookies.Keys[
i];
24
Lp Http Request
V d:
<% int i, num;
string varField, varName;
num = Request.Cookies.Count;
for (i = 0; i < num; i++)
{
varField = Request.Cookies.Keys[i];
varName = Request.Cookies.
[varField].Value;
}
%>
25
Lp Http Response
Http Response c s dng truyn
d liu webserver n webBrowser
Data
26
Lp Http Response
Cc thuc tnh
27
Lp Http Response
C php:
Response.Buffer=true/false;
28
Lp Http Response
29
Lp Http Response
Xa cookies
Response.Cookies.Clear()
;
30
Lp Http Response
Lp Http Response
// c Cookies
v = Request.Cookies[Infor].value ;
// result :v=abc,xyz,123
// hay dng
v1 = Request.Cookies[Infor] [Us];//abc
v2 = Request.Cookies[Infor] [Pwd];//xyz
v3 = Request.Cookies[Infor]
[Author];//123
32
Lp Http Response
Cc phng thc
Clear(): Xa vng tm
C php:
Response.Clear()
Lp Http Response
V d:
<%
int i;
Response.Buffer = true;
for (i = 1; i < 10000; i++)
{%>
<%=i%>
<%if (i % 10000 == 0)
Response.Flush();%>
<%}%>
34
Lp Http Response
Lp Http Response
V d:
Response.Redirect(T1.aspx);
m=m+1;
Try
{
Response.Redirect(T1.aspx, false);
m=m+1;
}
Catch
{
Response.Redirect(T2.aspx);
}
36
Lp Http Response
<% = strname
hoc
%>
Response.Write(strname)
37
Lp Http Server
i tng Server cung cp cc phng thc
gip chuyn iu khin gia cc trang vi
nhau, ly thng tin v m li, encode,
Cc thuc tnh
MachineName: tn server
ScriptTimeout: thit lp thgian x l ti
a 1 file
V d:
Server.ScriptTimeout=100s //thit lp thgian
mc nh
38
Lp Http Server
Cc phng thc
MapPath(URL): nh x ng dn o
thnh ng dn vt l cho mt tp tin trn
Server
C php:
Server.MapPath(path)
Lp Http Server
40
Lp Http Server
Lp Http Server
HtmlDecode(strcontent):gii m cc th
HTML
V d:
Str=n>5
Str=server.HtmlDecode(str); // kt qu
n>5
42
Lp Http Server
Lp Http Server
44
Lp Http Server
45
Lp Http Server
C php
Server.CreateObject(progid)
Lp Http Server
V d:
To i tng ADODB.Connection, ADODB.Recordset,
MSWC.AdRotator, ...
<%
Set myAd=Server.CreateObject(MSWC.AdRotator)
Set myCon=Server.CreateObject(ADODB.Connection)
Set myCon=Server.CreateObject(ADODB.Recordset)
%>`
47
Lp Http Server
C php
Server.Execute(path)
Session
Session
Khi to:
Session[SessionName]=va
lue
Ly gi tr t bin Session
varName=Convert.ToString(Session[SessionN
ame])
Session[SessionName]=Session[SessionNa
me] +value + @
@: dng tch cc gi tr (c th chn ty )
50
Session
Thuc tnh:
Timeout: Qui nh khong thi gian (tnh
bng pht) Web Server duy tr Session nu
ngi dng khng gi yu cu no v li
Server.
Gi tr mc nh ca Timeout l 20. Nu
khng c yu cu no k t ln yu cu sau
cng mt khong thi gian l <Timeout>
pht, i tng Session m Web server cp
cho ln lm vic s t ng c gii
phng.
51
Session
Nhng yu cu sau , web server coi nh
l mt ngi dng mi, v s cp mt i
tng Session mi
V d:
Session.TimeOut = 100 //Thit lp thi gian
cho 1 phin lm vic ca user l 100 pht
C th thit lp thuc tnh ny trong tp tin
Web.config trong <system.web>
<sessionState timeout=100" />
52
Session
53
Session
Phng thc
Abandon(): gii phng vng nh c
dng duy tr i tng Session trn
Web Server ngay khi c gi thc hin.
Nhng yu cu sau c Web server
coi nh l mt ngi dng mi.
Remove(SessionName): xo d liu
trn bin VarSession
RemoveAll(): Xo d liu, nhng
sessionID vn tn ti
54
Session
V d: To chc nng ng nhp v thot
T Menu chn Website Add new item
Web User Control, t tn l Login.aspx
Thit k giao din nh sau:
55
Session
Khi to v Hu thng tin ng nhp ca ngi dng
trong Session
void Session_Start(object sender, EventArgs e)
{
// Tng s khch ving khi c 1 phin lm vic ca user
Application["SoKhachVieng"] =
(int)Application["SoKhachVieng"] + 1;
// Thit lp thi gian TimeOut cho mi phin l 30 pht
Session.Timeout = 30;
// t trng thi ban u ca user l cha ng nhp
Session["LOGIN_OK"] = false;
}
56
Session
protected bool Authority(string user, string
pwd)
{
if (user == "aaa" && pwd == "1234")
{
return true;
}
return false;
}
58
Application
Application
S kin: global.asax
Application_Start: Khi ng dng web
ln u tin c gi
Application_End: Khi ng dng web
kt thc.
60
Application
Khi to:
i tng Application c to khi client
yu cu bt k trang no trong ng dng
Application[TenBien] = Value ;
61
Application
Ly gi tr:
string s = (string)
Application[chuoi] ;
int count = (int)
Application[count] ;
62
Application
V d: Khi to v lu thng tin S ngi
duyt website khi website c kch hot ln
u tin
void Application_Start(object sender,
EventArgs e)
{
Application["SoKhachVieng"] = 0;
// Khi to s khch ving ban u l 0
}
63
Application
64
Application
V d :Ly thng tin s khch ving website
v hin th ra mn hnh
M trang Index.aspx ch View Code, X
l trong hm Page_Load nh sau:
protected void Page_Load(object sender,
EventArgs e)
{
lbtCount.Text = "S khch ving thm :" +
Application["SoKhachVieng"].ToString();
}
65