Code Simplified – Viral Sarvaiya

Code Simplified – Viral Sarvaiya, Web Developer Friends, dot net Developer, Sql Server Developer

Calling Web Service from Java script in asp.net c#

Posted by Viral Sarvaiya on March 23, 2010

Here i m demonstrate the simple application that calling webservie from javascript

step 1 : file -> new -> web site

step 2 : select ASP.NET Web Service

step 3 : Add new web form name “default.aspx”

Step 4 : following code shows the “service.cs” file


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

[WebService(Namespace = "http://Localhost...xys/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]

<strong>[System.Web.Script.Services.ScriptService()]</strong>

public class Service : System.Web.Services.WebService
{
public Service () {

//Uncomment the following line if using designed components
//InitializeComponent();
}

[WebMethod]
public string HelloWorld(string strNoOfData)
{
return strNoOfData;
}

}

step 5 : following code shows the “default.aspx” file


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" language="javascript">
function CallService() {
Service.HelloWorld(document.getElementById('Textbox1').value, OnComplete, OnError, OnTimeOut);
}

function OnComplete(Text) {
alert(Text);
}

function OnTimeOut(arg) {
alert("timeOut has occured");
}

function OnError(arg) {
alert("error has occured: " + arg._message);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/Service.asmx" />
</Services>
</asp:ScriptManager>

<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<fieldset>
<asp:TextBox ID="Textbox1" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="Call Service" OnClientClick="CallService()" />
</fieldset>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>

Step 6 : Run The Application

Enjoy Coding…

About these ads

4 Responses to “Calling Web Service from Java script in asp.net c#”

  1. […] Disclaimer Calling Web Service from Java script in asp.net c# […]

  2. is the premier information resource for the ASP.net developer community. Jase Webservice

  3. […] the original post: Calling Web Service from Java script in asp.net c# « Web Developer … Share and […]

  4. viralsarvaiya said

    Dear H,
    Thanks for the Appreciation and give me Five Stars…..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

Join 135 other followers

%d bloggers like this: