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
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:
<%
dim fname
fname=Request.Form("fname")
If fname<>"" Then
Response.Write("Hello " & fname & "!")
End If
%>
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)
Sunday, June 29, 2008
Asp Dotnet Webforms
All server controls must appear within a
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
--------------------------------------------------------------------------------
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:
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.
In the following example we declare a Button control in an .aspx file. A button click runs a subroutine which changes the text on the button:
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
--------------------------------------------------------------------------------
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:
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.
In the following example we declare a Button control in an .aspx file. A button click runs a subroutine which changes the text on the button:
Asp Dotnet 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()
%>
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:
Note: The Page_Load event contains no object references or event arguments!
Example
--------------------------------------------------------------------------------
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):
The example above will write the "The date and time is...." message only the first time the page is loaded. When a user clicks on the Submit button, the submit subroutine will write "Hello World!" to the second label, but the date and time in the first label will not change.
--------------------------------------------------------------------------------
ASP.NET - Event Handlers
Look at the following code:
<%
lbl1.Text="The date and time is " & now()
%>
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:
Note: The Page_Load event contains no object references or event arguments!
Example
--------------------------------------------------------------------------------
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):
The example above will write the "The date and time is...." message only the first time the page is loaded. When a user clicks on the Submit button, the submit subroutine will write "Hello World!" to the second label, but the date and time in the first label will not change.
Asp Dotnet Controls
Server controls are tags that are understood by the server.
--------------------------------------------------------------------------------
Limitations in Classic ASP
The listing below was copied from the previous chapter:
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
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:
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:
--------------------------------------------------------------------------------
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:
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:
--------------------------------------------------------------------------------
Limitations in Classic ASP
The listing below was copied from the previous chapter:
Hello W3Schools!
<%Response.Write(now())%>
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
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:
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:
--------------------------------------------------------------------------------
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:
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:
Asp Dot - Web pages
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:
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 .aspx extension.
This code displays our example as an ASP.NET page:
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:
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:
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.
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:
Hello W3Schools!
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 .aspx extension.
This code displays our example as an ASP.NET page:
Hello W3Schools!
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:
Hello W3Schools!
<%Response.Write(now())%>
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:
Hello W3Schools!
<%Response.Write(now())%>
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 Dotnet Install
ASP.NET is easy to install. Just follow the instructions below.
--------------------------------------------------------------------------------
What You Need
A Windows Computer
ASP.NET is a Microsoft technology. To run ASP.NET you need a computer capable of running Windows.
Windows 2000 or XP
If you are serious about developing ASP.NET applications you should install Windows 2000 Professional or Windows XP Professional.
In both cases, make sure you install the Internet Information Services (IIS) from the Add/Remove Windows components dialog.
Service Packs and Updates
Before ASP.NET can be installed on your computer, it is necessary to have all relevant service packs and security updates installed.
The easiest way to do this is to activate your Windows Internet Update. When you access the Windows Update page, you will be instructed to install the latest service packs and all critical security updates. For Windows 2000, make sure you install Service Pack 2. I will also recommend that you install Internet Explorer 6.
Read the note about connection speed and download time at the bottom of this page.
--------------------------------------------------------------------------------
Remove Your Beta Version
If you have a Beta version of ASP.NET installed, we recommend that you completely uninstall it. Or even better: start with a fresh Windows 2000 or XP installation.
--------------------------------------------------------------------------------
Install .NET
From your Windows Update you can now select to install the Microsoft .NET Framework.
After download, the .NET framework will install itself on your computer - there are no options to select for installation.
You should now be ready to develop your first ASP.NET application!
--------------------------------------------------------------------------------
The .NET Software Development Kit
If you have the necessary bandwidth to download over 130 MB, you might consider downloading the full Microsoft .NET Software Development Kit (SDK).
We fully recommend getting the SDK for learning more about .NET and for the documentation, samples, and tools included.
--------------------------------------------------------------------------------
Connection Speed and Download Time
If you have a slow Internet connection, you might have problems downloading large files like the service packs, the SDK and the latest version of Internet Explorer.
If download speed is a problem, our best suggestion is to get the latest files from someone else, from a colleague, from a friend, or from one of the CDs that comes with many popular computer magazines. Look for Windows 2000 Service Pack 2, Internet Explorer 6, and the Microsoft .NET Framework.
--------------------------------------------------------------------------------
What You Need
A Windows Computer
ASP.NET is a Microsoft technology. To run ASP.NET you need a computer capable of running Windows.
Windows 2000 or XP
If you are serious about developing ASP.NET applications you should install Windows 2000 Professional or Windows XP Professional.
In both cases, make sure you install the Internet Information Services (IIS) from the Add/Remove Windows components dialog.
Service Packs and Updates
Before ASP.NET can be installed on your computer, it is necessary to have all relevant service packs and security updates installed.
The easiest way to do this is to activate your Windows Internet Update. When you access the Windows Update page, you will be instructed to install the latest service packs and all critical security updates. For Windows 2000, make sure you install Service Pack 2. I will also recommend that you install Internet Explorer 6.
Read the note about connection speed and download time at the bottom of this page.
--------------------------------------------------------------------------------
Remove Your Beta Version
If you have a Beta version of ASP.NET installed, we recommend that you completely uninstall it. Or even better: start with a fresh Windows 2000 or XP installation.
--------------------------------------------------------------------------------
Install .NET
From your Windows Update you can now select to install the Microsoft .NET Framework.
After download, the .NET framework will install itself on your computer - there are no options to select for installation.
You should now be ready to develop your first ASP.NET application!
--------------------------------------------------------------------------------
The .NET Software Development Kit
If you have the necessary bandwidth to download over 130 MB, you might consider downloading the full Microsoft .NET Software Development Kit (SDK).
We fully recommend getting the SDK for learning more about .NET and for the documentation, samples, and tools included.
--------------------------------------------------------------------------------
Connection Speed and Download Time
If you have a slow Internet connection, you might have problems downloading large files like the service packs, the SDK and the latest version of Internet Explorer.
If download speed is a problem, our best suggestion is to get the latest files from someone else, from a colleague, from a friend, or from one of the CDs that comes with many popular computer magazines. Look for Windows 2000 Service Pack 2, Internet Explorer 6, and the Microsoft .NET Framework.
ASP.NET is easy to install. Just follow the instructions below.
--------------------------------------------------------------------------------
What You Need
A Windows Computer
ASP.NET is a Microsoft technology. To run ASP.NET you need a computer capable of running Windows.
Windows 2000 or XP
If you are serious about developing ASP.NET applications you should install Windows 2000 Professional or Windows XP Professional.
In both cases, make sure you install the Internet Information Services (IIS) from the Add/Remove Windows components dialog.
Service Packs and Updates
Before ASP.NET can be installed on your computer, it is necessary to have all relevant service packs and security updates installed.
The easiest way to do this is to activate your Windows Internet Update. When you access the Windows Update page, you will be instructed to install the latest service packs and all critical security updates. For Windows 2000, make sure you install Service Pack 2. I will also recommend that you install Internet Explorer 6.
Read the note about connection speed and download time at the bottom of this page.
--------------------------------------------------------------------------------
Remove Your Beta Version
If you have a Beta version of ASP.NET installed, we recommend that you completely uninstall it. Or even better: start with a fresh Windows 2000 or XP installation.
--------------------------------------------------------------------------------
Install .NET
From your Windows Update you can now select to install the Microsoft .NET Framework.
After download, the .NET framework will install itself on your computer - there are no options to select for installation.
You should now be ready to develop your first ASP.NET application!
--------------------------------------------------------------------------------
The .NET Software Development Kit
If you have the necessary bandwidth to download over 130 MB, you might consider downloading the full Microsoft .NET Software Development Kit (SDK).
We fully recommend getting the SDK for learning more about .NET and for the documentation, samples, and tools included.
--------------------------------------------------------------------------------
Connection Speed and Download Time
If you have a slow Internet connection, you might have problems downloading large files like the service packs, the SDK and the latest version of Internet Explorer.
If download speed is a problem, our best suggestion is to get the latest files from someone else, from a colleague, from a friend, or from one of the CDs that comes with many popular computer magazines. Look for Windows 2000 Service Pack 2, Internet Explorer 6, and the Microsoft .NET Framework.
--------------------------------------------------------------------------------
What You Need
A Windows Computer
ASP.NET is a Microsoft technology. To run ASP.NET you need a computer capable of running Windows.
Windows 2000 or XP
If you are serious about developing ASP.NET applications you should install Windows 2000 Professional or Windows XP Professional.
In both cases, make sure you install the Internet Information Services (IIS) from the Add/Remove Windows components dialog.
Service Packs and Updates
Before ASP.NET can be installed on your computer, it is necessary to have all relevant service packs and security updates installed.
The easiest way to do this is to activate your Windows Internet Update. When you access the Windows Update page, you will be instructed to install the latest service packs and all critical security updates. For Windows 2000, make sure you install Service Pack 2. I will also recommend that you install Internet Explorer 6.
Read the note about connection speed and download time at the bottom of this page.
--------------------------------------------------------------------------------
Remove Your Beta Version
If you have a Beta version of ASP.NET installed, we recommend that you completely uninstall it. Or even better: start with a fresh Windows 2000 or XP installation.
--------------------------------------------------------------------------------
Install .NET
From your Windows Update you can now select to install the Microsoft .NET Framework.
After download, the .NET framework will install itself on your computer - there are no options to select for installation.
You should now be ready to develop your first ASP.NET application!
--------------------------------------------------------------------------------
The .NET Software Development Kit
If you have the necessary bandwidth to download over 130 MB, you might consider downloading the full Microsoft .NET Software Development Kit (SDK).
We fully recommend getting the SDK for learning more about .NET and for the documentation, samples, and tools included.
--------------------------------------------------------------------------------
Connection Speed and Download Time
If you have a slow Internet connection, you might have problems downloading large files like the service packs, the SDK and the latest version of Internet Explorer.
If download speed is a problem, our best suggestion is to get the latest files from someone else, from a colleague, from a friend, or from one of the CDs that comes with many popular computer magazines. Look for Windows 2000 Service Pack 2, Internet Explorer 6, and the Microsoft .NET Framework.
Subscribe to:
Posts (Atom)