views:

368

answers:

5

Hello all,

I am very new to jQuery and have got a quick question.

I wish to use my server side classes in my jQuery code, something similar to this:

$(document).ready(function() {
var temp = <%# myClass.Id %>;
})

Is this possible? if so, how?

Thank you very much

This is the later question I refined my former question to:

I'm sorry, I think I didn't explain myself too well... I've got a class name User. It's a class I built in my business logic.

I've got a web page named UserProfile, inside it I've got the following property exposing the current logged in user:

    public BL.User CurrUser        {                get { return (BL.User)Session["currUser"]; }        }I want to be able to access this User class from my aspx page using Jquery. How do I do that?
+2  A: 

This will only work if your javascript is embedded in your source files (e.g. the .aspx files):

<script type="text/javascript">
    var id = <%# myClass.Id %>; // store as raw value
    var id_string = '<%# myClass.Id %>'; // store in a string
</script>
Johnny G
A: 

if your javascript is on your aspx page, this will work just fine. However, you should put your javascript in its own .js files.

I include a script section on my aspx page to hold variables that I then use in my .js files.

like this:

<script>
   var classID = <%# myClass.Id %>;
</script>
Geoff
See @Roberts post about databinding syntax
Russ Bradberry
yeah, that's what i get for just copying from the original post
Geoff
+1  A: 

As others have said, if the JavaScript is in your aspx page, then using server tags will work fine.

If you have your jQuery in an external script file, then you could put this in your aspx page

<script type="text/javascript">
var myClass = $('#<%= myClass.ClientID %>');
</script>

and then use the variable in your external script file

$(function() {     
    myClass.click( function() { ... });
});

For other options take a look at this question and answer - How to stop ASP.NET from changing ids in order to use jQuery

Russ Cam
+1 Yep, you gotta use the ClientID property of your server side object. Otherwise the HTML rendered will be useless.
Dave
+2  A: 

The databinding syntax

<%# MyStaticClass.MyProperty %>

will only work if you call DataBind on the container (page). What you're after is most likely the following syntax:

<%= MyStaticClass.MyProperty %>

which will also give you access to you page / control members

<%= this.MyPageProperty %>

As was already mentioned you should really assign those values to java script variables and pass those variables to you JS functions.

Robert Wilczynski
A: 

I'm sorry, I think I didn't explain myself too well... I've got a class name User. It's a class I built in my business logic.

I've got a web page named UserProfile, inside it I've got the following property exposing the current logged in user:

 public BL.User CurrUser
 {
  get { return (BL.User)Session["currUser"]; }
 }

I want to be able to access this User class from my aspx page using Jquery. How do I do that?

@vondip - I don't believe it's possible to be able to access server side class properties from the client-side. You could write out the value to a hidden field and send it to the client (you might need to override ToString() for BL.User), but I'd be hesitant to recommend doing so looking at the value returned by the getter. What do you need to do this for? I'd also recommend editing your original question and appending this extra information to it :)
Russ Cam