Sohail Sayed

  • Calling methods in a codebehind function (PageMethods) from client side using ajax.net

    Hi,
        Everyone using ajax.net will be familiar with the updatepanels. In addition another well known way of utilizing the rich ajax.net library is calling web service methods or methods from the code behind file from the client side. Recently i came across a situation in my project where i wanted to call methods from code behind (i have been using updatepanels but in some situation all i was doing was getting few values from the db and i felt not using the updatepanel would be a better option rather calling the my code behind function using pagemethods feature would be more appropriate). Though  there is a good deal of information on the internet on update panels and calling web services i found it difficult to reach a good enough article showing me how to call a function from the codebehind file.  Finally though :-) i was able to call the code behind function. i am going to demonstrate the same in this blog.
       Before starting let me indicate there could be many reasons for calling code behind methods from client side using ajax.net .....in my case i wanted to use existing functionality in my code behind and we were not using any webservices .

       Here i am going to demonstrate a simple   application that calls a code behind method

    to start first we create an ajax enabled website. Then in our page we add the heart of ajax.net -- the ScriptManager control and set the EnablePageMethods property as true

    <ajax:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">

    We also place a div and input buttons on our form

    <div id="div1">

  • Creating a custom Bread Crumb control in asp.net

    Hi,
        We all might be aware of the bread crumbs shown on so many websites. Bread crumbs help users in a big way to navigate the website in a structured manner. Asp.net provides a very good sitemap control which can be used to display a bread crumb. This uses a sitemap file which is actually a xml file indicating the struture and hierarchy of your web site.

       A few days ago a colleage of mine ran into soem issues with the standard asp.net sitemap control. It became evident that managing and keeping track of query string was not easy in the standard sitemap control. 

      This motivated me to develop a custom bread crumb control. The control i developed was a pretty simple one. I will  discuss about this control and explain in detail about the code.
        Some of the features of this bread crumb control are -
        1. It keeps track of the query string of previous pages browsed.
        2. It doesnt require any xml file.
       
        This is a very simple control and as you can see it can be developed further. The asp.net sitemap control is superior to it in a lot of ways and i will recommend users to use it unless you need new features.

         The breadcrumb consists of two main classes. - BreadCrumbNode and BreadCrumb

         BreadCrumbNode represents a visited URL or the current page, in addition it contains information like the caption/title to be shown on the breadcrumb. You can modify the class to include more information.

         The other class is the BreadCrumb which represents the BreadCrumb Control. Below is the code for both the classes - 

    namespace Web.Custom.Controls
    {

  • Inheritance, Abstract classes and Interfaces

    Hi,
         C++ was one of the first language i learnt and along with that i learnt the OOPs concept of inheritance. It was pretty simple and confusing at the same time (multiple inheritance concept saw me putting a lot of efforts in :-) ). However life became simpler with multiple inheritance eliminated in .Net. However came the new concept of interfaces in .Net. For a long time i couldnt get the understanding of when to use interfaces and when to use abstract classes. Things became much clearer later, though i have seen this topic remains a big confusion for a lot of people (atleast  i have seen this in my friend circle and also in lot of posts). I am writing on this topic as my first blog, and i hope it will help to make things related to interface and abstract classes clearer.

        Let us first look at the concept of inheritance. This has come up as a wonderful concept and one of the best methods to enable code reusability in the OOPs world. Let me give an example as how inheritance can be useful. For this let me give an example of the class System.Web.UI.Page class.  For those who have worked on asp.net this will be the most familiar class. This is a built in asp.net class that contains a lot of basic funcitonality related to a Web page. Now to create the simplest web page all i have to do is to create a class that inherits from the System.Web.UI.Page class ... and here you go ... my web page is ready ..though we may need to add some stuff to make it more meaningful. But once you inherit from this class you get access to a lot of objecft like response, request, server , and also lot of utility methods and events (you will find the list of  all the methods and events on the microsoft msdn website).[1]. This is the simplest example of the use of inheritance.
     
      Let me show you another example of the advantage of inheritance. In one of our projects We had to implement a lot of functionality related to formatting, localization and also performance tracking. We had to do custom formatting of data like currency , date and also numeric fields. For this we created our own controls that inherited from standard .Net controls. to be more specific let me show you the example of currency textbox and datetextbox. In our application we have to show currency formatted in a specific manner. Rather than formatting each and every textbox manually we create control that inherited from the asp.net Textbox control and formatted the content in that inherited control. This is shown below (this is a rough example, kindly ignore any syntax or casing errors) -

      public class CurrencyTextBox : Textbox
         public shadows property Text() as string
             get
                   return unformat( mybase.text) 'unformat will convert the formatted text to normal mumeric value 
             end get
             set (value as string)
                   mybase.text= format(value) 'format the numeric value to proper currency format
             end set
         end property
      end class

    another example for datetextbox
          public class DateTextBox: TextBox
              public sub onLoad()
                   me.attributes.add("onclick","showDateCalendar()") 'add attribute to show claendar when user clicks on textbox
              end sub
          end class
       
        using the above code i got the rid of taking care of formatting each control individually or adding attributes to each individual datetextboxes.

        Another example of inheritance i can tell is having a custom base class for all my base forms.
        We have a base class that itself inherits from System.Web.UI.Page class and all my webforms inherit from this class. We have put all common functionality in this base class and avoided repeating a lot of code that would have been otherwise written in each of the individual web forms. In addition to all these when ever we want to add any common functionality to all the web forms we need to add it to only the base class.

       I hope from here you can start appreciating the value of inheritance if you havent been earlier. Now let me go into abstract classes. In some cases we have a base class, but some times few functions of it it can not be defined fully. The simplest and perhaps a common example will be the class Shape which can be classified as the base class for the class Rectangle and class Circle. Every shape has area and volume. We cannot however define the functions related to them in the class Shape (the functions can be called -- CalculateArea , CalculateVolume). We just declare these functions and not define their implementation. Such functions are called as abstract functions[2]. And the classes that contain them get called as abstract classes.
     
       Interfaces are a special case where all the functions are abstract. The advantage of interfaces is that they enable in a way multiple inheritance.[3].

       Now comes the big confusion .. When to use interfaces and when to use base classes/ abstract base classes 

       A base class would normally represent a more generalized version of its child type like a furniture as a generalized type of chair or table.

       Generally speaking a child type (here type refers to class) may belong to more than one base type(this is called as multiple inheritance) but in most of the modern languages this is avoided due to higher complexity in multiple inheritance.
      in almost all cases a type can be calssified as inheriting from one main base type. Like a human(child) is an animal(base) and a cat(child) is also an animal(base). However types can also be classified according to thier behaviours like a human can move and a car can also move. However since most languages dont allow inheritance we define behaviours by implementing interfaces. in  addition implementing an interface will guarantee that a certain behaviour is present.