views:

64

answers:

1

I need to use javascript to request some data from the server in a.NET 3.5 Webforms demo app.

It comes to mind that I have only ever done this with AJAX.NET, jquery, and updatepanels. I don't want to involve external technologies, nor do I want to do even a partial postback. Can anyone point me at an example of the simplest way to do this?

+1  A: 

You can do that by exposing the data via a web service

[ScriptService] public class SimpleWebService : System.Web.Services.WebService{
[WebMethod]
public string EchoInput(String input)
{
    // Method code goes here.
}}

And can then add it into your asp.net page like so

<asp:ScriptManager runat="server" ID="scriptManager"> <Services>  <asp:ServiceReference
   path="~/WebServices/SimpleWebService.asmx" />  </Services></asp:ScriptManager>

To call the javascript

      // This function calls the Web Service method.  
        function EchoUserInput()
        {
            var echoElem = document.getElementById("EnteredValue");
            Samples.AspNet.SimpleWebService.EchoInput(echoElem.value,
                SucceededCallback);
        }

        // This is the callback function that
        // processes the Web Service return value.
        function SucceededCallback(result)
        {
            var RsltElem = document.getElementById("Results");
            RsltElem.innerHTML = result;
        }

There it is, easy as. Check more details at ASP.Net

Kevin
So then how would I use javascript to invoke this webservice?
George Mauer
I added the js call. Sorry about that.
Kevin
Thanks a lot - awesome!
George Mauer