Tuesday, November 11, 2014

Method Overloading and Overridding

Overloading and Overriding are the parts of Polymorphism.

Overloading is compile time polymorphism, also known as Early Binding or Static Binding.

Overriding is  run-time polymorphism, also known as Late Binding or Dynamic Binding.

So first we'll go through the Overloading.

Suppose you  need to define a method, but you don't have any idea about how many parameters would be there and also don't know the what type of data it would be.

Let's take a method named Insert_Employee.


 class Insert_Employee_Data  
   {  
     public static void Insert_Employee(int EmployeeID)  
     {  
       // Inserts EmployeeID as given and Employee's FirstName and LastName as empty  
     }  
     public static void Insert_Employee(int EmployeeID, string FirstName)  
     {  
       // Inserts EmployeeID and Employee Name as given whereas, LastName as empty  
     }  
     public static void Insert_Employee(int EmployeeID, string FirstName, string LastName)  
     {  
       // Inserts EmployeeID, Employee's FisrtName and LastName as given  
     }  

When your class's behaviours have totally different implementation but intended for single target then use Overloading.


Now let's go through Overriding.

Take a situation where you need to  use the base class's method in derived class with some add-on.
Suppose you have a price calculation class, which calculates the price and returns it to you.

The price calculation method should be declared as virtual, so that enhancements can be done to it later on (adding some expense may be).

   class Price_Calculation  
   {  
     public decimal UnitPrice { get; set; }  
     private int Quantity { get; set; }  
   
     public virtual decimal CalculatePrice()  
     {  
       return (UnitPrice * Quantity);  
     }  
   }  
   /// <summary>  
   /// This class also calculates the Price Amount but returns the total amount with Expenses also  
   /// </summary>  
   class Price_And_Expense_Calculation : PriceCalculation  
   {  
     public decimal ExpenseAmount { get; set; }  

     /// This method does not calculate the price itself, but overrideds the price calculation method and just  
     /// adds the expense amount to it  
   
     public override decimal CalculatePrice()  
     {  
       return (base.CalculatePrice() + ExpenseAmount);  
     }  
   }  

I hope you got the point to use overriding, means, if your class's behaviors have anything is common and can be shared with other derived forms of classes then they should be overidden.


Wednesday, October 15, 2014

Text Box should accepts only date using Calendar Extender

Sometimes we need validation for date, that user can only put the date in textbox.

Here is a method through which user can only select the date from calendar extender. User can't type, paste etc. in textbox.

Below is my TextBox along with Calendar Extender.


 <asp:TextBox ID="txtDate" runat="server" CssClass="mytextbox" Enabled="False"></asp:TextBox>  
         <asp:CalendarExtender ID="txtDate_CalendarExtender" runat="server"   
           Enabled="True" TargetControlID="txtDate" Format="dd/MM/yyyy">  
         </asp:CalendarExtender>  

On the Page_Load event of this form you just need to add a attribute to the TextBox.

  protected void Page_Load(object sender, EventArgs e)  
   {  
     if (!IsPostBack)  
     {  
         txtDate.Attributes.Add("readonly", "readonly");  
     }  
   }  

Now you can only select date from calendar extender. You can't type, paste etc. in textbox.

Thursday, October 9, 2014

Access Control Id on client side with or without using Master Page


Here is the method through which you can access Asp.net control's Id using javascript

When you are working on a simple form.


 Let's suppose you want to make a textbox clear or blank on a button click on client side

HTML
 <asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>  


Put below javascript code in the <head></head> section of the form

  <script type="text/javascript">  
     function clrCtrl() {  
        document.getElementById('txtPassword').value = "";  
          }  
   </script>  
On button's OnClientClick event we can use clrCtrl() function.


When you are using Master Page

When you are using Master Page, you just need to change the javascript code like below and out it in 
Content Section

  <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">  
 <script type="text/javascript">  
    function clrCtrl() {  
   document.getElementById('<%=txtPassword.ClientID%>').value = "";  
    }  
   </script>  
 // here you html code begins  
 </asp:Content>  

LinkWithin

Related Posts Plugin for WordPress, Blogger...