odez
odez

Reputation:

Grab code behind property in Javascript?

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

Upvotes: 4

Views: 3698

Answers (5)

azamsharp
azamsharp

Reputation: 20086

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

Upvotes: 0

Josh Stodola
Josh Stodola

Reputation: 82513

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...

Upvotes: 0

jeremcc
jeremcc

Reputation: 8741

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 + ");");

Upvotes: 2

Eineki
Eineki

Reputation: 14959

Maybe it is a silly suggestion. Have you tried

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

?

Upvotes: 1

Sergio
Sergio

Reputation: 8259

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

Upvotes: 1

Related Questions