Click here to Skip to main content
15,895,462 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi The below code raise error para is undefined can any body help me why?

JavaScript
function abc() {  // java script code
    var   para=new Object();
    para="manoj";
    var retval=<%=aaa(para) %>;
    alert(retval);
    }


public String aaa(string aa)  // c# code
    {
        return (aa == "manoj" ? "manoj" : "kumar");
    }
Posted
Updated 23-Nov-10 4:24am
v2
Comments
Henry Minute 23-Nov-10 10:26am    
For your future reference. If you enclose your code snippets in <pre>your code......</pre> tags, it makes it easier to read and helps preserve formatting. :)

When you place "para" inside the <% %> brackets, and your javascript resides on your .asp(x) page, then you must have the "para" variable defined in your code behind. Anything in between these brackets is interpreted by the server and not the javascript interpreter.
 
Share this answer
 
Comments
Manfred Rudolf Bihy 23-Nov-10 11:00am    
I think it's some deprived (depraved) little soul that is suffering from low self esteem. So what! Take my 5 instead :)
Anyhow! We know we're right and sometimes that just has to be enough.
Hi Kumar,

<![CDATA[<%=aaa(para) %>]]>
is code in a server side, which means
this will be executed on the server and not on the client (like the
javascript block that server side code tag is written in).
So the problem is, I assume, that there is no variable para declared in
your code behind file and that's what the compiler is complaining about.

Hope this helps!

Cheers

Manfred
 
Share this answer
 
Comments
fjdiewornncalwe 23-Nov-10 10:54am    
Apparently the uni-voter has hit this thread... BTW... Nice catch...(I obviously agree with your answer as well)
The problem is the following line

var retval=<%=aaa(para) %>;


Because you are trying to use a variable defined in javascript inside what is essentially a C# (I assume) code block. The variable para does not exist within this section.

One way to get around this (May work for you)

function abc(){
  var jsVar = "manoj";
  var hiddenControl = <%= inpHdn.ClientID %>;
  document.getElementById(hiddenControl).value=jsVar;
}


Then use a form to store the value:

<form id="frm">
  <input type="hidden" id="inpHdn"  runat="server" />
</form>


Then in your code-behind you could use this:

private void aaa()
{
  return (inpHdn.Value == "manoj" ? "manoj" : "kumar");
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900