Monday 29 December 2014

How to call javascript and jquery function from code behind in ASP.net and c#

Introduction: Here I will tell you how you can call javascript and jquery function 
from codebehind.
Descirption:
Here I will tell you how you can call javascript and jquery function from codebehind.
If you are using scriptmanager on page then you can call
ScriptManager instance.
If you are not using script manager then you can call 
Page.ClientScript instance

Implementation:
In Code Behind 
protected void MyButton_Click(object sender, EventArgs e)
{
    Page.ClientScript.RegisterStartupScript(this.GetType(), "myScript", 
 "MyFunction();", true);
} 

protected void MyButton_Click(object sender, EventArgs e)
{
    ScriptManager.RegisterStartupScript(this.GetType(), "myScript", 
  "MyFunction();", true);
} 

Sunday 28 December 2014

Abstract classes in C#.NET, VB.NET

Introduction: Abstract classes are those classes that can not be instantiated, but they can be inherited by 
other classes.
Description: Abstract classes are those classes that can not be instantiated, but they can be inherited by 
other classes. Abstract classes are base classes and inherited classes are called derived classes.
Abstract class can have both abstract and non abstract method in it. 
When to use: It is use to give common functionality to all inherited classes. it works like a base class.
Implementation:
abstract class MyShapes
{ 
abstract public int MyArea();
}
class Square : MyShapes
{
int side = 0;
 public Square(int n)
{ 
side = n; 
}
// Override Area method
public override int MyArea()
{ 
return side * side; 
}
}
class Rectangle : MyShapes
{
int length = 0, width=0;
public Rectangle (int length, int width)
{
this.length = length;
this.width = width;
// Override Area method 
public override int MyArea()
{ 
return length * width; 
}
}

Thursday 4 December 2014

Call web method from page using jquery and ajax without postback

Introduction: In this article I will show you how to call webmethod from aspx page using jquery
and ajax without postback.

Description:  Call webmethod from aspx page using jquery and ajax without postback.



In Aspx Page:

<head runat="server">
    <title></title>
<script src="scripts/jquery-1.3.2.min.js" type="text/javascript"></script>
<script type = "text/javascript">
function ShowTime() {
    $.ajax({
        type: "POST",
        url: "CS.aspx/GetTime",
        data: '{name: "' + $("#<%=txtName.ClientID%>")[0].value + '" }',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: OnSuccess,
        failure: function(response) {
            alert(response.d);
        }
    });
}
function OnSuccess(response) {
    alert(response.d);
}
</script>
</head>

<body style = "font-family:Arial; font-size:10pt">
<form id="form1" runat="server">
<div>
Your Name :
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<input id="btnGetTime" type="button" value="Show Current Time"
    onclick = "ShowCurrentTime()" />
</div>
</form>
</body>


In Code Behind:

[System.Web.Services.WebMethod]
public static string GetCurrentTime(string name)
{
    return "Hello " + name + Environment.NewLine + "The Current Time is: "
        + DateTime.Now.ToString();
}