Web Service One-Liners!

Aug 16, 2008 at 11:09 PM
Edited Aug 16, 2008 at 11:17 PM
Hi, I am new to AJAX and JavaScript. Am getting some weird behaviour from my web service when called from dropthings...
-------------------------------------------------------------------------------------
1. This is my ScriptManager in Default.aspx:
-------------------------------------------------------------------------------------
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true" LoadScriptsBeforeUI="false" ScriptMode="Release">
    <Services>
        <asp:ServiceReference InlineScript="true" Path="PageService.asmx" />
        <asp:ServiceReference InlineScript="true" Path="ProxyAsync.asmx" />
        <asp:ServiceReference InlineScript="true" Path="WidgetService.asmx" />
        <asp:ServiceReference InlineScript="true" Path="Conversion.asmx" />
    </Services>
</asp:ScriptManager> 
-------------------------------------------------------------------------------------
2. This is my JavaScript in Widget ASPX:
-------------------------------------------------------------------------------------
<script type="text/javascript">
        function Convert()
        {
            var txtTextBox = $get("txtTextBox");
//            var text = "";
            switch (event.keyCode)
            {
                case 49: Conversion.OnKeyPress(1, txtTextBox, OnWSRequestComplete());
//              case 49: Conversion.OnKeyPress(1, txtTextBox);
                case 50: Conversion.OnKeyPress(2, txtTextBox);
                case 51: Conversion.OnKeyPress(3, txtTextBox);
                case 52: Conversion.OnKeyPress(4, txtTextBox);
                case 53: Conversion.OnKeyPress(5, txtTextBox);
                    break;
                default: return;
            }
//            OnWSRequestComplete(text);
        }
       
        function OnWSRequestComplete(result)
        {
            var txtTextBox = $get("txtTextBox");
            txtTextBox.control.set_data(result);
        }
    </script>
-------------------------------------------------------------------------------------
3. Codebehind:
-------------------------------------------------------------------------------------
if (this._Host.IsFirstLoad)
{
    txtTextBox.Attributes.Add("onkeypress", "return Convert();");
}
-------------------------------------------------------------------------------------
4. Finally, the web service
-------------------------------------------------------------------------------------

...
[WebService(Namespace = "http://www.dropthings.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class Conversion : System.Web.Services.WebService
...

[WebMethod]
[ScriptMethod(UseHttpGet = false, XmlSerializeString = true)]
public void OnKeyPress(Int32 number, System.Web.UI.WebControls.TextBox textBox)
{
 String word;
 switch (number)
 {
  case 1: break;
  case 2: break;
  case 3: break;
  case 4: break;
  case 5: break;
  default: return;
 }
    Int32 index = textBox.Text.LastIndexOf(" ") + 1;   ***
 ...More Code
}
-------------------------------------------------------------------------------------
Now, the problem is in the web service, the code I've marked as "***" basically, the code runs to this point, then for some reason, goes back up to the "switch(number)" again.... it's bonkers - I've never seen this kind of behavior before. What am I missing??

Thanks all!

Aug 18, 2008 at 9:58 PM
Okay, I can see that the textbox object si not being passed to the WebMethod... strange. I changed:

var txtTextBox = $get("txtTextBox");

to:

var txtTextBox = $get('txtTextBox');   (single quotes)

in case the syntax was wrong, but still no luck. Anyone know what's going on here?

Cheers!
Aug 20, 2008 at 7:16 PM
Hi All,

I got it; LOL. It's all because the controls are in a widget and that means that all controls are dynaimcally given names - so i had to refernece the item like so:

var

 

textBox = document.getElementById("WidgetPanelsLayout_WidgetContainer841_Widget841_txtTextBox");

Okay, that helps for one user, but the widget instance will change with different users. .. will create new post to query this.... LOL.

Cheers!