1

我对 jQuery 很陌生,有一个快速的问题。

我希望在我的 jQuery 代码中使用我的服务器端类,类似于以下内容:

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

这可能吗?如果是这样,如何?

非常感谢

这是我将前一个问题改进为的后一个问题:

对不起,我想我没有很好地解释自己......我有一个类名用户。这是我在业务逻辑中构建的一个类。

我有一个名为 UserProfile 的网页,在其中我有以下属性公开当前登录的用户:

    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?
4

3 回答 3

3

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.

于 2009-06-23T18:39:20.220 回答
2

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

于 2009-06-23T18:38:06.980 回答
2

这仅在您的 javascript 嵌入源文件(例如 .aspx 文件)时才有效:

<script type="text/javascript">
    var id = <%# myClass.Id %>; // store as raw value
    var id_string = '<%# myClass.Id %>'; // store in a string
</script>
于 2009-06-23T18:30:16.070 回答