Banner Ad

Monday, February 3, 2014

Attach Javascript to Button

By Francis   Posted at   2:01 PM   No comments

In this tutorial I’m going to explain how to add a javascript snippet to the ASP.Net server side Button Control.

 

Code:

Copy the below code in an .aspx file:

 

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
<asp:Button runat="server" ID="btnSubmit" Text="Click Me" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>


 


Copy the below code in an .aspx.vb file:

 
 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
btnSubmit.Attributes.Add("onclick", "alert('Button Clicked');return false;")
End Sub


Explanation:


The above code add “onclick” attribute to the Server side button “btnSubmit”. The attribute value is just a javascript snippet, which just show an alert box.


 


Take a look, after the alert we specify the “return  false;” which avoid the webform to be refreshed.


 


Hope this helps!

Tuesday, January 28, 2014

Daily Interview Question #5 : What is the difference between “Method Overloading” and “Method Overriding”?

By Francis   Posted at   1:47 PM   OOPS interview Questions No comments

Method overloading

Method overriding

method overloading used to achieve “early binding” or “static binding”. method overriding used to achieve “late (dynamic) binding” or “runtime polymorphism”.
method overloading means, in a class method can be declared with same method name and with different parameters. method overriding means, a method defined in base class can be redefined in the derived classes.
method signature (method name + parameter) must be different in method overloading. method signature must be same in method override.

 

Sample Program:

using system;
using system.collections.generic;
using system.text;

namespace consoleapplicationc
{
//Base class
class baseclass
{
private int add(int a, int b)
{
return a + b;
}
// Overload the Above Method
private float add(float a, float b)
{
return a + b;
}
// virtual method - Indicates this will be override in Child class
public virtual string whereiam()
{
return "In Base Class";
}
}
// Derived Class
class derived : baseclass
{
// Base class Method Override here
public override string whereiam()
{
return "In Derived Class";
}
}
class program
{
static void main(string[] args)
{
// Just create base class reference
baseclass basereference;
// Create Base class Object
baseclass baseobj = new baseclass();
// create object for Derived class
derived derivedobj = new derived();
// Base class reference contains derived object
basereference = derivedobj;
// Now Derived class method called with base ref
string strvalue = basereference.whereiam();
console.writeline(strvalue);
// Now Base class ref contains base object
basereference = baseobj;
string strval = basereference.whereiam();
console.writeline(strval);
console.read();
}
}
}
Output:
in derived class
in base class

Monday, January 27, 2014

Daily Interview Questions #4: What is the difference between Abstract Method and Virtual Method?

By Francis   Posted at   1:29 PM   OOPS interview Questions No comments

Abstract Method:

Abstract method specified by “abstract” keyword.

An abstract method contains no definition. Derived class must implement the abstract methods.

Object cannot be created for abstract class. That is abstract class can not be instantiated.

Virtual Method:

Virtual method specified by “virtual” keyword.

Used to implement “Run-Time” polymorphism.

Derived classes not forced to implement the “Virtual” methods that is available in Base class.

The process of redefine the virtual method in a derived class called as “Method Overridding”.

Friday, January 24, 2014

Daily Interview Question #3: What are the MEPs available in WCF?

By Francis   Posted at   11:12 PM   ASP.Net Interview Questions No comments
              MEP is the short form for Message Exchange Pattern. In Windows communication foundation, 3 types of Message exchange patterns are allowed. They are:

Thursday, January 23, 2014

Daily Interview Question#2 : What are the events will be fired when the “gridview.DataBind()” method called?

By Francis   Posted at   11:34 AM   ASP.Net Interview Questions No comments

When the Gridview.DataBind() method called, below events are fired in the following order:

  1. Databinding
  2. RowCreated  (Called for Each row)
  3. RowDatabound (called for each row)
  4. DataBound
Connect with Us