p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Beginning ASP.NET 4 : in C# and VB (http://p2p.wrox.com/forumdisplay.php?f=560)
-   -   Ch10 - page 354 step 7 - No Function (http://p2p.wrox.com/showthread.php?t=81293)

ken evans October 18th, 2010 07:36 AM

Ch10 - page 354 step 7 - No Function
 
Hello again!
Step 7 does not work for me.
The page "WebServices.aspx" displays without error.
But when I type my name and press the "SayHello" button, nothing happens.

The code is below.
Any clues?
Thanks
Ken

WebServices.aspx
Code:


<%@ Page Title="Web Services Demo" Language="C#" MasterPageFile="~/MasterPages/Frontend.master" AutoEventWireup="true" CodeFile="WewbServices.aspx.cs" Inherits="Demos_WewbServices" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cpMainContent" Runat="Server">
  <asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server">
  <Services>
  <asp:ServiceReference Path="~/WebServices/NameService.asmx" />
 
  </Services>
  </asp:ScriptManagerProxy>
  <input id="YourName" type="text" />
  <input id="SayHello" type="button" value="SayHello" />

  <script type="text/javascript">
    function HelloWorld()
    {
      var yourName = $get('YourName').value;
      NameService.HelloWorld(yourName, HelloWorldCallback);
    }

    function HelloWorldCallback(result)
    {
      alert(result);
    }

    $addHandler($get('SayHello'), 'click', HelloWorld);

  </script>
 
</asp:Content>

NameServices.cs
Code:

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

/// <summary>
/// Summary description for NameService
/// </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. Ref: page 351 & 353.
[System.Web.Script.Services.ScriptService]

public class NameService : System.Web.Services.WebService {

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

  // this code is updated from page 351 in the book to show three parameters with different data types
    [WebMethod]
    public string HelloWorld(string firstName, string lastName, int age)
    {
        return string.Format("Hello {0} {1} {2}", firstName, lastName, age);
    }
   
}


NameService.asmx
Code:

<%@ WebService Language="C#" CodeBehind="~/App_Code/NameService.cs" Class="NameService" %>

Imar October 18th, 2010 07:49 AM

Code:

public string HelloWorld(string firstName, string lastName, int age)
You made up parameters in the web service that you're not passing to it from the client side code. This should work:

Code:

public string HelloWorld(string firstName)
or this:

Code:

NameService.HelloWorld(yourName, "LastName", 39, HelloWorldCallback);
Hope this helps,

Imar
---------------------------------------------------------------------------------------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4 : in C# and VB, Beginning ASP.NET 3.5 : in C# and VB and ASP.NET 2.0 Instant Results.
While typing this post, I was listening to: Station Approach by Elbow (From the album: Leaders of the Free World) What's This?

ken evans October 18th, 2010 08:01 AM

Thanks Imar,
It's all working now.
I used the " firstName, lastName, age" code to experiment with multiple parameters in the previous exercise. It worked OK there but clearly broke things in this exercise.

Ken

Christos8 January 27th, 2012 08:25 AM

Problem with Calling Web Services from Client-Side Code
 
Hello!

I got a problem while writing the code of the try it out on the page 352.

At step 6, when writing

Code:

var yourName=$get('YourName').value
the .value does not highlighted from intellisence.

When I request the page in the browser, will be loaded, but writing in the textbox and pressing the button does not alert the message.

Any Idea?

Thank's in advance

Imar January 27th, 2012 10:08 AM

Quote:

the .value does not highlighted from intellisence.
This is by design. Visual Studio doesn't know the return value of $get has a value property. This is because of the untyped nature of JavaScript.

Quote:

but writing in the textbox and pressing the button does not alert the message.
Could be anything. Can you post the code for the page as well as the code for the web service? And have you tried debugging your code?

Imar

Christos8 January 27th, 2012 02:12 PM

Problem on client side for web servcice
 
Thanks, Imar for you direct responce.

the code in Web Sevice Demo page is the following

Code:

<%@ Page Title="Web Services Demo" Language="C#" MasterPageFile="~/MasterPages/Frontend.master" AutoEventWireup="true" CodeFile="WebServices.aspx.cs" Inherits="Demos_WebServices" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cpMainContent" Runat="Server">
<asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server">
    <Services>
        <asp:ServiceReference Path="~/WebServices/NameService.asmx" />
    </Services>
</asp:ScriptManagerProxy>
<script type="text/javascript">
    function HelloWorld() {
        var yourName = $get('YourName').value;
        NameService.HelloWorld(yourName, HelloWorldCallback, ErrorCallback);
    }
    function HelloWorldCallback(result) {
        alert(result);
    }
    function ErrorCallback(error) {
        alert(error.get_message);
    }
    $addHandler($get('sayHello'), 'click', HelloWorld());
</script>
<input id="YourName" type="text" />
<input id="SayHello" type="button" value="Say Hello" />
</asp:Content>

The Web service does work. I have tested many times. So the problem is not surely in the web service. Hence, I don't post the coressponding code.

No, I didn't debug my code.

Thank you in advance for your help.

By the chance, your book is really amazing. I have read it from the cover to the 354 page and I hadn't any problem. Also I have learned many things until know.

Christos

Imar January 28th, 2012 03:31 PM

Quote:

So the problem is not surely in the web service. Hence, I don't post the coressponding code.
Since it only takes a few seconds, you're probably better off posting the full code if you want people to look at your problem. The service may work by itself, but that doesn't mean it's named correctly, or has script services enabled or has any other problem.
Anyway, that doesn't seem to be the problem here.

Quote:

No, I didn't debug my code.
Probably a good habbit to get used to. Chapter 18 has the full details in case you want to know more. When you debug, it's easy to see if references are OK (they are not, in this case) and which code gets called.

Take a look at this:

Code:

$addHandler($get('sayHello'), 'click', HelloWorld());
You're using sayHello while the control is called SayHello with an upper case S.

Quote:

By the chance, your book is really amazing. I have read it from the cover to the 354 page and I hadn't any problem. Also I have learned many things until know.
Really glad to hear that. Thanks!

Imar

Christos8 January 30th, 2012 07:51 PM

Thanks
 
Imar thank's a lot for the hints. They really helped !

Thank's again for the direct responce.


All times are GMT -4. The time now is 06:58 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.