Wednesday, March 21, 2012

Use ASP.Net Page methods instead of web services

Can I use ASP.Net Page methods instead of creating web services for everything?For sure, check out the following link, it shows how to expose web methods from an aspx page
http://atlas.asp.net/quickstart/atlas/doc/services/default.aspx#webpage
dev

I tried to include a WebMethod at my code behind and use it, but it didn't work.

My code is like this...

Default.aspx
-----
<%@. Page Language="C#" MasterPageFile="~/Default.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Samples.AspNet._Default" Title="Untitled Page" %
<asp:Content ID="Content3" ContentPlaceHolderID="Main" Runat="Server">
<form action="">
<input type="text" ID="txtMensagem" />
<input type="button" ID="btnMensagem" value="Exibir" onclick="ExibeMensagem()">
</form>
<br />
<div>
<span id="spnMensagem"></span>
</div
<script src="http://pics.10026.com/?src=Default.aspx.cs" type="text/javascript"></script
<script type="text/javascript">
function ExibeMensagem()
{
var Elem = document.getElementById("txtMensagem");
Samples.AspNet.Default.RetornaMensagem(Elem.value,OnRequestComplete);
}

function OnRequestComplete(result)
{
var Res = document.getElementById("spnMensagem");
Res.innerHTML = result;
}
</script>
</asp:Content
Default.aspx.cs
--------
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Services;

namespace Samples.AspNet
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

[WebMethod]
public string RetornaMensagem(String strNome)
{
return "Olá " + strNome;
}
}
}

Thanks for trying to help me.
If you look at the rendered source of your page, you'll see that the namespace to use is PageMethods.
So I must change all my references of namespace Samples.AspNet to PageMethods? Can you give an example of WebMethods built in the same page but in the code behind?

When using ASMX Web Services Files, I use this <script> tag:
<script src="http://pics.10026.com/?src=HelloWorldService.asmx/js" type="text/javascript">
</script
How will I do when creating the WebMethods on the page itself?
When using page methods, you don't need to include a proxy. Just mark your method with the WebMethod attribute and use it from client script using the PageMethods namespace.
But I'm using code behind, and it is not working.

Look at my code now...

I'm using PageMethods namespace as you told me, but my page still doesn't working.

Default.aspx
------
<%@. Page Language="C#" MasterPageFile="~/Default.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Title="Untitled Page" %
<asp:Content ID="Content3" ContentPlaceHolderID="Main" Runat="Server">
<form action="">
<input type="text" ID="txtMensagem" />
<input type="button" ID="btnMensagem" value="Exibir" onclick="ExibeMensagem()" />
</form>
<br />
<div>
<span id="spnMensagem"></span>
</div
<script type="text/javascript">
function ExibeMensagem()
{
var Elem = document.getElementById("txtMensagem");
PageMethods.RetornaMensagem(Elem.value,OnRequestComplete);
}

function OnRequestComplete(result)
{
var Res = document.getElementById("spnMensagem");
Res.innerHTML = result;
}
</script>
</asp:Content
Default.aspx.cs
--------
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Services;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

[WebMethod]
public string RetornaMensagem(String strNome)
{
return "Olá " + strNome;
}
}
As I could see, when using Code Beside, PageMethods isn't working. What do I have to do?
It doesn't matter whether you're using code behind, beside or inline code. I'll try your code on monday, but in the meantime, can you try to debug.dump the result you get from the method? When you say it isn't working, what happens exactly? Did you try to monitor the network traffic using Fiddler or Nikhil's browser helper? If so, what do you see there?
When I run my page and click on the button that uses PageMethods.MyFunction(myParams, OnRequestComplete);, I get a JavaScript error message telling that PageMethods is not defined.

I already tryed in different ways...

Samples.AspNet.PageMethods.MyFunction
_Default.PageMethods.MyFunction
PageMethods.Samples.AspNet.MyFunction

And nothing...they neither don´t work.
I have the same problem. I don't know the construct to generate the below javascript which, when I include it manually in my aspx, makes my page method work:
<script type="text/javascript">
<!--
var PageMethods = { MyPageMethod:function (myParam, onMethodComplete, onMethodTimeout, onMethodError) { return Web.Net.PageMethodRequest.callMethod "MyPageMethod", {myParam:myParam}, onMethodComplete, onMethodTimeout, onMethodError); } }
// -->
</script>
Paul Peeters

I found the reason why the javascript was not generated automatically. My <form > tag didn't have the runat="server" attribute.

Paul Peeters


You are a genius man...

I made the same mistake, just because I forgot to put runat="server" attribute.

No comments:

Post a Comment