0% found this document useful (0 votes)
20 views7 pages

Web Technology Asp - Net Unit V

Uploaded by

bharathi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views7 pages

Web Technology Asp - Net Unit V

Uploaded by

bharathi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Request Object

When a browser asks for a page from a server, it is called a request. The Request object is used
to get information from a visitor. Its collections, properties, and methods are described below:

Collections

Collection Description
ClientCertificate Contains all the field values stored in the client certificate
Cookies Contains all the cookie values sent in a HTTP request
Contains all the form (input) values from a form that uses the post
Form
method
QueryString Contains all the variable values in a HTTP query string
ServerVariables Contains all the server variable values

Properties

Property Description
Returns the total number of bytes the client sent in the body of the
TotalBytes
request

Methods

Method Description
Retrieves the data sent to the server from the client as part of a post
BinaryRead
request and stores it in a safe array

Response Object
The ASP Response object is used to send output to the user from the server. Its collections,
properties, and methods are described below:

Collections

Collection Description
Sets a cookie value. If the cookie does not exist, it will be created, and
Cookies
take the value that is specified

Properties

Property Description
Buffer Specifies whether to buffer the page output or not
CacheControl Sets whether a proxy server can cache the output generated by ASP or
not
Appends the name of a character-set to the content-type header in the
Charset
Response object
ContentType Sets the HTTP content type for the Response object
Sets how long (in minutes) a page will be cached on a browser before it
Expires
expires
ExpiresAbsolute Sets a date and time when a page cached on a browser will expire
IsClientConnected Indicates if the client has disconnected from the server
Pics Appends a value to the PICS label response header
Status Specifies the value of the status line returned by the server

Methods

Method Description
AddHeader Adds a new HTTP header and a value to the HTTP response
AppendToLog Adds a string to the end of the server log entry
BinaryWrite Writes data directly to the output without any character conversion
Clear Clears any buffered HTML output
End Stops processing a script, and returns the current result
Flush Sends buffered HTML output immediately
Redirect Redirects the user to a different URL
Write Writes a specified string to the output

Example

The following simple example has a text box control where the user can enter name, a button to send
the information to the server, and a label control to display the URL of the client computer.

The content file:


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"
Inherits="server_side._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">
<title>Untitled Page</title>
</head>

<body>
<form id="form1" runat="server">
<div>

Enter your name:


<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" />
<br />
<asp:Label ID="Label1" runat="server"/>

</div>
</form>
</body>

</html>

The code behind Button1_Click:

protected void Button1_Click(object sender, EventArgs e) {

if (!String.IsNullOrEmpty(TextBox1.Text)) {

// Access the HttpServerUtility methods through


// the intrinsic Server object.
Label1.Text = "Welcome, " + Server.HtmlEncode(TextBox1.Text) + ". <br/> The url is " +
Server.UrlEncode(Request.Url.ToString())
}
}

Run the page to see the following result:


ASP.NET Server Side
Cookies
Cookies are small pieces of text, stored on the client's computer to be used only by the website
setting the cookies. This allows web applications to save information for the user, and then re-
use it on each page if needed. Here is an example where we save a users choice of background
color:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"


Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Cookies</title>
</head>
<body runat="server" id="BodyTag">
<form id="form1" runat="server">
<asp:DropDownList runat="server" id="ColorSelector" autopostback="true"
onselectedindexchanged="ColorSelector_IndexChanged">
<asp:ListItem value="White" selected="True">Select
color...</asp:ListItem>
<asp:ListItem value="Red">Red</asp:ListItem>
<asp:ListItem value="Green">Green</asp:ListItem>
<asp:ListItem value="Blue">Blue</asp:ListItem>
</asp:DropDownList>
</form>
</body>
</html>

The page simply contains a DropDownList control, which automatically posts back each time a
new item is selected. It has 3 different colors, besides the default one, which is simply white.
Once a new item is selected, the ColorSelector_IndexChanged method is fired, from our
CodeBehind file:

using System;
using System.Data;
using System.Web;

public partial class _Default : System.Web.UI.Page


{
protected void Page_Load(object sender, EventArgs e)
{
if(Request.Cookies["BackgroundColor"] != null)
{
ColorSelector.SelectedValue =
Request.Cookies["BackgroundColor"].Value;
BodyTag.Style["background-color"] = ColorSelector.SelectedValue;
}
}

protected void ColorSelector_IndexChanged(object sender, EventArgs e)


{
BodyTag.Style["background-color"] = ColorSelector.SelectedValue;
HttpCookie cookie = new HttpCookie("BackgroundColor");
cookie.Value = ColorSelector.SelectedValue;
cookie.Expires = DateTime.Now.AddHours(1);
Response.SetCookie(cookie);
}
}

Okay, two different parts to be explained here. First, the Page_Load method, which is called on
each page request. Here we check for a cookie to tell us which background color should be used.
If we find it, we set the value of our dropdown list to reflect this, as well as the background color
of the page, simply by accessing the style attribute of the body tag.

Then we have the ColorSelector_IndexChanged method, which is called each time the user
selects a new color. Here we set the background color of the page, and then we create a cookie,
to hold the value for us. We allow it to expire after one hour, and then we set it by calling the
SetCookie method on the Response object.

Try running the example, and set a color. Now close the browser, and start it up again. You will
see that the choice of color is saved, and it will remain saved for an hour. However, nothing
prevents you from saving the choice for much longer. Simply add a bigger value to the expiry
date, or set an absolute value for it.

You might also like