ASP.NET
  Home arrow ASP.NET arrow Page 2 - Classes and ASP.NET AJAX
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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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

Classes and ASP.NET AJAX
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 4
    2008-06-19

    Table of Contents:
  • Classes and ASP.NET AJAX
  • Accessing base methods
  • Interfaces
  • Client Versions of .NET Classes
  • Enumerations

  • 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


    Classes and ASP.NET AJAX - Accessing base methods


    (Page 2 of 5 )

    When we talk about class inheritance, a logical question is whether methods can be overridden in derived classes. The answer is yes. The next question: is there any way to access the equivalent method of the base class, (i.e., the overridden method)? Even better, the answer is again yes, ASP.NET AJAX allows you to do so. To demonstrate this, let’s add a toString() method to OReilly.Softwarethat outputs the product and vendor names stored by the class. Theprototypeproperty ensures automated inheritance and also helps demonstrate access to the base method later on.

      OReilly.Software.prototype.toString = function() {
       
    return this.getName() + " from " + this.getVendor();
      }

    You could also directly access the properties_nameand_vendoras variables. Using the getter methods is just a personal preference. There is no functional difference in doing so.

    In theOReilly.Browserclass, you could write a similartoString()method:

      OReilly.Browser.prototype.toString = function(){
        return this.getName() + " from " + 
    this.getVendor() +
               (this.getIsJavaScriptSupported() ? " (w/ JS)" : " (w/o JS)");
      }

    However, it is once again advisable to reuse existing code, in this case, the base class’stoString()method. ASP.NET AJAX provides you withcallBaseMethod(), a helper method to call a method from the parent class that can take up to three parameters:

    instance
      
    The instance whose parent’s method to call (usually
       this)

    methodName
      
    The name of the method (as a string)

    baseArguments
       Parameters for the method, if any (as an array)

    In this case, thetoString()method ofOReilly.Browsercan be implemented as the following code demonstrates:

      OReilly.Browser.prototype.toString = function() {
        return OReilly.Browser.callBaseMethod(this, "toString") +
               (this.getIsJavaScriptSupported() ? " (w/ JS)" : " (w/o JS)");
      }

    Now, the code to output the browser information can be reduced a bit to these commands below:

      var s = "";
      var ie = new OReilly.Browser("Internet Explorer", "Microsoft", true);
      s = ie.toString() + "<br />";

      var lynx = new OReilly.Browser("Lynx", null, false);
      s += lynx.toString();
      document.getElementById("output").innerHTML = s;

    Example 4-3 shows the complete listing.

    Example 4-3. Accessing a base class method

    ClientBaseMethods.aspx

    <%@ Page Language="C#" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server">

      <title>ASP.NET AJAX</title>

      <script language="Javascript" type="text/javascript">
      function pageLoad() {
        var s = "";

        Type.registerNamespace("OReilly");
       
    OReilly.Software = function(name, vendor) {
          var _name = (name != null) ? name : "unknown";
          var _vendor = (vendor != null) ? vendor : "unknown";

          this.getName = function() {
           
    return _name;
          }
          this.setName = function(name) {
            _name = name;
          }

          this.getVendor = function() {
           
    return _vendor;
          }
          this.setVendor = function(vendor) {
            _vendor = vendor;
         
    }
        }
        Type.registerClass("OReilly.Software");

        OReilly.Browser = function(name, vendor, isJavaScriptSupported) {
          OReilly.Browser.initializeBase(this, new Array(name, vendor));
          var _isJavaScriptSupported = (isJavaScriptSupported != null) ?

    isJavaScriptSupported : false;
          this.getIsJavaScriptSupported = function() {
           
    return _isJavaScriptSupported;
            }
            this.setIsJavaScriptSupported = function(isJavaScriptSupported) {
             
    _isJavaScriptSupported = isJavaScriptSupported;
          } 
       
    }
       OReilly.Browser.registerClass("OReilly.Browser", OReilly.Software);

        OReilly.Software.prototype.toString = function() {
          return this.getName() + " from " + this.getVendor();
          } 

         OReilly.Browser.prototype.toString = function() {
            return OReilly.Browser.callBaseMethod(this, "toString") + 
             (this.getIsJavaScriptSupported() ? " (w/ JS)" : " (w/o JS)");
        };

        var ie = new OReilly.Browser("Internet Explorer", "Microsoft", true);
        s = ie.toString() + "<br />";

        var lynx = new OReilly.Browser("Lynx", null, false);
        
    s += lynx.toString();

        document.getElementById("output").innerHTML = s;
      }
     
    </script>

    </head>
    <body>
      <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <div id="output">
        </div>
      </form>
    </body>
    </html>

    Note that when you run this page, the output of this code is identical to that shown in Figure4-2.

    More ASP.NET Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "Programming ASP.NET AJAX," published by...
       · Please provide above code using what purpose how to execute in ASP.NET
     

    Buy this book now. This article is excerpted from chapter four of Programming ASP.NET AJAX, written by Christian Wenz (O'Reilly, 2007; ISBN: 0596514247). Check it out today at your favorite bookstore. Buy this book now.

    ASP.NET ARTICLES

    - Develop Your First ASP.NET Website with Visu...
    - Run ASP.NET in Windows XP Home with Cassini ...
    - How to Test a Web Application
    - How to Add Code and Validation Controls to a...
    - Working in Source and Split Views to Build a...
    - How to Build a Web Form for a One-Page Web A...
    - How to Develop a One-Page Web Application
    - An ASP.NET Web Application in Action
    - Developing ASP.NET Web Applications
    - An Introduction to ASP.NET Web Programming
    - Introduction to the ADO.NET Entity Framework...
    - Completing an In-Text Advertising System und...
    - Programming an In-Text Advertising System un...
    - Building an In-Text Advertising System Under...
    - Developing a Mini ASP.NET AJAX Server Centri...





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