ASP.NET
  Home arrow ASP.NET arrow Page 3 - ASP.NET Basics (Part 7): Command and Contr...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

ASP.NET Basics (Part 7): Command and Control
By: Harish Kamath (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 33
    2003-11-24

    Table of Contents:
  • ASP.NET Basics (Part 7): Command and Control
  • The Last Action Hero
  • Requesting More
  • Test Drive
  • The Taste Test
  • A Matter of Control
  • Alien Invasion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    ASP.NET Basics (Part 7): Command and Control - Requesting More


    (Page 3 of 7 )

    Before moving on to more complex form processing, let's dawdle a little bit with our new acquaintance, the Request object, and find out a little more about it. The Request object is one of the more useful objects you'll encounter in your ASP.NET development, since the concept of a request and a response is the backbone of the client-server architecture. The Internet is, after all, a vast network of servers, with each of us accessing it through clients. For each bit of information that we wish to access, there is a "request" sent to some server that in turn sends the appropriate "response" back to the browser (client).

    In addition to the "QueryString" and "Form" properties you just saw, the Request object also comes with a bunch of other useful properties; the following example demonstrates some of them:


    <html>
    <
    head>
    <
    basefont face="Arial">
    </
    head>
    <
    body>

    <
    table border="1" cellspacing="5" cellpadding="5">
    <
    tr>
    <
    td><b>Variable</b></td>
    <
    td><b>Value</b></td>
    </
    tr>

    <
    tr>
    <
    td>Hostname</td>
    <
    td>
    <% 
    Response.Write (Request.UserHostName) %>
    </
    td>
    </
    tr>

    <
    tr>
    <
    td>Host address</td>
    <
    td>
    <% 
    Response.Write (Request.UserHostAddress) %>
    </
    td>
    </
    tr>

    <
    tr>
    <
    td>URL</td>
    <
    td>
    <% 
    Response.Write (Request.Url) %>
    </
    td>
    </
    tr>

    <
    tr>
    <
    td>Path to script</td>
    <
    td>
    <% 
    Response.Write (Request.PhysicalPath) %>
    </
    td>
    </
    tr


    <
    tr>
    <
    td>Browser</td>
    <
    td>
    <% 
    Response.Write (Request.UserAgent) %>
    </
    td>
    </
    tr

    </
    table>
    </
    body>
    </
    html>



    And when you view the file in your browser, you'll probably see something like this:



    Why stop there? ASP.NET also provides a Browser object (a property of the Request object that, coincidentally, is itself an object) that provides information about the HTTP client you're using. Here is an example of how it can be used:


    <html>
    <
    head>
    <
    basefont face="Arial">
    </
    head>
    <
    body>

    <
    table border="1" cellspacing="5" cellpadding="5">
    <
    tr>
    <
    td><b>Variable</b></td>
    <
    td><b>Value</b></td>
    </
    tr>

    <
    tr>
    <
    td>Client version</td>
    <
    td>
    <% 
    Response.Write (Request.Browser.Version) %>
    </
    td>
    </
    tr

    <
    tr>
    <
    td>Supports tables?</td>
    <
    td>
    <% 
    Response.Write (Request.Browser.Tables) %>
    </
    td>
    </
    tr

    <
    tr>
    <
    td>Supports frames?</td>
    <
    td>
    <% 
    Response.Write (Request.Browser.Frames) %>
    </
    td>
    </
    tr

    <
    tr>
    <
    td>Supports Java applets?</td>
    <
    td>
    <% 
    Response.Write (Request.Browser.JavaApplets) %>
    </
    td>
    </
    tr

    <
    tr>
    <
    tdSupports JavaScript?</td>
    <
    td>
    <% 
    Response.Write (Request.Browser.JavaScript) %>
    </
    td>
    </
    tr

    <
    tr>
    <
    td>JavaScript version supported</td>
    <
    td>
    <% 
    Response.Write (Request.Browser.EcmaScriptVersion) %>
    </
    td>
    </
    tr

    </
    table>
    </
    body>
    </
    html>



    Here's the output:



    All these variables come in handy if you need to make decisions on the basis of remote variables, as the following example demonstrates:


    <script language="c#" runat="server">
    void Page_Load()
    {   
        
    string browser Request.UserAgent;

        if(
    browser.IndexOf("MSIE") >= 0)
        {
            
    // IE-specific code
            
    output.Text "You're using Internet Explorer, aren't you?";
        }
        else if(
    browser.IndexOf("Mozilla") >= 0)
        {
            
    // Mozilla-specific code
            
    output.Text "Mozilla rocks, dood! Gimme five!";
        }
        else
        {
            
    // any other browser
            
    output.Text "Hmmm, you're using a browser I don't know about. Never mind, you're welcome too!";
        }

    </script>
    <html>
    <head></head>
    <body>
    <asp:label id="output" runat="server" />
    </body>
    </html>



    Note my usage of the IndexOf() method of the "string" object (the "string" datatype, like just about everything else in the .NET environment, is an object, with its own set of properties and methods) - this method is used to scan a string for a specific character sequence. It returns 0 if the sequence cannot be matched, and is thus very useful to run browser detection routines, as in the sample above.

    More ASP.NET Articles
    More By Harish Kamath (c) Melonfire


     

    ASP.NET ARTICLES

    - Developing a Mini ASP.NET AJAX Server Centri...
    - Disadvantages of the ASP.NET MVC Framework
    - Advantages of the ASP.NET MVC Approach
    - ASP.NET Web Forms Weaknesses
    - ASP.NET Web Forms Meets ASP.NET MVC
    - Source Code for Saving and Retrieving Data w...
    - Using GridView to Save and Retrieve Data wit...
    - Handling Dynamic Images in ASP.NET 3.5 AJAX ...
    - Retrieving Data with AJAX and the GridView C...
    - Playing with Images in ASP.NET 3.5 AJAX Appl...
    - Saving and Retrieving Data with AJAX
    - Enhancing PHP Via the ASP.NET AJAX Framework...
    - Enhancing PHP Programming with the ASP.NET A...
    - Classes and ASP.NET AJAX
    - Using ASP.NET AJAX

     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     




    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT