Visual Basic .NET Programming
Understanding ASP.NET * Property of STI Page 1 of 8
TOPIC TITLE: Understanding ASP.NET Specific Objectives: At the end of the topic session, the students are expected to: Cognitive:
1. Explain the benefits of ASP.NET and its various libraries in application development.
2. Explain the ASP.NET objects. 3. Explain the basic concepts in managing and securing ASP.NET
applications. 4. Familiarize the steps in creating a new web application project.
Affective:
1. Listen to others with respect. 2. Participate in class discussions actively.
MATERIALS/EQUIPMENT:
o topic slides o OHP
TOPIC PREPARATION:
o Have the students research on ASP.NET. o It is imperative for the instructor to incorporate various kinds of
teaching strategies while discussing the suggested topics. The instructor may use the suggested learning activities below to facilitate a thorough and creative discussion of the topic.
o Prepare the slides to be presented in the class.
TOPIC PRESENTATION: The topic will revolve around ASP.NET. This will be the suggested flow of discussion for the course topic:
1. Ask the students to share their research about ASP.NET. 2. Discuss ASP.NET by explaining what it is and explaining the
benefits of ASP.NET and its various libraries in application development.
3. Explain the ASP.NET objects. 4. Explain the basic concepts in managing and securing ASP.NET
applications. 5. Demonstrate how to create a new web application project.
Visual Basic .NET Programming
Understanding ASP.NET * Property of STI Page 2 of 8
What is ASP.NET Page 1 of 10
Understanding ASP.NET
Visual Basic .NET Programming
* Property of STIPage 1 of 10
What is ASP.NETWhat is ASP.NETWhat is ASP.NETWhat is ASP.NET
� a web programming technology developed
by Microsoft
� used to create dynamic web pages
� Dynamic web page
• a web page where page content is
dynamically generated for every request
� successor to ASP
What is ASP.NET ASP.NET is a web programming technology developed by Microsoft that is used to create dynamic web pages. Dynamic web page refers to a web page where page content is dynamically generated for every request. ASP.NET is the successor to Active Server Pages (ASP) which was the earlier dynamic web page creation technology. ASP.NET introduces many improvements (to be discussed on the next slide) that allow developers to create powerful, secure, scalable and manageable web application. [What is ASP.NET, Page 1 of 10]
Overview of ASP.NET Page 2 of 10
Understanding ASP.NET
Visual Basic .NET Programming
* Property of STIPage 2 of 10
Overview of ASP.NETOverview of ASP.NETOverview of ASP.NETOverview of ASP.NET
� benefits of using ASP.NET
� Code-behind support
� Code can be compiled from any
Microsoft .NET-compatible language
� ASP.NET pages run side-by-side with ASP
� Automatic support for multiple browsers
� ASP.NET namespaces provide rich
functionality
� Built-in support for web services
Overview of ASP.NET Listed below are the benefits of using ASP.NET to developers.
• Code-behind support
• Code can be compiled from any Microsoft .NET-compatible language
• ASP.NET pages run side-by-side with ASP.
• Automatic support for multiple browsers.
• ASP.Net namespaces provide rich functionality.
• Built-in support for web services. ASP.NET uses an event-driven programming model similar to Visual Basic. Since web forms use the drag and drop style of page creation, it is easy to modify the code behind the controls on Web forms. This code resides in a separate file to separate the content from the code. ASP.NET can be created by using any of the .NET compatible languages such as Visual Basic .NET, C# and Microsoft Jscript. For this reason, ASP.NET is compiled rather than interpreted. ASP.NET uses the .asp file extension to allow ASP.NET pages to run side-by-side with existing ASP pages on the Internet Information Services (IIS). The Internet Information Services (IIS) provides Web server capabilities over an intranet, the Internet or extranet. Many organizations use IIS to host and manage Web pages on the Internet, to host and manage FTP sites and to route mail using the Simple Mail Transfer Protocol (SMTP). The ASP.NET runtime will only process files with .aspx extensions and .asp files will be processed with the ASP engine. ASP.NET applications can be rendered differently to suit the capabilities of different browsers. Applications can be created in the same way for both a simple browser and an enhanced browser that supports dynamic HTML (DHTML). The ASP.NET framework provides several namespaces that allow developers to use powerful built-in features. Lastly, ASP.Net contains support for Web services. [Overview of ASP.NET, Page 2 of 10]
Visual Basic .NET Programming
Understanding ASP.NET * Property of STI Page 3 of 8
The Response and Request Objects Page 3 of 10
Understanding ASP.NET
Visual Basic .NET Programming
* Property of STIPage 3 of 10
The Response and The Response and The Response and The Response and Request ObjectsRequest ObjectsRequest ObjectsRequest Objects
� The HttpRequest class is used to
request information such as query string
and form values from a client browser.
Dim strName As String =
Request.Form(“txtInput”)
� The HttpResponse class is used to send
information such as HTML output and
browser redirection.
Response.Write(“<H2> The date is:
“ & Now.Date & “</H2>”)
The Response and Request Objects
The System.Web namespace is used to access classes that provide the base functionality of ASP.NET applications. Two of the objects in this
namespace are Response and Request which are used to communicate between the client browser and the server computer.
The HttpRequest class is used to request information such as query string and form values from a client browser. An instance of this class is
the Request property of the current web form.
The following example shows how to request the value currently stored
in a text box named txtInput, on a Web Form. Dim strName As String = Request.Form(“txtInput”)
On the other hand, the HttpResponse class is used to send
information such as HTML output and browser redirection. An instance
of this class called Response of the current web form can be used for this purpose.
The following example shows how the Response object is used to display the current date. Response.Write(“<H2> The date is: “ & Now.Date &
“</H2>”)
[The Response and Request Objects, Page 3 of 10]
Maintaining Client-Side State Page 4 of 10
Understanding ASP.NET
Visual Basic .NET Programming
* Property of STIPage 4 of 10
Maintaining Maintaining Maintaining Maintaining ClientClientClientClient----Side StateSide StateSide StateSide State
� ASP.Net provides a functionality to
maintain state for a Web page when needed.
� HTTP is a stateless protocol. It cannot maintain a state over a period of time.
� Ways to maintain client-side state:
� View State
� Control State
� Cookies
� Hidden Fields
� Query Strings
Maintaining Client-Side State ASP.Net provides a functionality to maintain state for a Web page when needed. Web pages are stateless because the system the Web operates is facilitated by HTTP and HTTP is a stateless protocol. It cannot maintain a state over a period of time. It can only remember one request at a time and once a response is made, the state is terminated. If the browser wishes to make another request, the whole process has to start again. This mechanism allows more users to connect to a given server over a period of time. Web pages are recreated each time a client browser makes a request.
The state of the page and its content can be saved by using the View
State property or the StateBag class. A hidden variable is used to store information as part of the page that is sent to the browser. This information is then retrieved when the page is posted back to the server.
The System.Web.UI.Control class implements a property named
EnableViewState to save the state and content of the page. If this
property is set to True (the default) any control that inherits from this class will maintain view state and the view state of any constituent
control. If this is set to False, the page cannot be posted back to itself thus increasing performance by reducing the size of the page.
The StateBag class is used to maintain additional cross-request
information. Its instance is called ViewState that allows you to store
and retrieve extra information. The following example shows how to create a stateful property called PageVisitCount and assign a value
of 1.
Visual Basic .NET Programming
Understanding ASP.NET * Property of STI Page 4 of 8
ViewState(“PageVisitCount”) = 1 Response.Write(ViewState(:PageVisitCount”))
When the page is sent to the browser, a hidden variable is included as shown below. <input type=”hidden” name=”_VIEWSTATE” value=d0…NnU=”
/>
A hidden input in the HTML document contains the data of the controls
on this page. It is always named _VIEWSTATE and stores the control’s data as an encoded string as shown in the value attribute shown above.
Aside from ViewState, there are a number of ways to track individual
clients. One of these is the use of cookies. A cookie refers to textual information stored by a Web site on client’s computer. It is used to track the actions of the visitor. Cookie is created the first time the user visits the Web site. It collects information about the user and stored anonymously. The cookie is reactivated every time the user revisits the site. Another technique that can be used for tracking includes the use of input form elements of type “hidden.” ASP.NET allows you to store
information in a HiddenField control, which renders as a standard HTML hidden field. Using this element, a Web Form can store tracking information into a form in the Web page. When a page is submitted to the server, its contents are sent in the HTTP form collection along with the values of other controls. It returns to the client in response to a request. Control state is another feature in ASP.NET that allows developers to store control-state data in order for a control to work properly. The
ViewState property can be used for this purpose but it can be turned
off at a page level by developers, effectively breaking your control. To
use control state, the ControlState property can be used for this purpose. It allows you to persist property information that is specific to a
control and cannot be turned off like the ViewState property. One of the simplest ways of maintaining client-side state is query string. A query string is information that is appended to the end of a page URL. An example of this is given below: http://s6.ikariam.org/index.php?view=city&id=205438 In the URL path above, the query string starts with a question mark (?) and includes two attribute/value pairs, one called "view" and the other called "id." The limitation of query string is that some browsers and client devices impose a 20103-character limit on the length of the URL. Also, there is security issue in using query string. Information that is passed in a query string can be tampered with by a malicious user. [Maintaining Client-Side State, Page 4 of 10]
Maintaining Server-Side State
The HttpApplicationState class is used to store application-level
information. The Application object can be used to share global data
Visual Basic .NET Programming
Understanding ASP.NET * Property of STI Page 5 of 8
Maintaining Server-Side State Page 5 of 10
Understanding ASP.NET
Visual Basic .NET Programming
* Property of STIPage 5 of 10
Maintaining Maintaining Maintaining Maintaining ServerServerServerServer----Side StateSide StateSide StateSide State
� HttpApplicationState class
� used to store application-level information
� Application object can be used to share
global data across the application
Application(“WebApp_StartTime”) = Now
…
Response.Write(“Application Started:
“ &
Application(“WebApp_StartTime”))
Maintaining Server-Side State Page 6 of 10
Understanding ASP.NET
Visual Basic .NET Programming
* Property of STIPage 6 of 10
Maintaining Maintaining Maintaining Maintaining ServerServerServerServer----Side StateSide StateSide StateSide State
� HttpSessionState class
� Session object is used by server to
maintain session state information
Session(“WebSession_StartTime”) = Now
…
Response.write(“Session started: “ &
Session(“WebSession_StartTime”))
� Profile Properties feature
� allows programmers to define and store
user setting to be used in the Web
application
� allows easy management of user
information
� makes the user information available using
a strongly typed API
across the application. It has a default property named Item that is used to store object and retrieve a collection object. The following example shows how to store the start time of the application and then display the information at a later stage. Application(“WebApp_StartTime”) = Now
…
Response.Write(“Application Started: “ &
Application(“WebApp_StartTime”))
Session state is similar to application state. The only difference is that it is scoped to the current browser session. If different users use the application, each user session will have its own session state. In addition, if a user leaves the application and then returns later, the second user session will have a different session state from the first. The server maintains session state information using the Session object
which is an instance of the HttpSessionState class. The session
ends when a time out occurs or when the user moves away from the ASP.Net application. The following example shows how to store the start time of the session and then display the information at a later stage.
Session(“WebSession_StartTime”) = Now
…
Response.write(“Session started: “ &
Session(“WebSession_StartTime”))
Another way to maintain application-level information is the user profile properties feature of ASP.NET. ASP.NET 2.0 introduced the profile properties feature which allows programmers to define and store user setting to be used in the Web application. This feature is similar to session state, except that the profile data is not lost when a user's session expires. It allows easy management of user information. In addition, the profile makes the user information available using a strongly typed API that can be accessed from anywhere in the application.
[Maintaining Server-Side State, Pages 5-6 of 10]
Application and Session State Page 7 of 10
Understanding ASP.NET
Visual Basic .NET Programming
* Property of STIPage 7 of 10
Application and Application and Application and Application and Session StateSession StateSession StateSession State
� used to store information that can be
accessed by other components within the Web application
� used to share resources and state
management
� HttpApplicationState class contains
an Application property that provides easy
access to variables
� Application-level events:
• Application_Start
• Application_BeginRequest
• Application_EndRequest
• Application_End
� HttpSessionState class allows you to
store session attributes
� Session-level events:
• Session_Start
• Session_End
Application and Session State The application and session states are used to store information that can be accessed by other components within the Web application. They are used to share resources and state management. It can be initialized using the Global.aspx application file.
The HttpApplicationState class contains an Application property that provides easy access to variables. It also provides several events to initialize state at the application level:
• Application_Start – It is used to initialize application-state information. It is activated when the first user attempts to gain access the Web application. Any subsequent requests will not activate this event.
• Application_BeginRequest – This event is invoked when
a request for URL is received from a client application.
Visual Basic .NET Programming
Understanding ASP.NET * Property of STI Page 6 of 8
Application and Session State Page 8 of 10
Understanding ASP.NET
Visual Basic .NET Programming
* Property of STIPage 8 of 10
Application and Application and Application and Application and Session StateSession StateSession StateSession State
� Example:
Sub Application_Start(ByVal sender As
Object, ByVal e As EventArgs)
‘Initialize the application level
counter
Application(“SessionCounter”) = 0
End Sub
Sub Session_Start(ByVal sender As
Object, ByVal e As EventArgs)
‘Increment the application level
counter
Application(“SessionCounter”) =
Application(“SeesionCounter”) + 1
‘Assign new value to session-level data
Session(“StartTime”) = Now
End Sub
Sub Session_End(ByVal sender As Object,
ByVal e As EventArgs)
‘Decrement the application level
counter
Application(“SessonCounter”) =
Application(“SeesionCounter”) - 1
End Sub
• Application_EndRequest – This event is triggered when a request for URL has been completed.
• Application_End – It is activated when the last instance of
the Global class is destroyed so that any final resources can be cleaned up.
We said that HTTP is a stateless protocol. Each request and response message connection is independent of any others. This is significant because from one request to another request (from the same user) the HTTP server forgets the previous request. Therefore, the Web container must create a mechanism to store session information for a particular user. ASP.NET must support session objects, which store attributes that are unique to a specific client but exist across multiple HTTP requests.
ASP.NET provides an HttpSessionState class that allows you to store session attributes. ASP,Net provides the following session-level events:
• Session_Start – This event is activated at the beginning of a
new client session and is useful for initializing session-state information.
• Sesson_End – This event is activated when a client session
ends after a period of inactivity or if the Session.Abandon method is invoked as part of the application code.
The following example shows the application-state and session-state events. Sub Application_Start(ByVal sender As Object, ByVal e
As EventArgs)
‘Initialize the application level counter
Application(“SessionCounter”) = 0
End Sub
Sub Session_Start(ByVal sender As Object, ByVal e As
EventArgs)
‘Increment the application level counter
Application(“SessionCounter”) =
Application(“SeesionCounter”) + 1
‘Assign new value to session-level data
Session(“StartTime”) = Now
End Sub
Sub Session_End(ByVal sender As Object, ByVal e As
EventArgs)
‘Decrement the application level counter
Application(“SessonCounter”) =
Application(“SeesionCounter”) - 1
End Sub
[Application and Session State, Pages 7-8 of 10]
Web Forms Web Forms file have the file extension .aspx that contains the GUI of the Web page. Every ASPX file has a corresponding code-behind file for the code that executes as a result of user interaction. The separation of file is because of the fact that HTML tags with user-interface event handling script are difficult to maintain.
Visual Basic .NET Programming
Understanding ASP.NET * Property of STI Page 7 of 8
Web Forms Page 9 of 10
Understanding ASP.NET
Visual Basic .NET Programming
* Property of STIPage 9 of 10
Web FormsWeb FormsWeb FormsWeb Forms
� have the file extension .aspx
� every ASPX file has a corresponding code-behind file for the code
� .aspx file contains definitions for the visual
elements of the Web Form as well as the
link to the code-behind module that
contains the logic and event-handler of the
Web Form
� code-behind file is a file with an .aspx.vb
extension
� This class allow you to do the following:
� Create methods or event-handling code.
� Provide access to many of the commonly
used ASP.NET objects.
� Provide event handlers
The .aspx file contains definitions for the visual elements of the Web Form such as HTML tags, Web controls, client-side script and static text. It also contains the link to the code-behind module that contains the logic and event-handler of the Web Form. Visual Studio IDE has Web Forms Designer to create a page by using drag-drop techniques. Similarly, it has a HTML view to edit the HTML tags. The code-behind file is a file with an .aspx.vb extension in an ASP.NET project using Visual Basic .NET. The server-side and event-handling code is automatically placed in the code-behind file of a Web Form. The
file contains a class that is referenced by the @Page directive within
the .aspx file of the Web Form. This class allow you to do the following:
• Create methods or event-handling code.
• Provide access to many of the commonly used ASP.NET objects.
• Provide event handlers [Web Forms, Page 9 of 10]
Demo: Creating a New Web ApplicationProject Page 10 of 10
Understanding ASP.NET
Visual Basic .NET Programming
* Property of STIPage 10 of 10
Demo: Creating a New Demo: Creating a New Demo: Creating a New Demo: Creating a New Web Application ProjectWeb Application ProjectWeb Application ProjectWeb Application Project
� Open Visual Studio 2005.
� File > New Web Site
� Select ASP.NET Web Site.
� Set Location to File System. Then, set
the location of your Web application.
� Set Language to Visual Basic. Click OK.
� Rename the aspx file named Default.aspxto ASPDemo.aspx.
� Click on the Source tab.
� On the design view, My first ASP.NET Web page!.
� Right-click on the project name and select
View in Browser.
� Simply click Yes. A browser will open and
displays the “My first ASP.NET Web
page!.” in the Web page.
Demo: Creating a New Web Application Project Demonstrate to your students how to create a new Web application project.
1. Open Visual Studio 2005. 2. In the menu bar, click File and then click New Web Site to
display the New Web Site window. 3. Select ASP.NET Web Site from the New Web Site window. In
the Location information, make sure that the drop-down list is set to File System. Then, set the location of your Web application.
4. For the Language information, select Visual Basic. Click OK. 5. In the Solutions Explorer, explain to your students the different
files created for the project. 6. Rename the aspx file named Default.aspx to ASPDemo.aspx. 7. On the left is the Designer window. By default, it displays the
page in design view. Click on the Source tab. Explain to your students the information in the source view.
8. On the design view, My first ASP.NET Web page!. 9. In the Solutions Explorer, right-click on the project name and
select View in Browser. 10. Simply click Yes if a dialog box appears asking you to save
changes being made. A browser will open and displays the “My first ASP.NET Web page!.” in the Web page.
NOTE: Notice that an icon is added in the Notification Area of the Task Bar. This icon is the ASP.NET Development Server. The ASP.NET Development Server is used for testing and running Web sites. It assigns different port number for each Web application.
[Demo: Creating a New Web Application Project, Pages 10 of 10]
EVALUATION/GENERALIZATION:
• Microsoft’s ASP.NET technology is used for Web-based application development.
• The Web Form file represents the Web page that is sent to the client browser.
Visual Basic .NET Programming
Understanding ASP.NET * Property of STI Page 8 of 8
• Every ASPX file created in Visual Studio has a corresponding class written in a .NET compliant language.
• An HTML control is executed at the server when the control’s
runat attribute is set to “server”.
• The namespace System.Web contains classes that manage
client request and server response.
• The EnableViewState attribute determines whether a Web
control’s state persists when a post back occurs.
REFERENCES:
• Microsoft Official Course, (2002), 2373B: Programming with Microsoft Visual Basic .NET, Microsoft Corporation
• Holzner, Steven, (2003), Sams teach yourself Microsoft Visual Basic.Net 2003 in 21 days, USA, Sams Publishing
• Liberty, Jesse, (2003), Learning Visual Basic .NET, USA, O'Reilly & Associates, Inc