How to pass parameter from javascript to code behind?
-
18-06-2021 - |
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?
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 );
});
}
OTHER TIPS
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:
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow