Pages

24 Nov 2013

ASP.NET Web Forms - The ArrayList Object

The ArrayList object is a collection of items containing a single data value.

Examples

Try it Yourself - Examples


Create an ArrayList

The ArrayList object is a collection of items containing a single data value.
Items are added to the ArrayList with the Add() method.
The following code creates a new ArrayList object named mycountries and four items are added:
<script runat="server">
Sub Page_Load
if Not Page.IsPostBack then
  dim mycountries=New ArrayList
  mycountries.Add("Norway")
  mycountries.Add("Sweden")
  mycountries.Add("France")
  mycountries.Add("Italy")
end if
end sub
</script>
By default, an ArrayList object contains 16 entries. An ArrayList can be sized to its final size with the TrimToSize() method:
<script runat="server">
Sub Page_Load
if Not Page.IsPostBack then
  dim mycountries=New ArrayList
  mycountries.Add("Norway")
  mycountries.Add("Sweden")
  mycountries.Add("France")
  mycountries.Add("Italy")
  mycountries.TrimToSize()
end if
end sub
</script>
An ArrayList can also be sorted alphabetically or numerically with the Sort() method:
<script runat="server">
Sub Page_Load
if Not Page.IsPostBack then
  dim mycountries=New ArrayList
  mycountries.Add("Norway")
  mycountries.Add("Sweden")
  mycountries.Add("France")
  mycountries.Add("Italy")
  mycountries.TrimToSize()
  mycountries.Sort()
end if
end sub
</script>
To sort in reverse order, apply the Reverse() method after the Sort() method:
<script runat="server">
Sub Page_Load
if Not Page.IsPostBack then
  dim mycountries=New ArrayList
  mycountries.Add("Norway")
  mycountries.Add("Sweden")
  mycountries.Add("France")
  mycountries.Add("Italy")
  mycountries.TrimToSize()
  mycountries.Sort()
  mycountries.Reverse()
end if
end sub
</script>


Data Binding to an ArrayList

An ArrayList object may automatically generate the text and values to the following controls:
  • asp:RadioButtonList
  • asp:CheckBoxList
  • asp:DropDownList
  • asp:Listbox
To bind data to a RadioButtonList control, first create a RadioButtonList control (without any asp:ListItem elements) in an .aspx page:
<html>
<body>

<form runat="server">
<asp:RadioButtonList id="rb" runat="server" />
</form>

</body>
</html>
Then add the script that builds the list and binds the values in the list to the RadioButtonList control:

Example

<script runat="server">
Sub Page_Load
if Not Page.IsPostBack then
  dim mycountries=New ArrayList
  mycountries.Add("Norway")
  mycountries.Add("Sweden")
  mycountries.Add("France")
  mycountries.Add("Italy")
  mycountries.TrimToSize()
  mycountries.Sort()
  rb.DataSource=mycountries
  rb.DataBind()
end if
end sub
</script>

<html>
<body>

<form runat="server">
<asp:RadioButtonList id="rb" runat="server" />
</form>

</body>
</html>

Show example »
The DataSource property of the RadioButtonList control is set to the ArrayList and it defines the data source of the RadioButtonList control. The DataBind() method of the RadioButtonList control binds the data source with the RadioButtonList control.
Note: The data values are used as both the Text and Value properties for the control. To add Values that are different from the Text, use either the Hashtable object or the SortedList object.

ASP.NET Web Forms - Data Binding

We may use data binding to fill lists with selectable items from an imported data source, like a database, an XML file, or a script.

Data Binding

The following controls are list controls which support data binding:
  • asp:RadioButtonList
  • asp:CheckBoxList
  • asp:DropDownList
  • asp:Listbox
The selectable items in each of the above controls are usually defined by one or more asp:ListItem controls, like this:
<html>
<body>

<form runat="server">
<asp:RadioButtonList id="countrylist" runat="server">
<asp:ListItem value="N" text="Norway" />
<asp:ListItem value="S" text="Sweden" />
<asp:ListItem value="F" text="France" />
<asp:ListItem value="I" text="Italy" />
</asp:RadioButtonList>
</form>

</body>
</html>
However, with data binding we may use a separate source, like a database, an XML file, or a script to fill the list with selectable items.
By using an imported source, the data is separated from the HTML, and any changes to the items are made in the separate data source.
In the next three chapters, we will describe how to bind data from a scripted data source.

17 Nov 2013

ASP.NET Web Forms - The Button Control

The Button control is used to display a push button.

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.
The Button control's attributes and properties are listed in our web controls reference page.
The example below demonstrates a simple Button control:
<html>
<body>

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

</body>
</html>


Add a Script

A form is most often submitted by clicking on a button.
In the following example we declare one TextBox control, one Button control, and one Label control in an .aspx file. When the submit button is triggered, the submit subroutine is executed. The submit subroutine writes a text to the Label control:

Example

<script runat="server">
Sub submit(sender As Object, e As EventArgs)
lbl1.Text="Your name is " & txt1.Text
End Sub
</script>

<html>
<body>

<form runat="server">
Enter your name:
<asp:TextBox id="txt1" runat="server" />
<asp:Button OnClick="submit" Text="Submit" runat="server" />
<p><asp:Label id="lbl1" runat="server" /></p>
</form>

</body>
</html>

Show example »

ASP.NET Web Forms - The TextBox Control

The TextBox control is used to create a text box where the user can input text.

The TextBox Control

The TextBox control is used to create a text box where the user can input text.
The TextBox control's attributes and properties are listed in our web controls reference page.
The example below demonstrates some of the attributes you may use with the TextBox control:

Example

<html>
<body>

<form runat="server">

A basic TextBox:
<asp:TextBox id="tb1" runat="server" />
<br /><br />

A password TextBox:
<asp:TextBox id="tb2" TextMode="password" runat="server" />
<br /><br />

A TextBox with text:
<asp:TextBox id="tb4" Text="Hello World!" runat="server" />
<br /><br />

A multiline TextBox:
<asp:TextBox id="tb3" TextMode="multiline" runat="server" />
<br /><br />

A TextBox with height:
<asp:TextBox id="tb6" rows="5" TextMode="multiline"
runat="server" />
<br /><br />

A TextBox with width:
<asp:TextBox id="tb5" columns="30" runat="server" />

</form>

</body>
</html>

Show example »

Add a Script

The contents and settings of a TextBox control may be changed by server scripts when a form is submitted. A form can be submitted by clicking on a button or when a user changes the value in the TextBox control.
In the following example we declare one TextBox control, one Button control, and one Label control in an .aspx file. When the submit button is triggered, the submit subroutine is executed. The submit subroutine writes a text to the Label control:

Example

<script runat="server">
Sub submit(sender As Object, e As EventArgs)
lbl1.Text="Your name is " & txt1.Text
End Sub
</script>

<html>
<body>

<form runat="server">
Enter your name:
<asp:TextBox id="txt1" runat="server" />
<asp:Button OnClick="submit" Text="Submit" runat="server" />
<p><asp:Label id="lbl1" runat="server" /></p>
</form>

</body>
</html>

Show example »
In the following example we declare one TextBox control and one Label control in an .aspx file. When you change the value in the TextBox and either click outside the TextBox or press the Tab key, the change subroutine is executed. The submit subroutine writes a text to the Label control:

Example

<script runat="server">
Sub change(sender As Object, e As EventArgs)
lbl1.Text="You changed text to " & txt1.Text
End Sub
</script>

<html>
<body>

<form runat="server">
Enter your name:
<asp:TextBox id="txt1" runat="server"
text="Hello World!"
ontextchanged="change" autopostback="true"/>
<p><asp:Label id="lbl1" runat="server" /></p>
</form>

</body>
</html>

Show example »

13 Nov 2013

ASP.NET Web Forms - Maintaining the ViewState

You may save a lot of coding by maintaining the ViewState of the objects in your Web Form.

Maintaining the ViewState

When a form is submitted in classic ASP, all form values are cleared. Suppose you have submitted a form with a lot of information and the server comes back with an error. You will have to go back to the form and correct the information. You click the back button, and what happens.......ALL form values are CLEARED, and you will have to start all over again! The site did not maintain your ViewState.
When a form is submitted in ASP .NET, the form reappears in the browser window together with all form values. How come? This is because ASP .NET maintains your ViewState. The ViewState indicates the status of the page when submitted to the server. The status is defined through a hidden field placed on each page with a <form runat="server"> control. The source could look something like this:
<form name="_ctl0" method="post" action="page.aspx" id="_ctl0">
<input type="hidden" name="__VIEWSTATE"
value="dDwtNTI0ODU5MDE1Ozs+ZBCF2ryjMpeVgUrY2eTj79HNl4Q=" />

.....some code

</form>
Maintaining the ViewState is the default setting for ASP.NET Web Forms. If you want to NOT maintain the ViewState, include the directive <%@ Page EnableViewState="false" %> at the top of an .aspx page or add the attribute EnableViewState="false" to any control.
Look at the following .aspx file. It demonstrates the "old" way to do it. When you click on the submit button, the form value will disappear:

Example

<html>
<body>

<form action="demo_classicasp.aspx" method="post">
Your name: <input type="text" name="fname" size="20">
<input type="submit" value="Submit">
</form>
<%
dim fname
fname=Request.Form("fname")
If fname<>"" Then
Response.Write("Hello " & fname & "!")
End If
%>

</body>
</html>

Show example »
Here is the new ASP .NET way. When you click on the submit button, the form value will NOT disappear:

Example

Click view source in the right frame of the example to see that ASP .NET has added a hidden field in the form to maintain the ViewState
<script runat="server">
Sub submit(sender As Object, e As EventArgs)
lbl1.Text="Hello " & txt1.Text & "!"
End Sub
</script>

<html>
<body>

<form runat="server">
Your name: <asp:TextBox id="txt1" runat="server" />
<asp:Button OnClick="submit" Text="Submit" runat="server" />
<p><asp:Label id="lbl1" runat="server" /></p>
</form>

</body>
</html>

Show example »

ASP.NET Web Forms - HTML Forms

All server controls must appear within a <form> tag, and the <form> tag must contain the runat="server" attribute.

ASP.NET Web Forms

All server controls must appear within a <form> tag, and the <form> tag must contain the runat="server" attribute. The runat="server" attribute indicates that the form should be processed on the server. It also indicates that the enclosed controls can be accessed by server scripts:
<form runat="server">

...HTML + server controls

</form>
Note: The form is always submitted to the page itself. If you specify an action attribute, it is ignored. If you omit the method attribute, it will be set to method="post" by default. Also, if you do not specify the name and id attributes, they are automatically assigned by ASP.NET.
Note: An .aspx page can only contain ONE <form runat="server"> control!
If you select view source in an .aspx page containing a form with no name, method, action, or id attribute specified, you will see that ASP.NET has added these attributes to the form. It looks something like this:
<form name="_ctl0" method="post" action="page.aspx" id="_ctl0">

...some code

</form>


Submitting a Form

A form is most often submitted by clicking on a button. The Button server control in ASP.NET has the following format:
<asp:Button id="id" text="label" OnClick="sub" runat="server" />
The id attribute defines a unique name for the button and the text attribute assigns a label to the button. The onClick event handler specifies a named subroutine to execute.

7 Nov 2013

ASP.NET Web Forms - Events

An Event Handler is a subroutine that executes code for a given event.

ASP.NET - Event Handlers

Look at the following code:
<%
lbl1.Text="The date and time is " & now()
%>

<html>
<body>
<form runat="server">
<h3><asp:label id="lbl1" runat="server" /></h3>
</form>
</body>
</html>
When will the code above be executed? The answer is: "You don't know..."

The Page_Load Event

The Page_Load event is one of many events that ASP.NET understands. The Page_Load event is triggered when a page loads, and ASP.NET will automatically call the subroutine Page_Load, and execute the code inside it:

Example

<script runat="server">
Sub Page_Load
lbl1.Text="The date and time is " & now()
End Sub
</script>

<html>
<body>
<form runat="server">
<h3><asp:label id="lbl1" runat="server" /></h3>
</form>
</body>
</html>

Show example »
Note: The Page_Load event contains no object references or event arguments!

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):

Example

<script runat="server">
Sub Page_Load
if Not Page.IsPostBack then
  lbl1.Text="The date and time is " & now()
end if
End Sub

Sub submit(s As Object, e As EventArgs)
lbl2.Text="Hello World!"
End Sub
</script>

<html>
<body>
<form runat="server">
<h3><asp:label id="lbl1" runat="server" /></h3>
<h3><asp:label id="lbl2" runat="server" /></h3>
<asp:button text="Submit" onclick="submit" runat="server" />
</form>
</body>
</html>

ASP.NET Web Forms - Server Controls

Server controls are tags that are understood by the server.

Limitations in Classic ASP

The listing below was copied from the previous chapter:
<html>
<body bgcolor="yellow">
<center>
<h2>Hello W3Schools!</h2>
<p><%Response.Write(now())%></p>
</center>
</body>
</html>
The code above illustrates a limitation in Classic ASP: The code block has to be placed where you want the output to appear.
With Classic ASP it is impossible to separate executable code from the HTML itself. This makes the page difficult to read, and difficult to maintain.

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.
Note: All HTML server controls must be within a <form> tag with the runat="server" attribute. The runat="server" attribute indicates that the form should be processed on the server. It also indicates that the enclosed controls can be accessed by server scripts.
In the following example we declare an HtmlAnchor server control in an .aspx file. Then we manipulate the HRef attribute of the HtmlAnchor control in an event handler (an event handler is a subroutine that executes code for a given event). The Page_Load event is one of many events that ASP.NET understands:
<script runat="server">
Sub Page_Load
link1.HRef="http://www.w3schools.com"
End Sub
</script>

<html>
<body>

<form runat="server">
<a id="link1" runat="server">Visit W3Schools!</a>
</form>

</body>
</html>
The executable code itself has been moved outside the HTML.

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" />
In the following example we declare a Button server control in an .aspx file. Then we create an event handler for the Click event which changes the text on the button:
<script runat="server">
Sub submit(Source As Object, e As EventArgs)
button1.Text="You clicked me!"
End Sub
</script>

<html>
<body>

<form runat="server">
<asp:Button id="button1" Text="Click me!"
runat="server" OnClick="submit"/>
</form>

</body>
</html>


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" />
In the following example we declare one TextBox control, one Button control, and one RangeValidator control in an .aspx file. If validation fails, the text "The value must be from 1 to 100!" will be displayed in the RangeValidator control:

Example

<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>

5 Nov 2013

ASP.NET Web Forms - Events

An Event Handler is a subroutine that executes code for a given event.

ASP.NET - Event Handlers

Look at the following code:
<%
lbl1.Text="The date and time is " & now()
%>

<html>
<body>
<form runat="server">
<h3><asp:label id="lbl1" runat="server" /></h3>
</form>
</body>
</html>
When will the code above be executed? The answer is: "You don't know..."

The Page_Load Event

The Page_Load event is one of many events that ASP.NET understands. The Page_Load event is triggered when a page loads, and ASP.NET will automatically call the subroutine Page_Load, and execute the code inside it:

Example

<script runat="server">
Sub Page_Load
lbl1.Text="The date and time is " & now()
End Sub
</script>

<html>
<body>
<form runat="server">
<h3><asp:label id="lbl1" runat="server" /></h3>
</form>
</body>
</html>

Show example »
Note: The Page_Load event contains no object references or event arguments!

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):

Example

<script runat="server">
Sub Page_Load
if Not Page.IsPostBack then
  lbl1.Text="The date and time is " & now()
end if
End Sub

Sub submit(s As Object, e As EventArgs)
lbl2.Text="Hello World!"
End Sub
</script>

<html>
<body>
<form runat="server">
<h3><asp:label id="lbl1" runat="server" /></h3>
<h3><asp:label id="lbl2" runat="server" /></h3>
<asp:button text="Submit" onclick="submit" runat="server" />
</form>
</body>
</html>

ASP.NET Web Forms - Server Controls

Server controls are tags that are understood by the server.

Limitations in Classic ASP

The listing below was copied from the previous chapter:
<html>
<body bgcolor="yellow">
<center>
<h2>Hello W3Schools!</h2>
<p><%Response.Write(now())%></p>
</center>
</body>
</html>
The code above illustrates a limitation in Classic ASP: The code block has to be placed where you want the output to appear.
With Classic ASP it is impossible to separate executable code from the HTML itself. This makes the page difficult to read, and difficult to maintain.

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.
Note: All HTML server controls must be within a <form> tag with the runat="server" attribute. The runat="server" attribute indicates that the form should be processed on the server. It also indicates that the enclosed controls can be accessed by server scripts.
In the following example we declare an HtmlAnchor server control in an .aspx file. Then we manipulate the HRef attribute of the HtmlAnchor control in an event handler (an event handler is a subroutine that executes code for a given event). The Page_Load event is one of many events that ASP.NET understands:
<script runat="server">
Sub Page_Load
link1.HRef="http://www.w3schools.com"
End Sub
</script>

<html>
<body>

<form runat="server">
<a id="link1" runat="server">Visit W3Schools!</a>
</form>

</body>
</html>
The executable code itself has been moved outside the HTML.

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" />
In the following example we declare a Button server control in an .aspx file. Then we create an event handler for the Click event which changes the text on the button:
<script runat="server">
Sub submit(Source As Object, e As EventArgs)
button1.Text="You clicked me!"
End Sub
</script>

<html>
<body>

<form runat="server">
<asp:Button id="button1" Text="Click me!"
runat="server" OnClick="submit"/>
</form>

</body>
</html>


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" />
In the following example we declare one TextBox control, one Button control, and one RangeValidator control in an .aspx file. If validation fails, the text "The value must be from 1 to 100!" will be displayed in the RangeValidator control:

Example

<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>

30 Oct 2013

ASP.NET Web Forms - HTML Pages

A simple ASP.NET page looks just like an ordinary HTML page.

Hello W3Schools

To start learning ASP.NET, we will construct a very simple HTML page that will display "Hello W3Schools" in an Internet browser like this:

Hello W3Schools!




Hello W3Schools in HTML

This code displays the example as an HTML page:
<html>
<body bgcolor="yellow">
<center>
<h2>Hello W3Schools!</h2>
</center>
</body>
</html>
If you want to try it yourself, save the code in a file called "firstpage.htm", and create a link to the file like this:firstpage.htm

Hello W3Schools in ASP.NET

The simplest way to convert an HTML page into an ASP.NET page is to copy the HTML file to a new file with an .aspxextension.
This code displays our example as an ASP.NET page:
<html>
<body bgcolor="yellow">
<center>
<h2>Hello W3Schools!</h2>
</center>
</body>
</html>
If you want to try it yourself, save the code in a file called "firstpage.aspx", and create a link to the file like this:firstpage.aspx

How Does it Work?

Fundamentally an ASP.NET page is just the same as an HTML page.
An HTML page has the extension .htm. If a browser requests an HTML page from the server, the server sends the page to the browser without any modifications.
An ASP.NET page has the extension .aspx. If a browser requests an ASP.NET page, the server processes any executable code in the page, before the result is sent back to the browser.
The ASP.NET page above does not contain any executable code, so nothing is executed. In the next examples we will add some executable code to the page to demonstrate the difference between static HTML pages and dynamic ASP pages.

Classic ASP

Active Server Pages (ASP) has been around for several years. With ASP, executable code can be placed inside HTML pages.
Previous versions of ASP (before ASP .NET) are often called Classic ASP.
ASP.NET is not fully compatible with Classic ASP, but most Classic ASP pages will work fine as ASP.NET pages, with only minor changes.
If you want to learn more about Classic ASP, please visit our ASP Tutorial.

Dynamic Page in Classic ASP

To demonstrate how ASP can display pages with dynamic content, we have added some executable code (in red) to the previous example:
<html>
<body bgcolor="yellow">
<center>
<h2>Hello W3Schools!</h2>
<p><%Response.Write(now())%></p>
</center>
</body>
</html>
The code inside the <% --%> tags is executed on the server.
Response.Write is ASP code for writing something to the HTML output stream.
Now() is a function returning the servers current date and time.
If you want to try it yourself, save the code in a file called "dynpage.asp", and create a link to the file like this:dynpage.asp

Dynamic Page in ASP .NET

This following code displays our example as an ASP.NET page:
<html>
<body bgcolor="yellow">
<center>
<h2>Hello W3Schools!</h2>
<p><%Response.Write(now())%></p>
</center>
</body>
</html>
If you want to try it yourself, save the code in a file called "dynpage.aspx", and create a link to the file like this:dynpage.aspx

ASP.NET vs Classic ASP

The previous examples didn't demonstrate any differences between ASP.NET and Classic ASP.
As you can see from the two latest examples there are no differences between the two ASP and ASP.NET pages.
In the next chapters you will see how server controls make ASP.NET more powerful than Classic ASP.

ASP.NET Web Forms - Tutorial

Where to Start?

Many developers like to start learning a new technology by looking at working examples.
If you want to take a look at a working Web Forms examples, follow the ASP.NET Web Forms Demo.

What is Web Forms?

Web Forms is one of the 3 programming models for creating ASP.NET web sites and web applications.
The other two programming models are Web Pages and MVC (Model, View, Controller).
Web Forms is the oldest ASP.NET programming model, with event driven web pages written as a combination of HTML, server controls, and server code.
Web Forms are compiled and executed on the server, which generates the HTML that displays the web pages.
Web Forms comes with hundreds of different web controls and web components to build user-driven web sites with data access.

Visual Studio Express 2012/2010

Visual Studio Express is a free version of Microsoft Visual Studio.
Visual Studio Express is a development tool tailor made for Web Forms (and MVC).
Visual Studio Express contains:
  • MVC and Web Forms
  • Drag-and-drop web controls and web components
  • A web server language (Razor using VB or C#)
  • A web server (IIS Express)
  • A database server (SQL Server Compact)
  • A full web development framework (ASP.NET)
If you install Visual Studio Express, you will get more benefits from this tutorial.
If you want to install Visual Studio Express, click on one of these links:
Visual Web Developer 2012 (If you have Windows 7 or Windows 8)
Visual Web Developer 2010 (If you have Windows Vista or XP)

ASP.NET References

At the end of this tutorial you will find a complete ASP.NET reference with objects, components, properties and methods.
ASP.NET Reference

29 Oct 2013

ASP.NET MVC - Reference

Classes

ClassDescription
AcceptVerbsAttributeRepresents an attribute that specifies which HTTP verbs an action method will respond to.
ActionDescriptorProvides information about an action method, such as its name, controller, parameters, attributes, and filters.
ActionExecutedContextProvides the context for the ActionExecuted method of the ActionFilterAttribute class.
ActionExecutingContextProvides the context for the ActionExecuting method of the ActionFilterAttribute class.
ActionFilterAttributeRepresents the base class for filter attributes.
ActionMethodSelectorAttributeRepresents an attribute that is used to influence the selection of an action method.
ActionNameAttributeRepresents an attribute that is used for the name of an action.
ActionNameSelectorAttributeRepresents an attribute that affects the selection of an action method.
ActionResultEncapsulates the result of an action method and is used to perform a framework-level operation on behalf of the action method.
AdditionalMetadataAttributeProvides a class that implements the IMetadataAware interface in order to support additional metadata.
AjaxHelperRepresents support for rendering HTML in AJAX scenarios within a view.
AjaxHelper(Of TModel)Represents support for rendering HTML in AJAX scenarios within a strongly typed view.
AjaxRequestExtensionsRepresents a class that extends the HttpRequestBase class by adding the ability to determine whether an HTTP request is an AJAX request.
AllowHtmlAttributeAllows a request to include HTML markup during model binding by skipping request validation for the property. (It is strongly recommended that your application explicitly check all models where you disable request validation in order to prevent script exploits.)
AreaRegistrationProvides a way to register one or more areas in an ASP.NET MVC application.
AreaRegistrationContextEncapsulates the information that is required in order to register an area within an ASP.NET MVC application.
AssociatedMetadataProviderProvides an abstract class to implement a metadata provider.
AssociatedValidatorProviderProvides an abstract class for classes that implement a validation provider.
AsyncControllerProvides the base class for asynchronous controllers.
AsyncTimeoutAttributeRepresents an attribute that is used to set the timeout value, in milliseconds, for an asynchronous method.
AuthorizationContextEncapsulates the information that is required for using an AuthorizeAttribute attribute.
AuthorizeAttributeRepresents an attribute that is used to restrict access by callers to an action method.
BindAttributeRepresents an attribute that is used to provide details about how model binding to a parameter should occur.
BuildManagerCompiledViewRepresents the base class for views that are compiled by the BuildManager class before being rendered by a view engine.
BuildManagerViewEngineProvides a base class for view engines.
ByteArrayModelBinderMaps a browser request to a byte array.
ChildActionOnlyAttributeRepresents an attribute that is used to indicate that an action method should be called only as a child action.
ChildActionValueProviderRepresents a value provider for values from child actions.
ChildActionValueProviderFactoryRepresents a factory for creating value provider objects for child actions.
ClientDataTypeModelValidatorProviderReturns the client data-type model validators.
CompareAttributeProvides an attribute that compares two properties of a model.
ContentResultRepresents a user-defined content type that is the result of an action method.
ControllerProvides methods that respond to HTTP requests that are made to an ASP.NET MVC Web site.
ControllerActionInvokerRepresents a class that is responsible for invoking the action methods of a controller.
ControllerBaseRepresents the base class for all MVC controllers.
ControllerBuilderRepresents a class that is responsible for dynamically building a controller.
ControllerContextEncapsulates information about an HTTP request that matches specified RouteBase and ControllerBase instances.
ControllerDescriptorEncapsulates information that describes a controller, such as its name, type, and actions.
ControllerInstanceFilterProviderAdds the controller to the FilterProviderCollection instance.
CustomModelBinderAttributeRepresents an attribute that invokes a custom model binder.
DataAnnotationsModelMetadataProvides a container for common metadata, for the DataAnnotationsModelMetadataProvider class, and for the DataAnnotationsModelValidator class for a data model.
DataAnnotationsModelMetadataProviderImplements the default model metadata provider for ASP.NET MVC.
DataAnnotationsModelValidatorProvides a model validator.
DataAnnotationsModelValidator(Of TAttribute)Provides a model validator for a specified validation type.
DataAnnotationsModelValidatorProviderImplements the default validation provider for ASP.NET MVC.
DataErrorInfoModelValidatorProviderProvides a container for the error-information model validator.
DefaultControllerFactoryRepresents the controller factory that is registered by default.
DefaultModelBinderMaps a browser request to a data object. This class provides a concrete implementation of a model binder.
DefaultViewLocationCacheRepresents a memory cache for view locations.
DependencyResolverProvides a registration point for dependency resolvers that implement IDependencyResolver or the Common Service Locator IServiceLocator interface.
DependencyResolverExtensionsProvides a type-safe implementation of GetService and GetServices.
DictionaryValueProvider(Of TValue)Represents the base class for value providers whose values come from a collection that implements the IDictionary(Of TKey, TValue) interface.
EmptyModelMetadataProviderProvides an empty metadata provider for data models that do not require metadata.
EmptyModelValidatorProviderProvides an empty validation provider for models that do not require a validator.
EmptyResultRepresents a result that does nothing, such as a controller action method that returns nothing.
ExceptionContextProvides the context for using the HandleErrorAttribute class.
ExpressionHelperProvides a helper class to get the model name from an expression.
FieldValidationMetadataProvides a container for client-side field validation metadata.
FileContentResultSends the contents of a binary file to the response.
FilePathResultSends the contents of a file to the response.
FileResultRepresents a base class that is used to send binary file content to the response.
FileStreamResultSends binary content to the response by using a Stream instance.
FilterRepresents a metadata class that contains a reference to the implementation of one or more of the filter interfaces, the filter's order, and the filter's scope.
FilterAttributeRepresents the base class for action and result filter attributes.
FilterAttributeFilterProviderDefines a filter provider for filter attributes.
FilterInfoEncapsulates information about the available action filters.
FilterProviderCollectionRepresents the collection of filter providers for the application.
FilterProvidersProvides a registration point for filters.
FormCollectionContains the form value providers for the application.
FormContextEncapsulates information that is required in order to validate and process the input data from an HTML form.
FormValueProviderRepresents a value provider for form values that are contained in a NameValueCollection object.
FormValueProviderFactoryRepresents a class that is responsible for creating a new instance of a form-value provider object.
GlobalFilterCollectionRepresents a class that contains all the global filters.
GlobalFiltersRepresents the global filter collection.
HandleErrorAttributeRepresents an attribute that is used to handle an exception that is thrown by an action method.
HandleErrorInfoEncapsulates information for handling an error that was thrown by an action method.
HiddenInputAttributeRepresents an attribute that is used to indicate whether a property or field value should be rendered as a hidden input element.
HtmlHelperRepresents support for rendering HTML controls in a view.
HtmlHelper(Of TModel)Represents support for rendering HTML controls in a strongly typed view.
HttpDeleteAttributeRepresents an attribute that is used to restrict an action method so that the method handles only HTTP DELETE requests.
HttpFileCollectionValueProviderRepresents a value provider to use with values that come from a collection of HTTP files.
HttpFileCollectionValueProviderFactoryRepresents a class that is responsible for creating a new instance of an HTTP file collection value provider object.
HttpGetAttributeRepresents an attribute that is used to restrict an action method so that the method handles only HTTP GET requests.
HttpNotFoundResultDefines an object that is used to indicate that the requested resource was not found.
HttpPostAttributeRepresents an attribute that is used to restrict an action method so that the method handles only HTTP POST requests.
HttpPostedFileBaseModelBinderBinds a model to a posted file.
HttpPutAttributeRepresents an attribute that is used to restrict an action method so that the method handles only HTTP PUT requests.
HttpRequestExtensionsExtends the HttpRequestBase class that contains the HTTP values that were sent by a client during a Web request.
HttpStatusCodeResultProvides a way to return an action result with a specific HTTP response status code and description.
HttpUnauthorizedResultRepresents the result of an unauthorized HTTP request.
JavaScriptResultSends JavaScript content to the response.
JsonResultRepresents a class that is used to send JSON-formatted content to the response.
JsonValueProviderFactoryEnables action methods to send and receive JSON-formatted text and to model-bind the JSON text to parameters of action methods.
LinqBinaryModelBinderMaps a browser request to a LINQ Binary object.
ModelBinderAttributeRepresents an attribute that is used to associate a model type to a model-builder type.
ModelBinderDictionaryRepresents a class that contains all model binders for the application, listed by binder type.
ModelBinderProviderCollectionProvides a container for model binder providers.
ModelBinderProvidersProvides a container for model binder providers.
ModelBindersProvides global access to the model binders for the application.
ModelBindingContextProvides the context in which a model binder functions.
ModelClientValidationEqualToRuleProvides a container for an equality validation rule that is sent to the browser.
ModelClientValidationRangeRuleProvides a container for a range-validation rule that is sent to the browser.
ModelClientValidationRegexRuleProvides a container for a regular-expression client validation rule that is sent to the browser.
ModelClientValidationRemoteRuleProvides a container for a remote validation rule that is sent to the browser.
ModelClientValidationRequiredRuleProvides a container for client validation for required field.
ModelClientValidationRuleProvides a base class container for a client validation rule that is sent to the browser.
ModelClientValidationStringLengthRuleProvides a container for a string-length validation rule that is sent to the browser.
ModelErrorRepresents an error that occurs during model binding.
ModelErrorCollectionA collection of ModelError instances.
ModelMetadataProvides a container for common metadata, for the ModelMetadataProvider class, and for the ModelValidator class for a data model.
ModelMetadataProviderProvides an abstract base class for a custom metadata provider.
ModelMetadataProvidersProvides a container for the current ModelMetadataProvider instance.
ModelStateEncapsulates the state of model binding to a property of an action-method argument, or to the argument itself.
ModelStateDictionaryRepresents the state of an attempt to bind a posted form to an action method, which includes validation information.
ModelValidationResultProvides a container for a validation result.
ModelValidatorProvides a base class for implementing validation logic.
ModelValidatorProviderProvides a list of validators for a model.
ModelValidatorProviderCollectionProvides a container for a list of validation providers.
ModelValidatorProvidersProvides a container for the current validation provider.
MultiSelectListRepresents a list of items that users can select more than one item from.
MvcFilterWhen implemented in a derived class, provides a metadata class that contains a reference to the implementation of one or more of the filter interfaces, the filter's order, and the filter's scope.
MvcHandlerSelects the controller that will handle an HTTP request.
MvcHtmlStringRepresents an HTML-encoded string that should not be encoded again.
MvcHttpHandlerVerifies and processes an HTTP request.
MvcRouteHandlerCreates an object that implements the IHttpHandler interface and passes the request context to it.
MvcWebRazorHostFactoryCreates instances of MvcWebPageRazorHost files.
NameValueCollectionExtensionsExtends a NameValueCollection object so that the collection can be copied to a specified dictionary.
NameValueCollectionValueProviderRepresents the base class for value providers whose values come from a NameValueCollection object.
NoAsyncTimeoutAttributeProvides a convenience wrapper for the AsyncTimeoutAttribute attribute.
NonActionAttributeRepresents an attribute that is used to indicate that a controller method is not an action method.
OutputCacheAttributeRepresents an attribute that is used to mark an action method whose output will be cached.
ParameterBindingInfoEncapsulates information for binding action-method parameters to a data model.
ParameterDescriptorContains information that describes a parameter.
PartialViewResultRepresents a base class that is used to send a partial view to the response.
PreApplicationStartCodeProvides a registration point for ASP.NET Razor pre-application start code.
QueryStringValueProviderRepresents a value provider for query strings that are contained in a NameValueCollection object.
QueryStringValueProviderFactoryRepresents a class that is responsible for creating a new instance of a query-string value-provider object.
RangeAttributeAdapterProvides an adapter for the RangeAttribute attribute.
RazorViewRepresents the class used to create views that have Razor syntax.
RazorViewEngineRepresents a view engine that is used to render a Web page that uses the ASP.NET Razor syntax.
RedirectResultControls the processing of application actions by redirecting to a specified URI.
RedirectToRouteResultRepresents a result that performs a redirection by using the specified route values dictionary.
ReflectedActionDescriptorContains information that describes a reflected action method.
ReflectedControllerDescriptorContains information that describes a reflected controller.
ReflectedParameterDescriptorContains information that describes a reflected action-method parameter.
RegularExpressionAttributeAdapterProvides an adapter for the RegularExpressionAttribute attribute.
RemoteAttributeProvides an attribute that uses the jQuery validation plug-in remote validator.
RequiredAttributeAdapterProvides an adapter for the RequiredAttributeAttribute attribute.
RequireHttpsAttributeRepresents an attribute that forces an unsecured HTTP request to be re-sent over HTTPS.
ResultExecutedContextProvides the context for the OnResultExecuted method of the ActionFilterAttribute class.
ResultExecutingContextProvides the context for the OnResultExecuting method of the ActionFilterAttribute class.
RouteCollectionExtensionsExtends a RouteCollection object for MVC routing.
RouteDataValueProviderRepresents a value provider for route data that is contained in an object that implements the IDictionary(Of TKey, TValue) interface.
RouteDataValueProviderFactoryRepresents a factory for creating route-data value provider objects.
SelectListRepresents a list that lets users select one item.
SelectListItemRepresents the selected item in an instance of the SelectList class.
SessionStateAttributeSpecifies the session state of the controller.
SessionStateTempDataProviderProvides session-state data to the current TempDataDictionary object.
StringLengthAttributeAdapterProvides an adapter for the StringLengthAttribute attribute.
TempDataDictionaryRepresents a set of data that persists only from one request to the next.
TemplateInfoEncapsulates information about the current template context.
UrlHelperContains methods to build URLs for ASP.NET MVC within an application.
UrlParameterRepresents an optional parameter that is used by the MvcHandler class during routing.
ValidatableObjectAdapterProvides an object adapter that can be validated.
ValidateAntiForgeryTokenAttributeRepresents an attribute that is used to prevent forgery of a request.
ValidateInputAttributeRepresents an attribute that is used to mark action methods whose input must be validated.
ValueProviderCollectionRepresents the collection of value-provider objects for the application.
ValueProviderDictionaryObsolete. Represents a dictionary of value providers for the application.
ValueProviderFactoriesRepresents a container for value-provider factory objects.
ValueProviderFactoryRepresents a factory for creating value-provider objects.
ValueProviderFactoryCollectionRepresents the collection of value-provider factories for the application.
ValueProviderResultRepresents the result of binding a value (such as from a form post or query string) to an action-method argument property, or to the argument itself.
ViewContextEncapsulates information that is related to rendering a view.
ViewDataDictionaryRepresents a container that is used to pass data between a controller and a view.
ViewDataDictionary(Of TModel)Represents a container that is used to pass strongly typed data between a controller and a view.
ViewDataInfoEncapsulates information about the current template content that is used to develop templates and about HTML helpers that interact with templates.
ViewEngineCollectionRepresents a collection of view engines that are available to the application.
ViewEngineResultRepresents the result of locating a view engine.
ViewEnginesRepresents a collection of view engines that are available to the application.
ViewMasterPageRepresents the information that is needed to build a master view page.
ViewMasterPage(Of TModel)Represents the information that is required in order to build a strongly typed master view page.
ViewPageRepresents the properties and methods that are needed to render a view as a Web Forms page.
ViewPage(Of TModel)Represents the information that is required in order to render a strongly typed view as a Web Forms page.
ViewResultRepresents a class that is used to render a view by using an IView instance that is returned by an IViewEngine object.
ViewResultBaseRepresents a base class that is used to provide the model to the view and then render the view to the response.
ViewStartPageProvides an abstract class that can be used to implement a view start (master) page.
ViewTemplateUserControlProvides a container for TemplateInfo objects.
ViewTemplateUserControl(Of TModel)Provides a container for TemplateInfo objects.
ViewTypeRepresents the type of a view.
ViewUserControlRepresents the information that is needed to build a user control.
ViewUserControl(Of TModel)Represents the information that is required in order to build a strongly typed user control.
VirtualPathProviderViewEngineRepresents an abstract base-class implementation of the IViewEngine interface.
WebFormViewRepresents the information that is needed to build a Web Forms page in ASP.NET MVC.
WebFormViewEngineRepresents a view engine that is used to render a Web Forms page to the response.
WebViewPageRepresents the properties and methods that are needed in order to render a view that uses ASP.NET Razor syntax.
WebViewPage(Of TModel)Represents the properties and methods that are needed in order to render a view that uses ASP.NET Razor syntax.

Interfaces

InterfaceDescription
IActionFilterDefines the methods that are used in an action filter.
IActionInvokerDefines the contract for an action invoker, which is used to invoke an action in response to an HTTP request.
IAuthorizationFilterDefines the methods that are required for an authorization filter.
IClientValidatableProvides a way for the ASP.NET MVC validation framework to discover at run time whether a validator has support for client validation.
IControllerDefines the methods that are required for a controller.
IControllerActivatorProvides fine-grained control over how controllers are instantiated using dependency injection.
IControllerFactoryDefines the methods that are required for a controller factory.
IDependencyResolverDefines the methods that simplify service location and dependency resolution.
IExceptionFilterDefines the methods that are required for an exception filter.
IFilterProviderProvides an interface for finding filters.
IMetadataAwareProvides an interface for exposing attributes to the AssociatedMetadataProvider class.
IModelBinderDefines the methods that are required for a model binder.
IModelBinderProviderDefines methods that enable dynamic implementations of model binding for classes that implement the IModelBinder interface.
IMvcFilterDefines members that specify the order of filters and whether multiple filters are allowed.
IResultFilterDefines the methods that are required for a result filter.
IRouteWithAreaAssociates a route with an area in an ASP.NET MVC application.
ITempDataProviderDefines the contract for temporary-data providers that store data that is viewed on the next request.
IUnvalidatedValueProviderRepresents an IValueProvider interface that can skip request validation.
IValueProviderDefines the methods that are required for a value provider in ASP.NET MVC.
IViewDefines the methods that are required for a view.
IViewDataContainerDefines the methods that are required for a view data dictionary.
IViewEngineDefines the methods that are required for a view engine.
IViewLocationCacheDefines the methods that are required in order to cache view locations in memory.
IViewPageActivatorProvides fine-grained control