0

i tried pass parameter from jscript to code behind, but it don't work. My code:

[C#]

[WebMethod]
public static String sendEmail(String str)
{
     ...
     msg.body = str;
     ...
     return str;
}

[jscript]

If i use function alert, it return null

function onOkClickButton()
{
    var content = "Hello";
    alert("<%=sendEmail(" + content + ")%>");
}

if i use PageMethod, my email's body is null

<asp:ScriptManager enablePageMethods="true"></asp:ScriptManager> 
function onOkClickButton()
{
    var content = "Hello";
    PageMethods.sendEmail(content, onSuccess, onError);
}
function onSuccess(text)
{
    alert(text)
}

function onError(text)
{
    alert(text)
}

Can you help me?

1
  • Have you done this: <asp:ScriptManager ID="ScriptManager1" EnablePageMethods="true" EnablePartialRendering="true" runat="server" /> Commented Jul 3, 2012 at 23:12

2 Answers 2

1

jQuery ajax would be the easiest method, IMO:

function onOkClickButton()
{
    var content = "Hello";

    $.ajax({
      type: "POST",
      url: "urlToPageHostingSendEmail.aspx/sendEmail",
      data: { str: content }
    }).success(function( msg ) {
      alert( msg );
    });
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, Function sendEmail was placed in MasterPage. But when i set url: "MasterPage.master.aspx/sendEmail", it don't work.
Having it in the master page feels dirty to me, and I'm not 100% certain it will work, but it very well may. I'm not currently in a position to test it. This page is a good resource for properly configuring WebMethods in ASP.NET applications
0

You could use jquery to call your page method:

call page method without script manager from jquery

or

http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/

Also have a look at this video:

http://www.highoncoding.com/Articles/430_Calling_Page_Methods_Using_JQuery_and_Returning_JSON_Result.aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.