简体   繁体   English

内联C# <noscript> 标签

[英]Inline C# in <noscript> tags

I need to use the tag in ASP.NET in order to call a C# method from the code behind class, but it seems to be calling it irrespective of whether JavaScript is enabled or disabled. 我需要在ASP.NET中使用该标记,以便从类背后的代码中调用C#方法,但是无论是否启用JavaScript,它似乎都在调用它。 Other text inside the tags works as expected. 标签内的其他文本可以正常工作。

Below is the code I am using, is there something I am doing wrong? 以下是我正在使用的代码,我在做错什么吗?

Thanks 谢谢

<noscript>
  other text here
  <% DisableTextBox("textBoxId"); %>
</noscript>

Solution TextBox was disabled server-side, and enabled client-side using the following JavaScript/jQuery: 解决方案 TextBox在服务器端被禁用,并使用以下JavaScript / jQuery启用了客户端:

$("#txtTotalQuantScore").prop("disabled", false);

The ASP.NET view engine knows nothing about the noscript tag you've included. ASP.NET视图引擎对您包含的noscript标记一无所知。

All server side code will be executed by the server and the result written to the HTML page. 服务器将执行所有服务器端代码,并将结果写入HTML页面。 The noscript tag can not be used as a C# if statement. noscript标记不能用作C#if语句。

If you want to disable something when Javascript isn't available you can do so client side. 如果您想在Javascript不可用时禁用某些功能,可以在客户端进行。

One way would be to set the element in question to hidden in your CSS and then use Javascript to unhide it. 一种方法是将有问题的元素设置为隐藏在CSS中,然后使用Javascript取消隐藏它。

As @Jamie Dixon has described, what you are trying to do is not possible. 正如@Jamie Dixon所描述的,您试图做的事情是不可能的。

Looking at your method name, it appears you are trying to disable a textbox if the browser doesn't support javascript. 查看您的方法名称,如果浏览器不支持javascript,您似乎正在尝试禁用文本框。

Therefore I would recommend always disabling this textbox on the server-side, and enabling it with javascript on the client-side. 因此,我建议始终在服务器端禁用此文本框,并在客户端使用javascript启用它。

This way, only browsers supporting javascript will have the textbox enabled. 这样,只有支持javascript的浏览器才会启用文本框。

I would add you can detect server-side that a browser supports scripting, but there's no easy way to tell whether or not the user has enabled scripting. 我要补充一下,您可以检测到服务器端浏览器支持脚本,但是没有简单的方法来判断用户是否已启用脚本。

You can resort to a trick like setting a value in a hidden field through a script (0 for no scripting, 1 for enabled, for example), but this isn't always reliable. 您可以采取一些技巧,例如通过脚本在隐藏字段中设置值(例如0表示没有脚本编写,1表示启用),但这并不总是可靠的。

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

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