Session
| Application | ViewState | Cookie
| Hidden field | Query String | Cache
Server Side Technique
: Session , Application ( use system resource)
Client Side Technique: ViewState , Cookie,
Hidden Field , Cookie ( browswer )
Server.Transfer(" b.aspx" , TRUE);
// perserve control value, retrieve using Resquest.Form["txtID"]
in b.aspx
Session
( session value stored in ASP.NET process)
-Session state, by default , is enabled in ASP.NET
EnableSessionState=True
-SessionID values
are stored in a cookie on client side. Server uses ID to identify
repeat visit of user..
-Session value can be retrieved acoss all pages in .aspx // Session["name"].ToString();
-Session timeout is
specified in Session_Start()
global.asax
* session_start() , timeout=2 //2 minutes expired if NO refresh
or request from client
-Any type of value can be assign to Session variable
eg.
Session["FirstName"] = FirstNameTextBox.Text;
Session["LastName"] = objdt;
eg
-session variable must be cast upon retrieval.
DataSet stockPicks
= (DataSet)Session["StockPicks"];
Session["StockPicks"] = stockPicks; //write back modified
sotckpicks
pls: "page1.aspx?ar="
+ Session["username"] + "&ps="
+ Session["password"] )
Back
|
ViewState
(Not across page) More
-used to maintain state of non-postback-controls
(label) upon postback , NOT Across page.
(postback control : textbox,checkbox, GV)
-When page executed, all non-postback controls value are formated
into encoded-string, stored in hidden field . _ViewState
-when postback occurs, ASP.NET decode string, restore value at
page init.
** Disable VS if app contain complex control
such as GV, Datalist...
eg <% page EnableViewState="false" %> | <asp:gridview
EnableViewState="false" %>
Good
-it stores structured data, like dataset.
-Act like hidden field & cookie (store simple value)
Bad
-Its value can't be retrieved (invalid) across page.
-HTML file increase size, takes longer to load
eg. ViewState["name"]=objds
Back
|
Cookie
-A piece key-value pair value stored on client computer
-If expire time NOT SET
-cookie is destroyed when browser is closed
-If expire time IS SET
-cookie persist in Client PC for certain exp-time
Create cookie
HttpCookie ck=new HttpCookie("name");
ck.Value=txtname.Text;
TimeSpan tsMinute = new TimeSpan(0, 0, 10, 0); (
days, hours, min, sec )
ck.Expires=DateTime.Now+tsMinute;
Response.Cookies.Add(ck); // add to response cookie collection
Retrieve cookie
Request.Cookies["name"].Value
Back
|
Query String
-append state info on URL
-use key-value pair.
-www.myurl.com?find.aspx?Name=John?City=Memphis
-retrieve : Request.QueryString["City"] ;
Disadvantage
-Insecure, all info exposed on URL.
-limited length of query string.
-Can't stored structured data.
Back
|
Cache
What cache
-store info in highspeed storage for later retrival.
-use memory resouce.
-hold any type of data.
-Across page ( across function)
Why caching?
-Avoid the same frequently visited page to reload everytime being
requested.
When caching?
-This page is frequently accessed.
-Page output remain the same at most request.
OutPut Caching (entire page stored
in cache 15 secs, NOT variable)
- <%@ OutputCache Duration="15" VaryByParam="ddl" %>
- Once ddl changed value, entire page reset in cache for another 15 secs again.
Data Caching
- Cache["key"]=value ;
- Cache.Insert("customer", objds, ...) /* set exp time
*/
- Cache.Add(objds);
- cache.Remove();
- cache["key"].ToString();
private sub LoadData()
{ if (Cache["GridData"] == null)
{
ds.ReadXml(Server.MapPath("File.xml"));
Cache.Insert("GridData" , ds, null, DateTime.Now.AddSeconds(15), null);
// After 15 seconds in cache, ds is automatically removed.
}
else
{
ds=(Dataset)Cache["GridData"];
} gv.Datasource=ds;
gv.DataBind();
}
|
|