Monday, March 26, 2012

call webservce

Hi

i try to call a webservice when form load,but i can't

my code like

<form id="f" onload = "callService()>

<script language = "javascript">

function callServicce()

{..........}

</script>

</form>

please help me ??

thank you

You need to register your webservice using ScriptManager and ServiceReference.
Here are examples of the webservice and javascript code which calls the webservice. Method DoSearch is calling the webservice method

Webservice in (Webservice.asmx)

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService] //If this is not added, then it is not possible to generate javascript proxy.
public class MyWebService : System.Web.Services.WebService {

[WebMethod]
public string HelloWorld(string query)
{
string inputString = Server.HtmlEncode(query);
if (!String.IsNullOrEmpty(inputString))
{
return String.Format("Hello, you queried for {0}. The "
+ "current time is {1}", inputString, DateTime.Now);
}
else
{
return "The query string was null or empty";
}

}

}

JavaScript snippet:

<form id="Form1" runat="server">
<asp:ScriptManager runat="server" ID="scriptManager">
<Services>
<asp:ServiceReference path="~/WebService.asmx" />
</Services>
</asp:ScriptManager>
<div>
Search for
<input id="SearchKey" type="text" />
<input id="SearchButton" type="button" value="Search"
onclick="DoSearch()" />
</div>
</form>
<hr style="width: 300px" />
<div>
<span id="Results"></span>
</div>

<script type="text/javascript">

function DoSearch()
{
var SrchElem = document.getElementById("SearchKey");
MyWebService.HelloWorld(SrchElem.value, OnRequestComplete);
}

function OnRequestComplete(result)
{
var RsltElem = document.getElementById("Results");
RsltElem.innerHTML = result;
}

</script>

Hope this helps


thanks for your reply

but my question is how to call webservice on formload event ?

i already do what you show,but webservice could'n to be call on form load event


Ajax, your code looks fine. Perhaps you could post your entire script so we can debug further?
I think you could try ScriptManager1.RegisterClientScriptBlock method in Page_Load Event.

No comments:

Post a Comment