ASP.NET Objects

Working in .NET framework and ASP.NET means you will interact with many objects. We're not covering every object available because that would take a couple of books. However, we are going to hit on the most common that you'll be using in ASP.NET. By now you should be familiar with the Response and Request objects, which are used in all the programming environments that are available to Dreamweaver MX and which are covered in Part III of this book. Because most of the ASP.NET objects are similar to the ASP objects covered in the Chapter 13, we are not going to review each in detail. In this section, we'll review the Response, Request, and Page objects. Finally we'll cover the global.aspx file for session and application management.

The Response Object

The server uses the Response object to create a header and data to communicate with the client web browser. Using the Response object, you can manipulate both the header and data.

The Response.write Command

You may be somewhat familiar with the Response.write command from traditional ASP. However, as we've mentioned, ASP.NET expects a whole new web page structure. Remember, logic is typically placed at the top of the page, leaving the rest for the user interface. As you can see in our example in Listing 14.6 and the result in Figure 14.9, we've placed the Response.write command in the Page_Load event.

Listing 14.6: RESPONSEWRITE.ASPX

start example
<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1" %> <script runat="server">    sub Page_Load(obj as object, e as eventargs)       Response.write("Response.Write Example")    end sub  </script>  <html>  <head>  <title>ResponseWrite Example</title>  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">  </head> <body> </body>  </html> 
end example

click to expand
Figure 14.9: Viewing the output from ResponseWrite.aspx in Internet Explorer

The Response.buffer Command

As the web server compiles your ASP.NET script file and generates the header and date of the Response object, that data is usually held in a temporary buffer until all the script processes are complete. However, you can turn this buffer off or on using Response.buffer. To disable the buffer, insert Response.buffer = FALSE into the Page_Load event of your ASP.NET script file as shown in Listing 14.7.

Listing 14.7: RESPONSEBUFFER.ASPX

start example
<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1" %> <script runat="server">   sub Page_Load(obj as object, e as eventargs)       Response.buffer = False    end sub  </script>  <html>  <head>  <title>ResponseBuffer Example</title>  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">  </head> <body> Response.buffer example  </body> </html>
end example

The Response.redirect Command

You use the Response.redirect command to send a user to a different URL without their knowledge. As we demonstrated in Chapter 12, a typical implementation of Response.redirect is to protect a page with a login procedure. As you may recall, the concept is to look for a passed form field and compare the value to an authorized login. If the login values do not match, the script redirects the user to another web page. You'll notice that the ASP.NET implementation of the PageSecurity script shown in Listing 14.8 is similar to the ASP version. However, the ASP.NET code is encapsulated in the Page_Load event. In short, the Page_Load event checks for a submitted form field called Name. If Name has not been submitted, Page_Load redirects the user to HelloWorld-VB.aspx. If Name does exist, Page_Load compares the submitted value of Name to the appropriate password "admin". If the value of Name does not equal the appropriate password, Page_Load redirects the user to HelloWorld-VB.aspx. If the value of Name does match the password, the rest of the current PageSecurity.aspx page loads and the user can see the content.

Listing 14.8: PAGESECURITY.ASPX

start example
<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1" %> <script runat="server">    sub Page_Load(obj as object, e as eventargs)      if len(trim(request.form("Name"))) = 0 then      response.redirect("HelloWorld-VB.aspx")        else          if trim(request.form("Name")) <> "admin" then           response.redirect("HelloWorld-VB.aspx")          end if        end if      end sub  </script>  <html> <head>  <title>PageSecutity Example</title>  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">  </head> <body> </body> </html>
end example

The Request Object

The Request object receives content from the browser. It does so in the form of collections of variables that encapsulate the data for each request. There are several Request collections-Request.Form, Request.Querystring, and Request.ServerVariables.

The Request.Form Collection

When a form using the Post submission method is submitted to the current ASPX page, the collection of submitted form values is available through the Request.Form command. For example, to reference the submitted form value of a text box named FirstName, use the following command:

Request.Form("FirstName")

The Request.Querystring Collection

Request.Querystring is a collection of value pairs passed in the URL address to an ASPX page. For example, when a page passes a variable and value through a URL address such ashttp://www.sybex.com? FirstName =Bryanthe collection of passed form values is available to you through the Request.Querystring command. In addition to passing variables through the URL, you can also use the Get form submission method to pass value pairs of variables and values to a ASPX page. To reference the FirstName variable passed through the URL string in this example, you use this syntax:

Request.Querystring("FirstName")

Request.Form and Request.Querystring are useful collections carried over from traditional ASP. However, in contrast to traditional ASP, you will find you can accomplish most of the functionality of these collections with web server controls.

The Page Object

You can reference every ASPX page through the Page object. In addition, the Page object has several features and events you will find yourself using often. In fact, we've already used one of the events in several of our examples-Page_Load. Using the Page_Load event, we can direct ASP.NET to execute code as the page loads but before it renders HTML. This event is the perfect time to execute functionality such as authenticating users or redirecting users to established data connections.

Another feature of the Page object you will use often is the IsPostBack property. Remember we discussed a Postback Form, which is a form that submits data to itself. The IsPostBack property allows you to tell if the current page is being loaded for the first time or if the page is being loaded in response to a form submission. Listing 14.9 shows the IsPostBack property in action. As you can see in Figure 14.10 and Figure 14.11, when the page is loaded, the script uses the IsPostBack property to determine whether the page is loaded for the first time or in response to a postback generated by clicking the form's Enter button.

Listing 14.9: ISPOSTBACK.ASPX

start example
<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1" %> <script runat="server">    sub Page_Load(obj as object, e as eventargs)       If Not IsPostBack      lblMessage.Text = "This page is loaded for the first time."       else          lblMessage.Text = "This page is loaded is response to a post back."      end if     end sub </script>  <html>  <head>  <title>IsPostBack Example</title>  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">  </head> <body> <form runat="server">   <asp:label  runat="server" ></asp:label>   <asp:button  runat="server" Text="Enter" />  </form> </body> </html> 
end example

click to expand
Figure 14.10: Viewing IsPost-Back.aspx in Internet Explorer

click to expand
Figure 14.11: Viewing IsPost-Back.aspx in Internet Explorer

The global.asax file

The global.asax file is a tool for controlling both your application and the client session, and you can place this file in the root directory of your web application. ASP.NET looks for this file as a user makes their first request to your web application. The global.asax file controls the following events in the following order.

  1. Application_OnStart

    This event is executed when the first user requests a page from your ASP.NET application. This event is reset if the web server is restarted or if you edit the global.asax file.

  2. Session_OnStart

    This event triggers every time a new user requests a page from your ASP.NET application. It occurs directly after the Application_OnStart event.

  3. Session_OnEnd

    This event is triggered when a user or the site ends a session. This typically happens when a user has not requested a page for 20 minutes.

  4. Application_OnEnd

    The application ends when the last user connected to the ASP.NET application ends their session, when the server stops, or when the global.asax is altered.

Most developers use the global.asax file to initialize application and session variables or to run processes. For example, application and session code can be placed in the web application global.asax

file as shown in VB.NET in Listing 14.10 and C# in Listing 14.11. It is your responsibility to add code that destroys session and application variables on the session and application end events.

Listing 14.10: GLOBAL.ASAX IN VB.NET

start example
Sub Application_Start(Sender As Object, E As EventArgs)     application("emailAddress") = "webmaster@helpmedia.com"  End Sub Sub Session_Start(Sender As Object, E As EventArgs)     session("userSid") = Request.cookies("userSid")     Session.Timeout = 1 End Sub Sub Session_End(Sender As Object, E As EventArgs)     session("userSid") = null  End Sub Sub Application_End(Sender As Object, E As EventArgs)     application("emailAddress") = null  End Sub
end example

Listing 14.11: GLOBAL.ASAX IN C#

start example
void Application_Start(object sender, EventArgs e) {     application("emailAddress") = "webmaster@helpmedia.com";  } void Session_Start(object sender, EventArgs e) {     session("userSid") = Request.cookies("userSid");     Session.Timeout = 1;  } void Session_End (object sender, EventArgs e) {     session("userSid") = null;  } void Application_End (object sender, EventArgs e) {     application("emailAddress") = null; }
end example



Mastering Dreamweaver MX Databases
Mastering Dreamweaver MX Databases
ISBN: 078214148X
EAN: 2147483647
Year: 2002
Pages: 214

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