1

I have a Javascript function and i want to pass a parameter from javascript to asp.net. my sample code like below:

function ConfirmMessage(message) {
            var msg = "<%= Efe.UI.WebApplication.AppCode.HelperClass.GetScreenMessage(message); %>"

alert.confirm(msg);

}

But I am getting error at "message" parameter. The error is:

The "name" does not exist in the current context.

How can I pass parameter from javascript to asp.net?

2
  • In some scenarios it's better to take a step back from the problem and adjust the overall approach, I suspect this might be one of those scenarios. What are you ultimately trying to do? You're mixing server-side code and C# too much here. It doesn't feel right. Commented Dec 9, 2014 at 9:00
  • You might read up on Ajax with ASP.NET Commented Dec 9, 2014 at 9:04

2 Answers 2

2

You cannot pass parameter from javascript to server side at inline code. You should use jquery ajax for that.

var message = "value";
$.get(
url: "/yoururl/GetScreenMessage",
data: {message : message},
success: function(data){
    alert(data);    
});
Sign up to request clarification or add additional context in comments.

4 Comments

not jQuery per se, but you'll need a different approach indeed
Thanks for your reply but it didnt worked. I thing, i have to try another way. Damn asp.net dont allow to pass parameter javascript to asp.net.
Evet @MahmutEFE, because server side rendered before javascript. If you setup relevant url, this should work.
thanks, i searched your approach on internet and finally i solved my problem. with WebMethod..
0

You're trying to dynamically call a server side function with a parameter, but the <%= %> part is actually a Response.Write. So, during the page load the page will try to render with the Efe.UI.WebApplication.*xxx* method's output. That won't work.

You'll need a different solution all together. Most likely you'll want to use a "REST-like" service call to get the result of the "message" parameter. I would suggest you take a look at AJAX : http://msdn.microsoft.com/en-us/library/bb398874%28v=vs.100%29.aspx and http://www.codeproject.com/Articles/29400/Ajax-Quick-Start-FAQ

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.