Is it possible to get the property(get; set; ) say Name from code behind(aspx.cs) file into jquery?
Yes, depending on your framework:
<script type="text/javascript">
var someProp = "<% = this.PropertyName; %>";
</script>
You may run into encoding issues, so make sure you escape the value for javascript.
Yep. If your script is inline in the aspx page, simply use the ASP tags to get it into the script.
<html.....
<script type="text/javascript">
public function myJSFunction()
{
var x = '<%= Name %>';
...
}
</script>
If your script isn't inline, i.e. it's coming from a separate javascript file, you have a couple of options.
You can add the variables that you need into the page using the technique above, and then your external javacript can reference it.
You can make the external javascript file a web resource by changing it's content type to "Embedded Resource" in the properties window, and then using the following:
[assembly: WebResource("myJS.js", "text/javascript", PerformSubstitution=true)]
The use of the "PerformSubstitution" flag on a WebResourceAttribute will make it so that the file is run through the asp parser before it is rendered, and it will replace any ASP tags it find in the file. Web resources have some drawbacks though so you should read up on them before deciding to use them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With