4

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

2
  • Can you "view source" in the browser and see how the javascript renders? Try running in firefox + firebug and see what the reported error is? Commented Feb 6, 2009 at 15:05
  • Yes, there is a way and you are going in the right direction. Unfortunately, the way question is posed gives too little info about implementation and zero details about the problem. Commented Feb 6, 2009 at 15:10

5 Answers 5

2

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 + ");");
Sign up to request clarification or add additional context in comments.

Comments

1

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

2 Comments

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.
the property consist of guid string in it. I work my way around using registerHiddenField method.
1

Maybe it is a silly suggestion. Have you tried

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

?

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.