Showing posts with label methods. Show all posts
Showing posts with label methods. Show all posts

Wednesday, March 28, 2012

PageMethods and NON-Static methods

OK, after 4 days of trying every possible combination of everything... I *finally* figured out how to get PageMethods to work. The myth of "it can't be in the code behind" is completely wrong, however I'm starting to see that "the method but be static" is NOT a myth.

How in the world am I supposed to update the GridView then?

Your first response should be ...


<asp:UpdatePanel ID="upComparisonData" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnComparisonNavigate" EventName="Click" />
</Triggers>


That's beautiful... works great... BUT... I need to do more than merely update. I can't just update, there's some purely client-side custom validation I *MUST* do. There are many components on this application which were created in purely client-side XHTML and ECMAScript DOM. The above code will NOT work for me because I have to set all kinds of status bars, check other services (non-Atlas), and do validation as well as flip the status of quite a few objects around (turn DIVs on, turn DIVs off... etc...)... I mean there's a bunch of stuff going on in this app.

So... how in the WORLD do I access a GridView in my page from a static method? I have no problem with writing this entire thing custom... that's childs play, but I would much rather do this in Atlas so other developers don't have to try to decode my ninja Ajax code.

"The myth of "it can't be in the code behind" is completely wrong"

it wasn't a myth on earlier versions (beta 1 i believe) and probably has been fixed in the two releases since then, hence the "myth" is no more


Good deal..

Anyone know if THIS *critical* design flaw has been fixed?


If everything you're doing is purely client-side, then instead of trying to use PageMethods to update your panel, just put a display:none button in your UpdatePanel and call it's .click() from the clientside:

$get(

'ctl00_bBinHist').click();

That causes a postback, even in the UpdatePanel (which obviously defeats the entire purpose of the UpdatePanel). This entire application has absolutely NO postbacks. I'm not about to add one now.

I worked around that situation by rewriting a TON... now I have another situation which a rewrite is simply not possible.


try:

var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_pageLoaded(PageLoadedEventHandler);
function PageLoadedEventHandler() {
// custom script
}
</script>

this will be execute after updatePanel... ;-)


hello.

well, if you're chaging UI during a partial request, you do really need to use an updatepanel. if you only need to check for values, why not pass them from the client to server as method parameters?
That's obvious... of course you need an UpdatePanel, but I'm not going to use triggers... that is completely useless.

What? I'm confused... how does that update a GridView on a whim?

I simply want to update a control from ECMAScript. There are many things that our library does to the application, so giving ALL the control to Atlas would be pointless. I need something that will allow a control to be updates via a simple client-side call, not a trigger which doesn't allow your own manipulations, or the useless PageMethods which in no way allows for updating of controls.

For now I'm just going to do this manually... have an ASPX page with the GridView and have an XmlHttp call to the page and do a screenscrape of the control, do a simple DOM parse, and DOM child. Not that difficult... I was just hoping Atlas did this for you as not everyone who works on this application knows Ajax programming as well as I do.

PageMethods and Exceptions

Hi,
i was looking for information about how to manage Exception throwed by page methods.
I mean, is there any way in the javascript function that is called on failure of the page method to know the type of the exception throwed?
I know that the object passed as parameter to this function has the get_message() method to get the exception's message, but i can't find any other information about it.

sorry for my poor english and thanks for your answers,

bye

Well, frankly your best option is to not throw them. Either handle them inside the page method or else have some kind of catchall error handler for the page / app (or both). In terms of debugging, you can then (in the catch block) log on the server any info you might want to, and not worry about whether the javascript object will support inspection of the core problem or not.


Say you call a page method like this:
function UpdateTime()
{
PageMethods.GetCurrentDate(OnSucceeded, OnFailed);
}
function OnSucceeded(result, userContext, methodName)
{
$get('Label1').innerHTML = result;
}
function OnFailed(error, userContext, methodName)
{
$get('Label1').innerHTML = "An error occured.";
}

OnFailed will be called anytime there's an exception thrown from the web method. "error" contains several useful properties, such as error._exceptionType, error._message, and error_stackTrace.


FYI the Exception Type, StackTrace etc is not the server side objects, instead it is the client info.


gt1329a:

"error" contains several useful properties, such aserror._exceptionType, error._message, and error_stackTrace.

thank you, this is what i was looking for!

but before i mark this thread as "resolved" there's another question a want to ask you:

where did u find those information? 'cause i look for them for pretty long time but i culdn't find anything but the get_message() method.


Checkouthttp://www.asp.net/ajax/documentation/live/ClientReference/Global/JavascriptTypeExtensions/ErrorTypeExt/default.aspx andhttp://msdn2.microsoft.com/en-us/library/b8664205-d28e-4cde-bd76-69173ff72dea.aspx


thanks a lot to all of you


YaWW:

where did u find those information? 'cause i look for them for pretty long time but i culdn't find anything but the get_message() method.

I find it easier toexplore the DOM with a JavaScript debugger, than to read the docs. That's how I found the exception information. I set a breakpoint on the client side error handler and then looked at what information was returned.

Monday, March 26, 2012

PageMethods is undefined

Hi there

I have a simple page that calls a server side page method via the scriptmanager / page methods detailed on this site. This has worked fine recently when until I updated our dev team to use the new asp.net ajax 1.0.

Once I'd updated my web.config as per the doco all works fine on my pc but on all others when I call the server side method I get the client script error " PageMethods Is 'Undefined' ". I am sure this is an environmental issue but I would appreciate any tips on how to figure out what is going on!

To update one of the boxes (eg our test server) I did the following steps.

    Uninstall previous ASP.net Ajax (RC1)Install new versionUpdate web.config as per instructions in the doco

Here is my source code

<%@dotnet.itags.org. Page Language="C#" AutoEventWireup="true" CodeFile="AddNote.aspx.cs" Inherits="controls_AddNote" EnableViewState="true" %><%@dotnet.itags.org. Import Namespace="System.Web.Services" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title>Add a note to this cell</title> <link rel="stylesheet" type="text/css" href="../css/calumo default.css" /> </head> <body leftmargin="0" rightmargin="0" topmargin="0" bottommargin="0"> <script type="text/javascript" language="javascript"> function PageMethodCall() { var TicketId=parseInt($get("inpTicketId").value); var DataSource=$get("inpDataSrc").value; var Catalog=$get("inpCatalog").value; var Cube=$get("inpCube").value; var DataPointMdx=$get("inpMdx").value; var NoteValue=$get("taNote").value; if(NoteValue.length>0) PageMethods.SendNote(TicketId,DataSource,Catalog,Cube,DataPointMdx,NoteValue,OnSucceeded); } // This is the callback function // that process the page method call // return value. function OnSucceeded(result) { // Display the result. if(result==1) window.close(); else alert("Calumo Server Exception:-" + result); } function Validate() { if($get("taNote").innerText.length>0) $get("btnSend").value="Send"; else $get("btnSend").value="Close"; } </script> <form id="frmAddNote" runat="server" target="_self"><asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager> <input runat="server" type="hidden" id="inpTicketId" value="" /> <input runat="server" type="hidden" id="inpDataSrc" value="" /> <input runat="server" type="hidden" id="inpCatalog" value="" /> <input runat="server" type="hidden" id="inpCube" value="" /> <input runat="server" type="hidden" id="inpMdx" value="" /> <table id="Table1" runat="server" height="100%" width="95%" cellpadding="0" cellspacing="0"> <tr id="trHeader" > <td><img alt="" src="../images/Calumo/top_bar.jpg"/></td> </tr> <tr><td> </td></tr> <tr id="trBody"> <td id="tdNoteArea" align="center">  <textarea cols="10" runat="server" style="height:100%;width:650px;" id="taNote" rows="10" ></textarea> </td> </tr> <tr><td> </td></tr> <tr id="trFooter"> <td style="padding-right:20px;width:100%" id="tdClose" align="right"> <input id="btnSend" type="button" value="Send" onclick="javascript:PageMethodCall();" /> </td> </tr> </table> </form> </body></html>

and here is the server code

 [WebMethod()]public static string SendNote(int TicketId,string DataSource,string Catalog,string Cube,string DataPointMdx,string NoteValue) {try {// Add a note to the server code and return a sample msgreturn"It Works"; }catch(Exception ex) {return ex.Message; } }

Any hints on troubleshooting would be greatly appreciated

Thanks in advance

Graham

Try settingEnablePageMethods="true"

<

asp:ScriptManagerID="ScriptManager1"runat="server"EnablePageMethods="true">

Yep thats it! Would probably be a good idea to add to the update doco

thanks


Was that missing in the documentation somewhere? We'll happily fix it... just point me to where it's missing.


There should probably be a section on Page Methods...in the docs that is accessible by the left hand side index..

Also, for whatever reason - the UpdatePanels UpdateMode is set by default to "Always" a special highlight in the docs should indicate this is the default and to change it to conditional for partial rendering / asynchronous updates. I was kinda surprised when investigating some of the post and issues on always full postbacks - that when I just dragged and dropped an updatepanel in designer - it defaulted to "Always"... kinda missing the purpose I think as if you are releasing this - then the default should err on side of the Ajax technology it is supposed to provide and this is a change I think from the RCs but not positive as I always declare everything anyways... but alot of folks just drag and drop and assume they are getting the settings for exploiting the Ajax enviroment...


I followed this docohttp://ajax.asp.net/documentation/Migration_Guide_RC_to_RTM.aspx

and unless I skipped over it I didn't find any reference to the EnablePageMethods

thanks again


I agree! Where can you find reference and documentation to PageMethods ? I use the block of code on this post as my reference right now ..


http://ajax.asp.net/docs/tutorials/ExposingWebServicesToAJAXTutorial.aspx

I agree this would be nice to see in the "differences" docs. I'll see if it can be added.


Just wanted to say thanks, all.

I was trying to get a method to run from a page (both in the .aspx file and in the code behind) and kept getting the js error of "PageMethods" not found. Adding that attribute to the ScriptManager cured the problem.

Pete.