Saturday, March 24, 2012

Call c# function from javascript function

This is my code:

<%

@dotnet.itags.org.PageLanguage="C#"AutoEventWireup="true"CodeFile="Default2.aspx.cs"Inherits="Default2"%>

<%

@dotnet.itags.org.ImportNamespace="System.Web.Services"%>

<!

DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<

htmlxmlns="http://www.w3.org/1999/xhtml">

<

headrunat="server"><title>Untitled Page</title><scriptrunat=server>

[

WebMethod]publicstring GetName()

{

return"Hello Word";

}

</script><scriptlanguage=javascripttype="text/javascript">function btnCercaClick()

{

var value = GetName(success);

alert(value);

}

function success(result)

{

}

</script>

</

head>

<

body><formid="form1"runat="server"><div><asp:ScriptManagerID="ScriptManager1"runat="server"EnablePageMethods="True">

</asp:ScriptManager><asp:PanelID="pnlRicercaCliente"runat="server"Width="100%"><h3>

Find Cliente:

</h3><pstyle="vertical-align: middle"><inputid="txtRicerca"type="text"/><inputid="btnCerca"type="button"value="button"onclick="btnCercaClick()"/></p></asp:Panel></div></form>

</

body>

</

html>

This is a simple fragment of code, but there is an error.

Why??

Thanks.

How do want to call server code from client code?If you look at the web source, you will see the server code does not appears

The method you're calling (GetName) should be marked static, and then you should call it with PageMethods.GetName(success).

See the example at the bottom ofhttp://ajax.asp.net/docs/tutorials/ExposingWebServicesToAJAXTutorial.aspx.

No comments:

Post a Comment