AJAX和放大器; ASP.net,服务器引用外部文件控制放大器、服务器、文件、AJAX

2023-09-10 19:42:44 作者:灿烂阳光下的向日葵

我有一些JavaScript的看起来像这样一个ASP.NET页面:

I've got some JavaScript in an ASP.NET page that looks like this:

var list = $get('<%=Topics.ClientID %>');

我有许多功能现在在这里写这个语法时,我想集中我的JavaScript和推动这一到外部JavaScript文件。然而,这打破了,因为'主题'无法找到。

I have many functions written now where this syntax is used, and I would like to centralize my JavaScript and move this into an external JavaScript file. This breaks however, since 'Topics' cannot be found.

是什么让这个工作最好的策略是什么?我想我应该通过控制/控制信息作为参数传递给函数,但我似乎无法得到语法工作。有什么建议?

What is the best strategy for getting this to work? I assume I should pass the control/control information as a parameter to the function, but I can't seem to get the syntax to work. Any suggestions?

推荐答案

这对ASP.NET JS发展的一个普遍问题。至于我,我用同样的方法,每次和它看起来很好。

It's a common problem for ASP.NET JS development. As for me, I'm using same approach each time and it looks fine.

我已经习惯了OOP在Javascript,所以我大部分的JS外部文件如下:

I'm used to OOP in Javascript, so most my JS external files look like:

function CouponManager()
{
}

和中的.aspx code我做的:

And in .aspx code i do:

<script language="javascript">
    var couponManager = new CouponManager();
</script>

如果我需要传递一些参数更改类的声明:

If I need to pass some parameters I change the declaration of class to:

function CouponManager(params)
{
    // .. stuff here

    this.initialize = function(initParams)
    {
       // .. accessing initParams variable for server control IDs
    };

    this.initialize(params);
}

和距离的.aspx code我做了以下内容:

And from .aspx code I do the following:

<script language="javascript">
    var couponManager = new CouponManager
    ({
        txtCouponNameId = '<%= txtCouponName.ClientID %>',
        txtCouponDescriptionId = '<%= txtCouponDescription.ClientID %>'
    });
</script>

这个方法可以让我JS从.aspx页面中分离出来,并拥有所有服务器控件依赖于单个标签。

This approach allows me to separate JS from .aspx page and have all server control dependencies in a single tag.