Unit-4 - ASP - Net and Web Development - Part-2
Unit-4 - ASP - Net and Web Development - Part-2
Unit-4 - ASP - Net and Web Development - Part-2
I. Write :
• It’s like cout or printf.
• Basically it prints data on the screen.
Example :
protected void Page_Load(object sender , EventArgs e)
{
Response.Write(“Hello”);
Response.Write(“<hr>”);
}
ASP.NET Inbuilt Object
II. Redirect :
<asp:Button ID=“btnjump” runat=“server” Text=“Jump to”
onclick=“btnjump_click” />
Example :
protected void btnjump_click(object sender , EventArgs e)
{
Response.Redirect(“frmAdo1.aspx”);
}
ASP.NET Inbuilt Object
2. Request Object
• Provides access to the current page request, including the request headers,
cookies, query string and so on.
3. Server Object
• Exposes utility methods that you can use to transfer control between pages,
get information about the most recent error, encode and decode HTML text
and more.
Server.Transfer(“frmQry2.aspx”);
• It’s faster then Response.redirect and can’t be used to jump at any other
website.
Server.MachineName
• Return Machine Name.
ASP.NET Inbuilt Object
Server.MapPath(“Foldername”);
• Return path of the server.
Example
txtPath.Text = (Server.MapPath(“Img”))
returns D:\slnProject\Img
4. Context Object
• Provides access to the entire current context.
• You can use this class to share information between pages.
ASP.NET Inbuilt Object
5. Application Object
• Provides access to application-wide methods and events for all sessions.
6. Session Object
• Provides information to the current user session.
7. Trace Object
• Provides a way to display both the system and custom trace diagnostic
messages in the HTTP page output.
Preserving State in Web Application
• Generally, web applications are based on stateless HTTP protocol which
does not retain any information about user requests.
• In typical client and server communication using HTTP protocol, page is
created each time the page is requested.
• Web pages are stateless and are HTTP-based, a new instance of the page is
returned and after each roundtrip the page is destroyed.
• A new instance of the Web page class is created each time the page is
posted to the server.
• Developer is forced to implement various state management technique
when developing applications which provide customized content and
which “remembers” the user.
Types of State Management
• State management options can be divided into two categories :
• Client-Side management
• Server-Side management
View State
Query String
View State (Page level)
• Page level means it stores the page-level state of a Webpage.
• This is the default method that the page uses to preserve page and control
property values between round trip.
• View state can be used to store state information for a single user.
• We can store page specific information.
• View state is a built in feature in web controls to persist data between page
post backs.
• Each web page and the controls on the page have the EnableViewState
property.
• By default, EnableViewState property is set to true.
• ASP.NET framework uses the ViewState property to automatically save the
values of the web page and each control on the web page prior to rendering
the page.
View State (Page level)
• We can also disable View State for the entire page by adding
EnableViewState=false to @page directive.
• When page is created on web server this hidden control populate the state
of the controls and when page is posted back to server this information is
retrieved and assigned to controls.
• When the page is posted back to the server, the page parses the view-state
string at page initialization and restores property information in the page.
• We can store values in ViewState as well.
Syntax :
Store value
Viewstate[“KeyName”] = value
Example
Viewstate[“Age”] = 22
Fetch Value
Variable Name = Viewstate[“KeyName”]
Example
Ans = Viewstate[“Age”].ToString();
View State (Page level)
public partial class example : System.Web.UI
{
protected void btnStore_Click(object sender , EventArgs e)
{
Viewstate[“no”] = txtNo1.Text;
}
protected void btnTransfer_Click(object sender , EventArgs e)
{
txtNo2.Text = Viewstate[“no”].ToString();
}
}
Cookies
• A cookie is a small amount of data that is stored either in a text file on the
client system or in-memory in the client browser session.
• Cookies are handy when a particular user needs a specific piece of data,
and it needs to be persisted for a variable period of time.
• Cookie is just one or more pieces of information stored as text strings.
• Cookies can either be temporary or persistent.
• The cookie contains information the Web application can read whenever
the user visits the site.
• Each cookie must have a unique name.
• We can set a cookie’s date and time.
• If we do not set the cookie’s expiration, the cookie is created but it is not
stored on the user’s hard disk.
Cookies
Example
• A site conducting a poll might use a cookie simply as a Boolean value to
indicate whether a user’s browser has already participated in voting so that
the user cannot vote twice.
• You might have noticed the “Remember me next” time option in most of
the websites.
• Cookies are often used for personalization, where content is customized for
a known user.
• Syntax :
Store Value
HttpCookie ckname = new HttpCookie(“stroreckName”);
Ckname.Values[“KeyName”] = value
Ckname.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(ckname);
Cookies
• Example :
HttpCookie tmpPoll = new HttpCookie(“userPoll”);
tmpPoll.Values[“Given”] = 1
tmpPoll.Expires = DateTime.Now.AddYears(1);
Response.Cookies.Add(tmpPoll);
• Fetch Value :
Variable Name =
Request.Cookies[“storeckname”].Values[“KeyName”].ToString();
• Example :
Ans = Request.Cookies[“userPoll”].Values[“Given”].ToString();
Cookies
protected void btnStore_Click(object sender , EventArgs e)
{
HttpCookie tmpCook = new HttpCookie(“Visituser”);
tmpCook.Values[“Name”] = txtUname.Text;
tmpCook.Values[“Pwd”] = txtPwd.Text;
tmpCook.Values[“Dt”] = DateTime.Now.ToShortDateString();
tmpCook.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(tmpCook);
}
Cookies
protected void btnFetch_Click(object sender , EventArgs e)
{
txtFUname.Text = Request.Cookies[“Visituser”].Values[“Name”].ToString();
txtFPwd.Text = Request.Cookies[“Visituser”].Values[“Pwd”].ToString();
txtExpDate.Text = Request.Cookies[“Visituser”].Values[“Dt”].ToString();
}
QueryString
• A query string is information that is appended to the end of a page URL.
• It is like GET method of PHP.
• Query strings are usually used to send information from one page to
another page. They are passed along with URL in clear text.
• This information is passed in URL of the request.
• A typical URL with a query string look like :
http://www.gtu.ac.in/result.aspx?category=BE&semester=6
• In the URL path above, the query string starts with a question mark (?) and
includes two attribute/value pair, one called “category” and other called
“semester”.
• We can only pass smaller amounts of data using query strings.
• Most browser impose a limit of 255 characters on URL length.
QueryString
Syntax :