简体   繁体   English

如何从外部JavaScript文件访问服务器控件?

[英]How can i access server controls from my external JavaScript file?

When i use this "#<%= txtNumberOfDrugsInKit.ClientID %>" , i can access the server control from my JQuery script; 当我使用此"#<%= txtNumberOfDrugsInKit.ClientID %>" ,我可以从我的JQuery脚本访问服务器控件; but when i put this in an external script file, it does not work. 但是当我把它放在一个外部脚本文件中时,它不起作用。

How can i access an asp textbox from my external JavaScript file? 如何从外部JavaScript文件访问ASP文本框? I cant believe this is not working. 我不能相信这是行不通的。

What I do is put a script on my main page (not in an external file) that consists solely of an object definition like this: 我要做的是将一个脚本放在我的主页上(而不是在外部文件中),该脚本仅包含这样的对象定义:

var Controls = {
    'Name':<%="'" + txtNumberOfDrugsInKit.ClientID%>',
    'OtherName':<%="'" + otherControl.ClientID%>'
};

The trick here is that you have to put this in the header or you can't use it from external files, and so you have to add runat="server" to your head element declaration. 这里的窍门是必须将其放在标题中,否则不能从外部文件中使用它,因此必须将runat="server"添加到head元素声明中。 This also explains why I use an object rather than simple variable names; 这也解释了为什么我使用对象而不是简单的变量名。 it minimizes the chance for a naming collision elsewhere (I only have the "Controls" name to worry about). 它将其他地方发生命名冲突的机会降到最低(我只需要担心“控件”名称)。

Then I can use that Controls object in an external script like this: 然后,我可以在外部脚本中使用该Controls对象,如下所示:

var OtherElement = document.getElementById(Controls.OtherName);

or 要么

var jQueryObj = $('#' + Controls.OtherName);

See another example here: 在这里查看另一个示例:
Can I count on ctl00_PagePlaceHolder_myId staying the same? 我可以指望ctl00_PagePlaceHolder_myId保持不变吗?

I think article will help you out. 我认为文章会帮助您。 http://lanitdev.wordpress.com/2009/06/08/extending-jquery-to-select-asp-controls/ + http://lanitdev.wordpress.com/2009/06/08/extending-jquery-to-select-asp-controls/ +

I have used this solution and it works very well to select server controls without having to add extra markup. 我已经使用了此解决方案,并且无需添加额外的标记即可很好地选择服务器控件。

I assume #<%= txtNumberOfDrugsInKit.ClientID %> gets evaluated on the server side , so of course it doesnt work in an external js, because those are served statically, not processed on the server side. 我假设#<%= txtNumberOfDrugsInKit.ClientID %>服务器端进行了评估,因此当然在外部js中不起作用,因为这些是静态提供的,而不是在服务器端进行处理的。

I think you are not fully aware of what code runs on the server and what runs on the client. 我认为您不完全了解服务器上运行的代码和客户端上运行的代码。 One way of passing that data into your external script is putting it into a hidden input, or a hidden div, and then reading from that DOM element in your external script. 将数据传递到外部脚本中的一种方法是将其放入隐藏的输入或隐藏的div中,然后从外部脚本中读取该DOM元素。

"#<%= txtNumberOfDrugsInKit.ClientID %>" is most likely not executed in a external JS file as it is not part of the ASP.NET page You'll have to "hardcode" the ID. “#<%= txtNumberOfDrugsInKit.ClientID%>”很可能不在外部JS文件中执行,因为它不是ASP.NET页的一部分。您必须“硬编码” ID。

But if you have problems with "weird" ID's that ASP.NET generates, then you can put the ClientID into a hidden field and read it from there in your external JS file 但是,如果ASP.NET生成的“怪异” ID出现问题,则可以将ClientID放入隐藏字段中,然后从外部JS文件中读取该字段。

What I've done in the past to get around this is to use a CSS name instead. 我过去为解决此问题所做的就是改用CSS名称。 On your txtNumberOfDrugsInKit control, you could do something like the following... 在txtNumberOfDrugsInKit控件上,您可以执行以下操作...

<asp:TextBox ID="txtNumberOfDrugsInKit" CssClass="txtNumberOfDrugsInKit" ... />

And then in your jQuery, instead of referencing it as $("#<%= txtNumberOfDrugsInKit.ClientID %>") , you can reference it by class name instead. 然后在您的jQuery中,您可以通过类名来引用它,而不是将它引用为$("#<%= txtNumberOfDrugsInKit.ClientID %>") In that case, you'd just do $(".txtNumberOfDrugsInKit") . 在这种情况下,您只需要执行$(".txtNumberOfDrugsInKit") As long as you keep those selectors unique on your page - picking up just the one control with it is no problem at all. 只要您在页面上保持这些选择器的唯一性-只需选择一个控件就完全没有问题。

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

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