The Essentials


Before we get started, let s go back to the basics and review all the essentials you need to know for developing your own ASP.NET application. You probably know most of this already, but it ll serve as a great refresher and an excellent desk reference. You ll find a couple of nicely wrapped functions for working with cookies and the Session object here, too.

  • ASP.NET is part of the .NET Framework. Its predecessor was ASP. You need a minimum of Windows 2000/XP (or greater) with Internet Information Services 5.0 to run the ASP.NET portion of the .NET Framework.

  • Using Visual Studio .NET, you can create ASP.NET Web applications. Each Web application typically consists of one or more Web forms, which each represent interactive ASPX pages on your site.

  • To create a new Web application project in Visual Studio .NET, click File New Project, select the Visual Basic Projects project type, and choose the ASP.NET Web application template. Specify a Web address as the project location, and click on OK.

  • By default, a Web page opens in GridLayout , which positions controls absolutely on a page. To change this, select DOCUMENT in the Properties window and change the pageLayout property to FlowLayout .

  • To create a Web application using VB .NET, paint your Toolbox controls onto a Web form, set properties as appropriate, and add Visual Basic code to glue it all together.

  • The toolbox contains two types of controls for use on ASP.NET Web pages: ASP.NET server controls and HTML controls. The ASP.NET Web Form controls are the most powerful, rendering on the server and allowing full programmatic control. The HTML controls are simply regular browser objects, such as a Submit button; this is the sort of control you could add through the likes of Microsoft FrontPage.

  • Controls on ASP.NET Web pages don t have Name properties, as Windows applications do. Rather, they have an ID , which is exactly the same thing. It s the name you use to refer to the control in code.

  • ASP.NET (part of the .NET Framework) intercepts every ASPX page request or postback made to your server. ASP.NET takes the information given, processes it, runs any relevant code, and then returns its HTML results to the client. Typically, all code is run on the server side.

  • ASP.NET and its classes think before sending results back to the client. For example, if the client browser supports only HTML 3.2, it will attempt to send only pure HTML 3.2 back. If, however, the target client supports some of the more-advanced features you may have used in your pages, these are sent down the line.

  • A postback occurs when your user makes a request to your Web page (by, say, clicking on an ASP.NET Button control on the page) and the browser resubmits its data to the Web server (to, say, run your VB .NET code behind the button). However, the code in your page Load event runs regardless of whether this is the first time a page has loaded or whether this is a postback. You can ensure that code in your page Load event runs only once by using code similar to the following:

     If Page.IsPostBack = False Then      ' This code runs only once  End If 
  • If you ve used ASP in the past, you ll be happy to know the Application , Response , Request , Server , Session , and User objects are all still available to you. They may work slightly differently, but their core responsibilities remain the same.

  • In Web forms, the state of your controls is maintained automatically, unlike with regular ASP. This is done using a hidden __VIEWSTATE field. For example, the __VIEWSTATE may store the Text property of a Label control, ensuring that that property remains consistent as the user posts back a form, and so on.

  • The Session object allows you to store information about an individual s session with your application. For example, you may store a visitor s name, the last product they looked at, or the items in their online shopping basket . Following are common functions for storing and retrieving items from the Session object (for the Page argument, pass in your current page ”which you can refer to by using the Me keyword ”as a parameter):

     Public Sub AddItemToSession(ByVal ItemName As String, _      ByVal Item As Object, ByVal Page As System.Web.UI.Page)        ' Adds an item to the current session        ' Sample usage from within an ASP.NET page:        ' - AddItemToSession(Username, John, Me)      If IsItemInSession(Page, ItemName) Then         Page.Session.Item(ItemName) = Item      Else         Page.Session.Add(ItemName, Item)     End If  End Sub  Public Function GetItemFromSession(ByVal ItemName As String, _     ByVal Page As System.Web.UI.Page) As Object     ' Returns an item from the current session     If IsItemInSession(Page, ItemName) Then _       Return Page.Session.Item(ItemName)  End Function  Public Function IsItemInSession(ByVal ItemName As String, _     ByVal Page As System.Web.UI.Page) As Boolean     ' Returns a true if item with ItemName is in session     If Not Page.Session.Item(ItemName) Is Nothing Then Return True  End Function  Public Sub ClearSession(ByVal Page As System.Web.UI.Page)     ' Clears the session     Page.Session.Clear()  End Sub 
  • The Application object works in exactly the same way as the Session object. However, the difference is that, whereas the Session object can hold information for every unique visitor, the Application object holds one set of data for the whole application. So, use the Session object when you want to store and retrieve information for the current user, and use the Application object when you want to store and retrieve something for all users, such as a commonly accessed data set.

  • Information stored in the Session object expires after a default time period of twenty minutes. This period can be changed by editing the Web.config file. Data in the Application object stays alive while your actual ASP.NET Web application is running (that is, while your site actually has live visitors that haven t expired ).

  • You can add code to run when your ASP.NET Web application starts or ends or when a Session starts or ends through the Global.asax file in your application. You can also add controls to the Design mode of this file: adding a timer, for example, will enable you to run code at set intervals whenever your application is running. The Global.asax file is sometimes referred to as the ASP.NET application file , and you can see why.

  • Cookies allow you to store information more permanently, through your users machines. The following functions allow you to easily set and retrieve cookie values. This is a simple one-value cookie implementation, but more sample code can be found by looking up Cookies1 sample and Cookies2 sample in the help index.

     Public Sub SetCookie(ByVal Item As String, _     ByVal Value As String, ByVal Page As System.Web.UI.Page)     ' Sends a cookie down to the user     ' Sample usage from within an ASP.NET page:     '  SetCookie(Username, John, Me)     Dim objCookie As New System.Web.HttpCookie(Item, Value)        objCookie.Expires = DateTime.MaxValue     Page.Response.AppendCookie(objCookie)  End Sub  Public Function GetCookie(ByVal Item As String, _     ByVal Page As System.Web.UI.Page) As String     ' Retrieves a value from a cookie     ' Sample usage from within an ASP.NET page:     '  x = GetCookie(Username, Me)     Dim objCookies As System.Web.HttpCookieCollection        objCookies = Page.Request.Cookies     If Not objCookies Is Nothing Then        If Not (objCookies.Item(Item) Is Nothing) Then           Return objCookies.Item(Item).Value        End If     End If  End Function 
  • Query strings allow you to pass values in the URL of a Web page request. For example, a search at Google.com for Visual Basic will take you to http://www.google.com/search?q=Visual+Basic, where the query string is everything after the question mark. You can see here that we have one parameter called q that has a value of Visual Basic . (The + is an encoded space, which is sometimes referred to as a GET request.) You can use the Page.Request.QueryString object to examine the value of a parameter. The following function attempts to simplify the process:

     Public Function GetQueryString(ByVal ItemName As String, _     ByVal Page As System.Web.UI.Page) As String     ' Returns a string from the query string parameter or     ' nothing if unavailable (allowing you to check for Nothing)     ' Sample usage from within an ASP.NET page:     ' - GetQueryString(q, Me)     If Not Page.Request.QueryString.Item(ItemName) Is Nothing Then        Return Page.Request.QueryString.Item(ItemName).ToString     End If  End Function 
  • To encode a string for use in a query string, use the Server.UrlEncode function. To decode a query string parameter, use the Server.UrlDecode function.

  • To find out the server path of your Web application for, say, saving an uploaded file to a directory or reading a file on the server, use the Server.MapPath function. You ll also need to ensure that you have appropriate read/write permissions. To find out your server name, use the Server.MachineName property.

  • Debugging your Web application is as simple as debugging a regular Windows application. To mark a line for debugging, move to the line and press F9. To run your application, select Debug Start (or press F5) and interact through your Web browser. When your marked line of code is encountered , the application will pause and allow you to step through each line by selecting Debug Step Into (or pressing F8).

  • Compiling your application consists of selecting Build from the Build menu. This generates a DLL file in your project s Bin folder, which contains your code and powers your ASPX pages.

  • The default configuration for any solution is Debug, which creates debug files when you compile your application. Before releasing your final version, change the configuration (visible on the Standard toolbar or by selecting the menu item Build Configuration Manager) from Debug to Release, which removes all the debug symbols and optimizes your code further. It also stops unsightly code being displayed in your error messages.

  • ASP and ASP.NET Web pages can work alongside each other without problem. This means that porting a larger Web site can be easily broken down into stages, upgrading just the pages you have redeveloped.




The Ultimate VB .NET and ASP.NET Code Book
The Ultimate VB .NET and ASP.NET Code Book
ISBN: 1590591062
EAN: 2147483647
Year: 2003
Pages: 76
Authors: Karl Moore

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net