简体   繁体   English

AJAX和ASP.net,引用外部文件中的服务器控件

[英]AJAX & ASP.net, Referencing server controls in external file

I've got some JavaScript in an ASP.NET page that looks like this: 我在ASP.NET页面中有一些JavaScript,如下所示:

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

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. 我现在编写了许多函数,使用了这种语法,我想集中我的JavaScript并将其移动到外部JavaScript文件中。 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? 有什么建议么?

You should create a javascript method from inside the usercontol which returns the client side element. 您应该从usercontol内部创建一个返回客户端元素的javascript方法。 Then in your other page/control, just access that method 然后在您的其他页面/控件中,只需访问该方法

In User Control 在用户控制中

 <script language="javascript">
     function GetTopics() {
          return = $get('<%=Topics.ClientID %>');
     } 
 </script>

In other page/control 在其他页面/控件中

 <script language="javascript">
     var list = GetTopics();
 </script>

Edit - The problem you are facing is you need Topics.ClientID where it doesn't exist. 编辑 - 您面临的问题是您需要不存在的Topics.ClientID。 So the only real way to bridge that gap is to put it in a common place. 因此,弥合这一差距的唯一真正方法是将其置于一个共同的位置。 If you really don't want to do that, you can try and select your element by some other criteria. 如果您真的不想这样做,可以尝试通过其他一些标准选择元素。 If you are using jQuery, you could mark an element with a class of Topics then find it with $(".Topics"). 如果您使用的是jQuery,则可以使用一类主题标记一个元素,然后使用$(“。Topics”)找到它。

It's a common problem for ASP.NET JS development. 这是ASP.NET JS开发的常见问题。 As for me, I'm using same approach each time and it looks fine. 至于我,我每次都使用相同的方法,看起来很好。

I'm used to OOP in Javascript, so most my JS external files look like: 我已经习惯了Javascript中的OOP,所以我的大多数JS外部文件看起来像:

function CouponManager()
{
}

And in .aspx code i do: 在.aspx代码我做:

<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);
}

And from .aspx code I do the following: 从.aspx代码我做以下事情:

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

This approach allows me to separate JS from .aspx page and have all server control dependencies in a single tag. 这种方法允许我将JS与.aspx页面分开,并将所有服务器控件依赖项放在一个标记中。

if you know that you only have one server control called "Topics" per page, and you use naming conventions you can inherit from whatever the control Topics is (maybe it's a HiddenField? you don't specify) and override its ClientId getter to return its server id like this: 如果您知道每个页面只有一个名为“Topics”的服务器控件,并且您使用命名约定,则可以从控件主题继承(可能是HiddenField?您没有指定)并覆盖其ClientId getter以返回它的服务器ID如下:

http://andreascode.wordpress.com/2009/04/27/tiny-drips-of-aspnet-juice/ http://andreascode.wordpress.com/2009/04/27/tiny-drips-of-aspnet-juice/

then you can know in your javascript files that there will be a hidden field in the page with the id set to "Topics" and use that directly. 然后你可以在你的javascript文件中知道页面中有一个隐藏字段,其id设置为“Topics”并直接使用它。

depending on your domain/situation this could either save you a lot of time or screw you over big time. 根据您的域名/情况,这可能会为您节省大量时间或让您度过难关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM