Creating ASP.NET Pages

function OpenWin(url, w, h) { if(!w) w = 400; if(!h) h = 300; window.open(url, "_new", "width=" + w + ",height=" + h + ",menubar=no,toobar=no,scrollbars=yes", true); } function Print() { window.focus(); if(window.print) { window.print(); window.setTimeout('window.close();',5000); } }
Team-Fly    

Special Edition Using Microsoft® Visual Basic® .NET
By Brian Siler, Jeff Spotts
Table of Contents
Chapter 17.  Using Active Server Pages.NET


ASP.NET pages have an .aspx extension. To get started with ASP.NET you'll need an editor. As we mentioned earlier, Notepad is a good editor for simple Web pages and will suffice for the ASP.NET samples in this chapter. As you will see in later chapters, Visual Studio provides the means for working with ASP.NET pages as you would forms in a project. However, you can still use Visual Studio's HTML editor to edit .aspx files outside a project. To do this, start with an HTML file and then save it with an .aspx extension. While in the HTML editing mode, you can type Visual Basic code directly into the file.

Understanding Script Tags

To separate your VB code from HTML tags in an ASP.NET page, you use script tags. While processing the page, Internet Information server executes only the code within the special script tags, two forms of which are listed here:

  • The HTML <SCRIPT> tag with the RUNAT="SERVER" attribute indicates declarative server-side code.

  • The <% and %> tags mark the beginning and end of the server-side code to be executed.

The later syntax is shorter and is probably familiar to users of previous versions of ASP. However, ASP.NET has slightly different rules about the use of the different script tag forms. Starting with ASP.NET, you cannot use the short form if your code contains a custom function declaration. For example, the following code would work fine in ASP, but is no longer supported in ASP.NET:

 'THIS WILL NOT WORK IN ASP.NET!  <%     Sub Test()        Response.Write("Hey")     End Sub     Call Test()  %> 

Starting with ASP.NET, use the <SCRIPT> tag for function declarations and the <%and %> tags for statements to be executed during page processing. For example, you would have to rewrite the previous code to run in ASP.NET as follows:

 <SCRIPT LANGUAGE="VB" RUNAT="SERVER">     Sub Test()        Response.Write("Hey")     End Sub  </SCRIPT>  <%     Call Test()  %> 

Note

Starting in ASP.NET, you must use the longer <SCRIPT RUNAT="SERVER"> tag to define functions.


More information on migration from ASP to ASP.NET is available in the Quickstart Tutorials included with the .NET Framework SDK. You probably already have these samples installed on the machine on which you installed Visual Studio. To view this very useful ASP.NET information, open the following URL: http://localhost/quickstart.

Importing Namespaces

In Chapter 5, "Visual Basic Building Blocks," we introduced the concept of a namespaces and described how to use the Imports statement in a Visual Basic application to specify namespaces used within your program. You can also import namespaces in ASP.NET page, although the syntax is a little different:

 <% @Import Namespace = "System.IO" %> 

The previous line of code imports the System.IO namespace for use in the current ASP.NET page. Note that this statement must precede any variable declarations.

For more details on Importing a Namespace, p.113

Because ASP.NET includes the full power of the VB language, you can, of course, define your own namespaces and import them for use as an ASP application. By placing compiled DLLs in a bin subdirectory of an ASP.NET application, you can import a custom namespace. For more information, see the topic "Working with Business Objects" in the ASP.NET Quickstart tutorial.

Configuring Your ASP.NET Application

One initiative of the .NET framework was ease of setup and deployment through the use of user-editable configuration files. Configuration files, which can control many different settings in an .NET applications, are text-based XML files, which mean they are readable and can be edited easily by humans. For example, remote administration of ASP.NET applications can be performed, even if you do not have the IIS management software installed. By placing a file called web.config in a directory on your Web server, you can override the default configuration settings.

Note

The default settings for an application are inherited from the most recent configuration settings in the parent directory path. The default master configuration files for the machine are located in the \windows \Microsoft.Net\Framework\version \ CONFIG directory, where windows is your Windows directory and version is the version of the .NET Framework installed on the machine.


A configuration file starts with a <configuration> tag and contains several sections, each related to a specific configuration area. One of the first things you'll probably want to do with a configuration file is alter ASP.NET's custom error handling. In previous versions of ASP, when an unhandled error occurs it is sent to the client. However, ASP.NET allows you to easily substitute it with your own error page:

 <configuration>      <system.web>          <customErrors mode="On" defaultRedirect="errorpage.aspx"/>      </system.web>  </configuration> 

The third line of the previous configuration file sets the default error page to errorpage.aspx. This means if the code in the ASP.NET application throws an unhandled exception, the user will not see the error text. Instead, he will see the custom error page, which can be programmed to display a friendlier message:

 Response.Write ("An error has occurred in the following page:")  Response.Write (Request.QueryString("aspxerrorpath"))  Response.Write ("<BR>Please call tech support if you dare!") 

For troubleshooting and development purposes, you will probably want to set the custom error setting to Off. Another interesting use of a configuration is to store static, app-specific settings such as a connection string. To do so, simply add a section to your config.web file containing a key-value pair:

 <appSettings>    <add key="ConnectionString" value="server=BSSQL;database=ap;UID=apweb" />  </appSettings> 

The previous sample configuration text creates an application setting called ConnectionString. The following code can be used from any page in the same ASP.NET application to retrieve its value:

 Dim strConn As String  = ConfigurationSettings.AppSettings("ConnectionString") 

To learn more about the different types of configuration settings, see the help topic "Configuring .NET Framework Applications."


    Team-Fly    
    Top
     



    Special Edition Using Visual Basic. NET
    Special Edition Using Visual Basic.NET
    ISBN: 078972572X
    EAN: 2147483647
    Year: 2001
    Pages: 198

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