Showing posts with label webmethod. Show all posts
Showing posts with label webmethod. Show all posts

Wednesday, March 28, 2012

Calling a WebMethod from JavaScript (In another directory) doesnt work

Hello everybody,

I have a Web Part and in that web part (ascx) and in a seperate directory (Javascript/Authentication/login.js) I have a javascript.

I have a web method from the other side that check and see if a user is available or not. I know 100% my webmethod is working because when I run it I can test the webmethod in browser from Visual Studio.

The problem is that when even I try to access that webmethod (webservice) the error : WebService is not defined pops up.

I want to know why this is happening ? Is it because I have my javascript in a seperate file other than my web service file (asmx) ?


I appreciate any help.

Thanks.

Please totally forgive me, I had a syntax error, I fixed it now, working fine.

I typed the name of my web service wrong.

Sorry Again.

Monday, March 26, 2012

Call webmethod in page possible

I thought it was possible to call a public method with the attribute [WebMethod()] above it. But when I try out the sample of the AutoCompleteExtender it works perfectly when I put the code in a webservice (.asmx). Unfortunately when I put the same code in my default.aspx page it doesn't work.

I changed the ServicePath to ="Default.aspx" but that doesn't seem to do the trick.

So does anyone have a clue here what I'm doing wrong?Hi,

at the moment the auto complete stuff works only with a web service. Page methods are not supported.
However, do a search in this forum since a user (I can't remember the name, sorry) has coded a custom version that supports page methods.

Jay Kimble is your man.

http://david.codebetter.com/blogs/jay.kimble/archive/2006/07/17/147436.aspx


The auto complete extender in my library also supports this.

Call WebMethod declared on a DIFFERENT aspx page?

I'd like to expose a WebMethod on an ASPX page and then invoke it from my Atlas page:

<atlas:ScriptManagerrunat="server"ID="scriptManager">

<services>

<atlas:servicereferencepath="MyAspxBasedService.aspx"type="text/javascript"/>

</services>

</atlas:ScriptManager>

Then invoke it. The reason is that I want the full ASPX processing to happen. In short, I want to declaratively create controls on the ASPX page, then render partial pieces of them to be returned back to the Atlas client.

Any help?

Tad

Just as I sent, I noticed that you wanted to call a page method on adifferent page, so clearly PageMethods.YourMethodName() won't work.

Calling a method on a different page is not supported. I'm not convinced that this is desirable; when you call a method on the current page, the point is that the server page gets populated with all the data from the browser, and that your method can make use of it. But if it's a different page, it can only have its clean original state, which seems a lot less useful.

David


Hi Tad,

You don't need to use any servicereference to call a method that's on the page itself. Just mark the method with a [WebMethod] attribute, and call it using the name PageMethods.YourMethodName(...).

David


Thanks David.

I've got a flyout asp:Menu control on my site that is 3 levels deep, which bulks up the page size and is pig slow. I thought about seeing if I could get the same benefits of the control by making it populate on demand. So, my ASPX call will pass a parameter indicating which branch of my menu I need to render (from my database). I would use the menu control to programmatically build the branch and render the html which I'd return from my webservice call. (of course I'd cache it too).

The nice thing about the ASPX page is that I can declaratively define the menu properties and templates.

I suppose I could make an XmlHttpRequest directly to the ASPX and pass query string params, but that seems soooooo YESTERDAY!

I'm just foolin' around. Any ideas before I go off and just build my own lightweight flyout control?

Tad


Why not use a Web Service that provides your site navigation menu data that takes, as input, information indicating the current branch that the page is on? Atlas is definitely designed for dealing with that type of scenario, and the 'pig slow' effect can be mitigated by having bits of your menu populated dynamically asynchronously using the Web Service invocation.

Obviously if you're tied to only using an ASPx page, then thats that...but one of the first things I did with Atlas was whip out a dynamically-generated site navigator that was powered by a Web Service. I was really impressed with it.


I wouldn't attempt that myself. Plus, I don't think a flyout menu is the right control for on-demand population (which is exactly why we didn't include the feature in the first place). Imagine what will hapen if the user flies his mouse cursor over the menu (which he'll necessarily do unvoluntarily): tens of requests sent to the server at the same time. Ugly.

Developing a lighter menu with fewer features but with focus on CSS without inline styles is probably a much better option. I was thinking of doing that when I have some time (which probably means not in the next few weeks).


Sure, that's a good point, and even without using a web service and dynamic population, reducing the depth of the menu and starting at the current hierarchy level can help considerably.

A combination of a menu and a SiteMapPath can give a good navigation UI without going too fancy.

About what you describe, the thing is that the menu does not support populate on demand. A treeView would do the trick, but that may not be the UI you want (although the TreeView appearance can be brought close to that of a menu with the exception of the fly-outs).

Saturday, March 24, 2012

Call a WebMethod from javascript (I dont want to use any server side buttons only javascri

I think my problem is slightly different.

I have defined a region of an image using jquery (javascript) I check the click event, it something like onclick for button, so as you can see its all javascript nothing server side button, etc

I want to load a .asmx dynamically in an update panel. I know how to do this using a link button. Now what I want to do is to call a method that load this file in my server code behind.

This is what I tried to do so far. I simplified the problem.

' <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" EnablePartialRendering="true">
<Scripts>
<asp:ScriptReference Path="JavaScript/loadmypages.js"/>
</Scripts>
</asp:ScriptManager>

'

loadmypages.js is a simple javascript that call my function (i am hoping that it will) through PageMethods. this is a simple style of my javascript.

'

// JScript File

var Load_content2 = function()
{
PageMethods.myfunction_on_code_behind(OnComplete1, OnTimeOut1, OnError1);
return false;
}

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

function OnTimeOut1(result)
{
document.getElementById('lblMsg').innerHTML ="Time out";
}

function OnError1(result)
{
document.getElementById('lblMsg').innerHTML ="There is an error!";
}

'

I have tried to make my method accesable for javascript like this:

'

using System.Web.Services;

[WebMethod]
[System.Web.Script.Services.ScriptMethod()]
public string myfunction_on_code_behind()
{
return "Its good if it works";
}

'

but every time i click on that region the script debugger say PageMethods is not defined.

Could someone help me to solve this problem ? am I missing something here ? as you can see I have even enabled page method: EnablePageMethods="true"

Thank you

Hi,

Please note that the method should be a static method.
Hope this helps.
Can you post your whole webservice file here? I think you are missing something.

Has there been any updates to MS Ajax that don't require PageMethods to use static methods yet?


No, nor would I expect one in the future.