Question

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. 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:

$("#txtTotalQuantScore").prop("disabled", false);
Was it helpful?

Solution

The ASP.NET view engine knows nothing about the noscript tag you've included.

All server side code will be executed by the server and the result written to the HTML page. The noscript tag can not be used as a C# if statement.

If you want to disable something when Javascript isn't available you can do so client side.

One way would be to set the element in question to hidden in your CSS and then use Javascript to unhide it.

OTHER TIPS

As @Jamie Dixon has described, what you are trying to do is not possible.

Looking at your method name, it appears you are trying to disable a textbox if the browser doesn't support javascript.

Therefore I would recommend always disabling this textbox on the server-side, and enabling it with javascript on the client-side.

This way, only browsers supporting javascript will have the textbox enabled.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top