通过Javascript访问asp.net内容页面中的控件

发布于 2024-09-04 04:28:02 字数 450 浏览 7 评论 0原文

我的内容页面中有一个表单,我正在通过 Javascript 对其进行一些客户端验证。如果我将 JS 代码直接放在内容页面中,Javascript 的行为将符合预期。但是,如果我将 JS 代码放在它自己的文件中,并尝试从内容/母版页(通过脚本标记的 src 属性)访问该代码,那么当调用 JS 中的验证函数时,我会收到运行时错误。

具体来说,我收到以下错误。 Microsoft JScript 运行时错误:此行反对预期/必需 - document.getElementById('<%=txtemailId.ClientID %>').value

txtemailId 位于内容页面中。

Javascript 代码放置在validation.js 中并通过母版页访问。

我猜测的原因是,当.net解析文件时,它无法用稍后生成的客户端值替换txtemailId.ClientID。那么,应该如何去做呢?谢谢!

I have a form in my content page for which I am doing some client side validations via Javascript. The Javascript behaves as expected if I place the JS code directly in the content page. But if I place the JS code in it's own file and try accessing that from the content/master page (through the script tag's src attribute), I get a run time error when the validation function in JS being called.

To be specific, I get the below error.
Microsoft JScript runtime error: Objected expected/required at this line - document.getElementById('<%=txtemailId.ClientID %>').value

txtemailId is in the content page.

Javascript code is placed in validation.js and accessed via master page.

The reason I guess is that when .net is parsing the files, it is unable to substitute txtemailId.ClientID with the client side value that would be generated later on. So, how should one go about it? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

空宴 2024-09-11 04:28:02

答案很简单。

在您的内容页面中声明一个内联 JScript 变量,确保该变量位于您的标记上方。

<script>
  var emailClientId = <%=txtemailId.ClientID%>;
</script>

在 include.js 文件中使用全局范围的 emailClientId 变量。

显然,这有点笨拙,因为并非所有 JScript 代码都包含在 include.js 文件中,这使得调试/诊断变得困难,因为不清楚该值具体来自何处。您应该在代码中清楚地注释/记录这一点,以便将来更轻松地维护代码库。

The answer to this is quite simple.

Within your content page declare an in-line JScript variable, make sure this is above your tag.

<script>
  var emailClientId = <%=txtemailId.ClientID%>;
</script>

Within your include.js file make use of the globally scoped emailClientId variable.

Obviously this is a bit clumsy because not all of the JScript code is contained within the include.js file, which makes it difficult to debug / diagnose as it is not clear specifically where the value is coming from. You should clearly comment / document this within your code to make maintenance of he codebase easier in the future.

泪冰清 2024-09-11 04:28:02

您是对的,如果代码 <%=txtemailId.ClientID %> 位于 ASP.Net 文件中,则该代码只会被 ASP.Net 替换为真实的客户端 ID。因此,如果您将此 javascript 放在单独的 .js 文件中,ASP.Net 将对此一无所知,并且代码将不会被解析。

实现此目的的最简单方法是将控件 ID 设置为函数的参数。这样,您就可以在 .aspx(或 .ascx)文件中包含调用 javascript 代码以及 <%=txtemailId.ClientID %> 代码。

You're right, the code <%=txtemailId.ClientID %> will only get replaced with the real client ID by ASP.Net if this code is in an ASP.Net file. So if you put this javascript in a separate .js file, ASP.Net will know nothing about it, and the code will not be parsed.

The easiest way to achieve this is to make the control IDs parameters of your functions. This way you can have the calling javascript code in your .aspx (or .ascx) file, along with the <%=txtemailId.ClientID %> code.

一片旧的回忆 2024-09-11 04:28:02

独立的 .js 文件不通过 ASPX 解析器运行,因此不会评估您的表达式。

要解决此问题,您可以使用控件的 ID 在 ASPX 文件中设置全局变量(或函数参数或其他内容),然后在独立的 .js 文件中使用它们。

或者,您可以使用类名(不会被破坏)而不是 ID。 (使用 jQuery 这非常简单)

Standalone .js files are not run through the ASPX parser, so your expression is not being evaluated.

To solve the problem, you could set global variables (or function parameters, or something else) in the ASPX files with the IDs of the controls, then use them in the standalone .js file.

Alternatively, you could uses class names (which don't get mangled) instead of IDs. (This is very simple using jQuery)

心安伴我暖 2024-09-11 04:28:02

这样的方法最适合您想要实现的目标:

validation.js

var Validation = {
     EmailId: null,

     Validate: function() {
          var email = document.getElementById(EmailId).value;
     }
}

page.aspx

Validation.EmailId = '<%=txtemailId.ClientID %>'; //initialize
Validation.Validate(); //whenever you want to validate

An approach like this is best suited for something you want to achieve:

validation.js

var Validation = {
     EmailId: null,

     Validate: function() {
          var email = document.getElementById(EmailId).value;
     }
}

page.aspx

Validation.EmailId = '<%=txtemailId.ClientID %>'; //initialize
Validation.Validate(); //whenever you want to validate
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文