Wednesday, May 8, 2013

sample code for call webservice using ajax in asp.net

Here we call webservice using ajax update panal and javascript .
We see also how an ASP.NET AJAX-enabled web service generates the JavaScript needed to call the web service.
: sample code as below:
Webservice.asmx
using System;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[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]
public class WebService : System.Web.Services.WebService {
    public WebService () {
        //Uncomment the following line if using designed components
        //InitializeComponent();
    }
    [WebMethod]
    public string SayHello(int a,int b)
    {
        return Convert.ToString(a + b);
    }
  
   
}
Default.aspx
<%@ 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>Untitled Page</title>
    <script language="javascript" type="text/javascript">
<!--
function Button1_onclick() {
ret = WebService.SayHello(document.getElementById('Text1').value,document.getElementById('Text2').value,  OnComplete, OnTimeOut, OnError);
return(true);
}
function OnComplete(arg) {
alert(arg);
}
function OnTimeOut(arg) {
alert("TimeOut encountered when calling Say Hello.");
}
function OnError(arg) {
alert("Error encountered when calling Say Hello.");
}
// -->
</script>
</head>
<body>
      <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        <Services>
        <asp:ServiceReference Path="~/WebService.asmx" />
        </Services>
        </asp:ScriptManager>
        <br />
        <div>
            <input id="Text1" type="text" /><br />
                       <input id="Text2" type="text" /><br />
            <input id="Button1" style="width: 158px" type="button" value="button" language="javascript" onclick="return Button1_onclick()" />&nbsp;</div>
    </form>
</body>
</html>

Source code download : Source code

If you are searching life partner. your searching end with kpmarriage.com. now kpmarriage.com offer free matrimonial website which offer free message, free chat, free view contact information. so register here : kpmarriage.com- Free matrimonial website

0 comments:

Post a Comment