Minifycode 2022-04-24 Viewed 1K times ASP.NET

In this article you will learn, what is server side controls. We will discuss each of these objects in due time. In this tutorial we will explore the Server object, the Request object, and the Response object.

  • Session
  • Application
  • Cache
  • Request
  • Response
  • Server
  • User
  • Trace

 

Server Object

The Server object in Asp.NET is an instance of the System.Web.HttpServerUtility class. The HttpServerUtility class provides numerous properties and methods to perform many type of jobs.

Methods and Properties of the Server object

The methods and properties of the HttpServerUtility class are exposed through the intrinsic Server object provided by ASP.NET.

The following table provides a list of the properties:

Property Description
MachineName Name of server computer
ScriptTimeOut Gets and sets the request time-out value in seconds.

 

The following article provides a list of some important method:

Method Description
CreateObject(String) Creates an instance of the COM object identified by its PID (Programmatic ID).
CreateObject(Type) Creates an instance of the COM object identified by its Type.
Equals(Object) Determines whether the specified Object is equal to the current Object.
Execute(String) Executes the handler for the specified virtual path in the context of the current request.
Execute(String, Boolean) Executes the handler for the specified virtual path in the context of the current request and specifies whether to clear the QueryString and Form collections.
GetLastError Returns the previous exception.
GetType Gets the Type of the current instance.
HtmlEncode Changes an ordinary string into a string with legal HTML characters.
HtmlDecode Converts an Html string into an ordinary string.
ToString Returns a String that represents the current Object.
Transfer(String) For the current request, terminates execution of the current page and starts execution of a new page by using the specified URL path of the page.
UrlDecode Converts an URL string into an ordinary string.
UrlEncodeToken Works same as UrlEncode, but on a byte array that contains Base64-encoded data.
UrlDecodeToken Works same as UrlDecode, but on a byte array that contains Base64-encoded data.
MapPath Return the physical path that corresponds to a specified virtual file path on the server.
Transfer Transfers execution to another web page in the current application.

Object of a Request

The object request is an instance of the System.Web.HttpRequest class. It represents the values and properties of the HTTP request that makes the page loading into the browser.

The information presented by this object is wrapped by the higher level abstractions (model). However, this object helps in checking some information such as the client browser and cookies.

Properties and Methods of the Object Request

The following content provides some properties of the Request object:

Property Description
AcceptTypes Gets a string array of client-supported MIME accept types.
ApplicationPath Gets the ASP.NET application's virtual application root path on the server.
Browser Gets or sets information about the requesting client's browser capabilities.
ContentEncoding Gets or sets the character set of the entity-body.
ContentLength Specifies the length, in bytes, of content sent by the client.
ContentType Gets or sets the MIME content type of the incoming request.
Cookies Gets a collection of cookies sent by the client.
FilePath Gets the virtual path of the current request.
Files Gets the collection of files uploaded by the client, in multipart MIME format.
Form Gets a collection of form variables.
Headers Gets a collection of HTTP headers.
HttpMethod Gets the HTTP data transfer method (such as GET, POST, or HEAD) used by the client.
InputStream Gets the contents of the incoming HTTP entity body.
IsSecureConnection Gets a value indicating whether the HTTP connection uses secure sockets (that is, HTTPS).
QueryString Gets the collection of HTTP query string variables.
RawUrl Gets the raw URL of the current request.
RequestType Gets or sets the HTTP data transfer method (GET or POST) used by the client.
ServerVariables Gets a collection of Web server variables.
TotalBytes Gets the number of bytes in the current input stream.
Url Gets information about the URL of the current request.
UrlReferrer Gets information about the URL of the client's previous request that is linked to the current URL.
UserAgent Gets the raw user agent string of the client browser.
UserHostAddress Gets the IP host address of the remote client.
UserHostName Gets the DNS name of the remote client.
UserLanguages Gets a sorted string array of client language preferences.

The following table provides a list of some important methods:

Method Description
BinaryRead Performs a binary read of a specified number of bytes from the current input stream.
Equals(Object) Determines whether the specified object is equal to the current object. (Inherited from object.)
GetType Gets the Type of the current instance.
MapImageCoordinates Maps an incoming image-field form parameter to appropriate x-coordinate and y-coordinate values.
MapPath(String) Maps the specified virtual path to a physical path.
SaveAs Saves an HTTP request to disk.
ToString Returns a String that represents the current object.
ValidateInput Causes validation to occur for the collections accessed through the Cookies, Form, and QueryString properties.

Response Object

The Response object represents the server's response to the client request. It is an instance of the System.Web.HttpResponse class.

In ASP.NET, the response object does not play any vital role in sending HTML text to the client, because the server-side controls have nested, object oriented methods for rendering themselves.

However, the HttpResponse object still provides some important functionalities, like the cookie feature and the Redirect() method. The Response.Redirect() method allows transferring the user to another page, inside as well as outside the application. It requires a round trip.

Properties and Methods of the Response Object

The following table provides some noteworthy properties of the Response object:

Property Description
Buffer Get or set a value indicating whether to buffer the output and send it after the complete response is finished processing.
BufferOutput Get or set a value indicating whether to buffer the output and send it after the complete page is finished processing.
Charset Get or set the HTTP character set of the output stream.
ContentEncoding Get or set the HTTP character set of the output stream.
ContentType Get or set the HTTP MIME type of the output stream.
Cookies Get the response cookie collection.
Expires Get or set the number of minutes before a page cached on a browser expires.
ExpiresAbsolute Get or set the absolute date and time at which to remove cached information from the cache.
HeaderEncoding Gets or sets an encoding object that represents the encoding for the current header output stream.
Headers Gets the collection of response headers.
IsClientConnected Gets a value indicating whether the client is still connected to the server.
Output Enables output of text to the outgoing HTTP response stream.
OutputStream Enables binary output to the outgoing HTTP content body.
RedirectLocation Gets or sets the value of the Http Location header.
Status Sets the status line that is returned to the client.
StatusCode Gets or sets the HTTP status code of the output returned to the client.
StatusDescription Gets or sets the HTTP status string of the output returned to the client.
SubStatusCode Gets or sets a value qualifying the status code of the response.
SuppressContent Gets or sets a value indicating whether to send HTTP content to the client.

 

Example

 

<%@ 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>minifycode.in</title> 
</head> 
<body> 
<form id="form1" runat="server"> <div> 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>

Button1_Click:

protected void Button1_Click(object sender, EventArgs e) 
{
 if (!String.IsNullOrEmpty(TextBox1.Text)) 
{
 // the intrinsic Server object. 
Label1.Text = "Welcome, " + Server.HtmlEncode(TextBox1.Text) + ". <br/>Url is " + Server.UrlEncode(Request.Url.ToString()) 
}
 }

Server Side Methods, Properties and Object in asp.net
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html> <html> <head> <title>Asp.Net Repeater Control</title> </head> <body> <form id="form1" runat="server"> <div style="font:13px Verdana;width:310px;"> <asp:ScriptManager ID="scriptManager" runat="server" EnablePageMethods="true" /> <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"> <ContentTemplate> <h3>Asp.Net Repeater Control</h3> <%--THE REPEATER CONTROL.--%> <asp:Repeater id="rp" runat="server"> <%--HEADER OF THE REPEATER--%> <HeaderTemplate> <table border="0" width="200px"> </HeaderTemplate> <%--SHOWING ITEMS--%> <ItemTemplate> <tr> <td style="padding:2px; border:solid 1px #CCC;"> <asp:Label Text='<%# Container.DataItem.ToString() %>' runat="server"></asp:Label> </td> </tr> </ItemTemplate> <%--ALTERNATE TEMPLATE (SHOWING ITEMS IN DIFFERENT COLOR--%> <AlternatingItemTemplate> <tr> <td style="padding:2px; border:solid 1px #CCC; background:#EAF7FB; width:200px;"> <asp:Label Text='<%# Container.DataItem.ToString() %>' runat="server"></asp:Label> </td> </tr> </AlternatingItemTemplate> <%--REPEATER FOOTER--%> <FooterTemplate> </table> <div style="padding:20px 0;"> <asp:Label ID="lblFoot" runat="server"></asp:Label> </div> </FooterTemplate> </asp:Repeater> <%--BUTTON CONTROL--%> <asp:Button ID="btVF" Text="View Files" AutoPostBack="true" OnClick="Show" Font-Names="sanserif" Font-Italic="true" Font-Size="155%" runat="server" /> </ContentTemplate> <Triggers> <asp:AsyncPostBackTrigger ControlID="PostBackTrigger" /> </Triggers> </asp:UpdatePanel> </div> </form> </body> </html> using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.IO; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) {} protected void Show(object sender, EventArgs e) { DirectoryInfo Folder = new DirectoryInfo(Server.MapPath("Files\\")); FileInfo[] fileList = Folder.GetFiles("*.*"); //BIND THE FILE LIST WITH THE REPEATER CONTROL. rp.DataSource = fileList; rp.DataBind(); } } Adding the <AlternatingTemplate> below the <ItemTemplate> will show files with a different background color for every alternate row. <AlternatingItemTemplate> <div style="padding:3px; border:solid 1px #FFF; background:#black"> <asp:Label Text='<%# Container.DataItem.ToString() %>' runat="server"></asp:Label> </div> </AlternatingItemTemplate> </AlternatingItemTemplate> <SeparatorTemplate><hr /> </SeparatorTemplate> Add the <HeaderTemplate> element just above the <ItemTemplate> <HeaderTemplate> <div style="padding-bottom:20px"><strong>Files</strong> (Header)</div> </HeaderTemplate> Repeater with <FooterTemplate> element <FooterTemplate> <div style="padding-top:20px"><asp:Label ID="lblFooter" runat="server"></asp:Label></div> </FooterTemplate> Code Behind In C# Control ft = rp.Controls[rep.Controls.Count - 1].Controls[0]; Label lbl_Footer = ft.FindControl("lblFooter") as Label; lbl_Footer.Text = "Showing total <b>" + fileList.Length + "</b> files in the Footer.";