Monday 30 January 2012

The Button Control


The Button control is used to display a push button. The push button may be a submit button or a command button. By default, this control is a submit button.
A submit button does not have a command name and it posts the page back to the server when it is clicked. It is possible to write an event handler to control the actions performed when the submit button is clicked.
A command button has a command name and allows you to create multiple Button controls on a page. It is possible to write an event handler to control the actions performed when the command button is clicked.


<html>
<body>

<form runat="server">
<asp:Button id="b1" Text="Submit" runat="server" />
</form>

</body>
</html>

Tuesday 24 January 2012

The Page.IsPostBack Property

The Page_Load subroutine runs EVERY time the page is loaded. If you want to execute the code in the Page_Load subroutine only the FIRST time the page is loaded, you can use the Page.IsPostBack property. If the Page.IsPostBack property is false, the page is loaded for the first time, if it is true, the page is posted back to the server (i.e. from a button click on a form):

IMPORTANT QUESTIONS


What is Classic ASP?

Microsoft's previous server side scripting technology ASP (Active Server Pages) is now often called classic ASP.
ASP 3.0 was the last version of classic ASP.

ASP.NET is NOT ASP

ASP.NET is the next generation ASP, but it's not an upgraded version of ASP.
ASP.NET is an entirely new technology for server-side scripting. It was written from the ground up and is not backward compatible with classic ASP.
You can read more about the differences between ASP and ASP.NET in the next chapter of this tutorial.
ASP.NET is the major part of the Microsoft's .NET Framework.

What is ASP.NET?

ASP.NET is a server side scripting technology that enables scripts (embedded in web pages) to be executed by an Internet server.
  • ASP.NET is a Microsoft Technology
  • ASP stands for Active Server Pages
  • ASP.NET is a program that runs inside IIS
  • IIS (Internet Information Services) is Microsoft's Internet server
  • IIS comes as a free component with Windows servers
  • IIS is also a part of Windows 2000 and XP Professional

The Microsoft .NET Framework

The .NET Framework is the infrastructure for the Microsoft .NET platform.
The .NET Framework is an environment for building, deploying, and running Web applications and Web Services.
Microsoft's first server technology ASP (Active Server Pages), was a powerful and flexible "programming language". But it was too code oriented. It was not an application framework and not an enterprise development tool.
The Microsoft .NET Framework was developed to solve this problem.
.NET Frameworks keywords:
  • Easier and quicker programming
  • Reduced amount of code
  • Declarative programming model
  • Richer server control hierarchy with events
  • Larger class library
  • Better support for development tools
The .NET Framework consists of 3 main parts:
Programming languages:
  • C# (Pronounced C sharp)
  • Visual Basic (VB .NET)
  • J# (Pronounced J sharp)
Server technologies and client technologies:
  • ASP .NET (Active Server Pages)
  • Windows Forms (Windows desktop solutions)
  • Compact Framework (PDA / Mobile solutions)
Development environments:
  • Visual Studio .NET (VS .NET)
  • Visual Web Developer

ASP.NET 2.0

ASP.NET 2.0 improves upon ASP.NET by adding support for several new features.


ASP.NET 3.0

ASP.NET 3.0 is not a new version of ASP.NET. It's just the name for a new ASP.NET 2.0 framework library with support for Windows Presentation Foundation, Windows Communication Foundation, Windows Workflow Foundation; and Windows CardSpace.


Language Support

ASP.NET uses ADO.NET.
ASP.NET supports full Visual Basic, not VBScript.
ASP.NET supports C# (C sharp) and C++.
ASP.NET supports JScript.


ASP.NET - Server Controls

ASP.NET has solved the "spaghetti-code" problem described above with server controls.
Server controls are tags that are understood by the server.
There are three kinds of server controls:
  • HTML Server Controls - Traditional HTML tags
  • Web Server Controls - New ASP.NET tags
  • Validation Server Controls - For input validation

ASP.NET - HTML Server Controls

HTML server controls are HTML tags understood by the server.
HTML elements in ASP.NET files are, by default, treated as text. To make these elements programmable, add a runat="server" attribute to the HTML element. This attribute indicates that the element should be treated as a server control. The id attribute is added to identify the server control. The id reference can be used to manipulate the server control at run time.


ASP.NET - Web Server Controls

Web server controls are special ASP.NET tags understood by the server.
Like HTML server controls, Web server controls are also created on the server and they require a runat="server" attribute to work. However, Web server controls do not necessarily map to any existing HTML elements and they may represent more complex elements.
The syntax for creating a Web server control is:
<asp:control_name id="some_id" runat="server" />

ASP.NET - Validation Server Controls

Validation server controls are used to validate user-input. If the user-input does not pass validation, it will display an error message to the user.
Each validation control performs a specific type of validation (like validating against a specific value or a range of values).
By default, page validation is performed when a Button, ImageButton, or LinkButton control is clicked. You can prevent validation when a button control is clicked by setting the CausesValidation property to false.
The syntax for creating a Validation server control is:
<asp:control_name id="some_id" runat="server" />

<html>
<body>

<form runat="server">
<p>Enter a number from 1 to 100:
<asp:TextBox id="tbox1" runat="server" />
<br /><br />
<asp:Button Text="Submit" runat="server" />
</p>

<p>
<asp:RangeValidator
ControlToValidate="tbox1"
MinimumValue="1"
MaximumValue="100"
Type="Integer"
Text="The value must be from 1 to 100!"
runat="server" />
</p>
</form>

</body>
</html>




Calendar Control

The Calendar control displays a calendar through which users can navigate to any day in any year. When the ASP.NET page runs, the Calendar control is rendered as an HTML table. Therefore, a number of the control's properties pertain to various aspects of table formatting. A few of these properties are not fully supported in some older browsers, so not all of the formatting features will be available in those browsers.

Adrotator

The AdRotator Web server control can be used to display graphics that are linked to other pages. The list of graphics to be displayed and the associated target links ...


HTML View
--------------
<html>
<body>

<form runat="server">
<asp:AdRotator AdvertisementFile="Ad1.xml"
runat="server" OnAdCreated="change_url"
target="_blank" />
</form>

<p><a href="ad1.xml" target="_blank">View XML file</a></p>

</body>
</html>








XML View
------------

<Advertisements>
<Ad>
<ImageUrl>/banners/w6.gif</ImageUrl>
<NavigateUrl>http://www.w3schools.com</NavigateUrl>
<AlternateText>W3Schools Main Site</AlternateText>
<Impressions>50</Impressions>
<Keyword>elearning</Keyword>
</Ad>
<Ad>
<ImageUrl>/banners/rd_xhtml.jpg</ImageUrl>
<NavigateUrl>http://www.w3schools.com/xhtml/default.asp</NavigateUrl>
<AlternateText>XHTML Tutorial</AlternateText>
<Impressions>50</Impressions>
<Keyword>XHTML</Keyword>
</Ad>
</Advertisements>

Validation Server Controls


A Validation server control is used to validate the data of an input control. If the data does not pass validation, it will display an error message to the user.
The syntax for creating a Validation server control is:
<asp:control_name id="some_id" runat="server" />

Validation Server ControlDescription
CompareValidatorCompares the value of one input control to the value of another input control or to a fixed value
CustomValidatorAllows you to write a method to handle the validation of the value entered
RangeValidatorChecks that the user enters a value that falls between two values
RegularExpressionValidatorEnsures that the value of an input control matches a specified pattern
RequiredFieldValidatorMakes an input control a required field
ValidationSummaryDisplays a report of all validation errors occurred in a Web page

Web Server Controls


Like HTML server controls, Web server controls are also created on the server and they require a runat="server" attribute to work. However, Web server controls do not necessarily map to any existing HTML elements and they may represent more complex elements.
The syntax for creating a Web server control is:
<asp:control_name id="some_id" runat="server" />

Web Server ControlDescription
AdRotatorDisplays a sequence of images
ButtonDisplays a push button
CalendarDisplays a calendar
CalendarDayA day in a calendar control
CheckBoxDisplays a check box
CheckBoxListCreates a multi-selection check box group
DataGridDisplays fields of a data source in a grid
DataListDisplays items from a data source by using templates
DropDownListCreates a drop-down list
HyperLinkCreates a hyperlink
ImageDisplays an image
ImageButtonDisplays a clickable image
LabelDisplays static content which is programmable (lets you apply styles to its content)
LinkButtonCreates a hyperlink button
ListBoxCreates a single- or multi-selection drop-down list
ListItemCreates an item in a list
LiteralDisplays static content which is programmable(does not let you apply styles to its content)
PanelProvides a container for other controls
PlaceHolderReserves space for controls added by code
RadioButtonCreates a radio button
RadioButtonListCreates a group of radio buttons
BulletedListCreates a list in bullet format
RepeaterDisplays a repeated list of items bound to the control
StyleSets the style of controls
TableCreates a table
TableCellCreates a table cell
TableRowCreates a table row
TextBoxCreates a text box
XmlDisplays an XML file or the results of an XSL transform

AJAX - Send a Request To a Server


To send a request to a server, we use the open() and send() methods of the XMLHttpRequest object:
xmlhttp.open("GET","ajax_info.txt",true);
xmlhttp.send();

MethodDescription
open(method,url,async)Specifies the type of request, the URL, and if the request should be handled asynchronously or not.

method: the type of request: GET or POST
url: the location of the file on the server
async: true (asynchronous) or false (synchronous)
send(string)Sends the request off to the server.

string: Only used for POST requests


GET or POST?

GET is simpler and faster than POST, and can be used in most cases.
However, always use POST requests when:
  • A cached file is not an option (update a file or database on the server)
  • Sending a large amount of data to the server (POST has no size limitations)
  • Sending user input (which can contain unknown characters), POST is more robust and secure than GET

GET Requests

A simple GET request:

Example

xmlhttp.open("GET","demo_get.asp",true);
xmlhttp.send();




<html>
<head>
<script type="text/javascript">
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","demo_get.asp",true);
xmlhttp.send();
}
</script>
</head>
<body>

<h2>AJAX</h2>
<button type="button" onclick="loadXMLDoc()">Request data</button>
<div id="myDiv"></div>

</body>
</html>

AJAX


AJAX = Asynchronous JavaScript and XML.
AJAX is not a new programming language, but a new way to use existing standards.
AJAX is the art of exchanging data with a server, and update parts of a web page - without reloading the whole page.




What is AJAX?

AJAX = Asynchronous JavaScript and XML.
AJAX is a technique for creating fast and dynamic web pages.
AJAX allows web pages to be updated asynchronously by exchanging small amounts of data with the server behind the scenes. This means that it is possible to update parts of a web page, without reloading the whole page.
Classic web pages, (which do not use AJAX) must reload the entire page if the content should change.
Examples of applications using AJAX: Google Maps, Gmail, Youtube, and Facebook tabs.



AJAX is Based on Internet Standards

AJAX is based on internet standards, and uses a combination of:
  • XMLHttpRequest object (to exchange data asynchronously with a server)
  • JavaScript/DOM (to display/interact with the information)
  • CSS (to style the data)
  • XML (often used as the format for transferring data)
lamp  AJAX applications are browser- and platform-independent!





AJAX Example Explained

The AJAX application above contains one div section and one button.
The div section will be used to display information returned from a server. The button calls a function named loadXMLDoc(), if it is clicked:
<html>
<body>

<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="loadXMLDoc()">Change Content</button>

</body>
</html>
Next, add a <script> tag to the page's head section. The script section contains the loadXMLDoc() function:
<head>
<script type="text/javascript">
function loadXMLDoc()
{
.... AJAX script goes here ...
}
</script>
</head>