views:

494

answers:

5

I would like to burn in a c# code behind property into the javascript alert(<%= someProperty%>); . For some reason it is not working. Is there a way you can burn the codebehind property into the javascript? thanks

+1  A: 

I think you are missing the ' try like this -->alert('<%= someProperty%>');

Sergio
I have tried that. No go
if you property has a ' in it it could break the javascript.Keep in mind that any javascript error may stop the rest from running.
Sergio
the property consist of guid string in it. I work my way around using registerHiddenField method.
+1  A: 

Maybe it is a silly suggestion. Have you tried

alert("<%= someProperty%>");

?

Eineki
I have tried that also and it didn't work.
As Josh Stodola stated, where is the code?
Eineki
+2  A: 

Not sure if this works in your situation, but I think your best bet might be to attach the javascript to the event dynamically in your code behind on page load and just set the parameter value at that point.

For example:

btnSubmit.Attributes.Add("onclick","alert(" + someProperty + ");");
jeremcc
A: 

To use the <%= => ASP.NET expressions, your Javascript cannot reside in an external .js file. It has to be part of the ASPX markup in order for the scriplet to be interpreted.

<head>
  <script type="text/javascript">
    alert(escape('<%=someProperty%>'));
  </script>
</head>

Best regards...

Josh Stodola
A: 

You can use the ClientScript.RegisterExpandoAttribute method to register a C# code behind property into JavaScript property.

azamsharp