Web Site Interface Techniques



Web Site Interface Techniques
ASP.Net tutorial by Barbie Hocking  ©2010

Click for Sample Code           
As discussed in the Web Application Vs Web Site Model article, a Web Site's master pages, web forms, and user controls are not stand-alone classes and thus cannot directly cross-communicate with each other. Interfaces allow full cross-communication between these entities. Interfaces are not difficult to implement. The flexibility they provide makes it worth the additional time it takes to set them up.

Interface Overview
Even though an interface is a defined on a physical file, it can be looked at as a logical interaction point. Since it is an intersection point, it does not contain any processing. Instead, It defines the member signatures of the intersection points. In effect, it is simply defining the syntax of the object.  A class implementing an interfaces must include each of its signatures.

An interface is setup similar to a class; however, it uses the Interface descriptor instead of the Class descriptor. Note that the naming standard for interfaces dictates that the first letter of the name begins with 'I'.
  • Class: public class BasePage
  • Interface: public interface IInterfaceSample
How does an interface allow web master pages, web forms, and user controls to access each other's public methods and properties? A Web Site creates a dll for all components within a directory which creates a likelyhood that not all web entities exist within the same dll. Interfaces are created in the App_Code directory.  All interfaces and class objects reside in the App_Code.dll and as such can be accessed by web entities.

Sample Interface:
namespace InterfaceTechniques
{
    public interface IInterfaceSample
    {
        string ListSelectedProducts(bool bBuy);
    }
}

Implementing the interface on a web form:
namespace  InterfaceTechniques
{
    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.Web.UI; 
    using System.Web.UI.WebControls;

    public partial class InterfaceSample : System.Web.UI.Page, IInterfaceSample
    {
        protected void Page_Load( object sender, EventArgs e)
        { 
        }

        #region
IInterfaceSample Members
        public stringListSelectedProducts( bool bBuy)
        {
            if (bBuy)    
                return "this is a BUY';
            else
                return "this is a SELL';
        }
        #endregion 
     }
}


Accessing the interface from child user control:
The child control typecasts the web form control to the interface "IInterfaceSample".  The public methods and properties defined in the interface and implemented in the web form control are now accessible.
   lblParentMethodResults.Text = ((InterfaceTechniques.IInterfaceSample) this.Page).ListSelectedProducts(this.rbBuy.Checked);
 
Click for Sample Code           
 Contact Us     Links      ©2010 GeekPhilosopher.com - All rights reserved
Powered by www.ezjooz.com