Question

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?

Was it helpful?

Solution

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 );
    });
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top