Detect when JavaScript is disabled in ASP.NET - asp.net

In the Render method of an ASP.NET web-control, I need to alter the output of the Html based on whether JavaScript is enabled or disabled on the clients browser,
Does anyone know the right incantation to figure that out?

The problem with using script to check whether javascript is enabled is that you only find that out after the script hasn't run.
Some solutions try the opposite - they use javascript to set a value and then supply Javascript enabled controls if that value is later detected. However, this fails with javascript-sometimes-enabled tools like the Firefox NoScript plugin.
A more robust solution is to always send the plain-HTML compatible control, and then have javascript run on page load to add the correct event handlers/extra DOM elements/etc.
However I don't know how this fits in with the ASP.NET approach takes to controls.

The noscript tag is used to define an alternate content (text) if a script is NOT executed.
EDIT: Hi Kieron, take a look at this link: Creating a Server Control for JavaScript Testing, and Detect if JavaScript is enabled in ASPX

The browser does not communicate Javascript availability with the request.
One thing that I have done, though I'm not sure it is applicable in your case, is generate the page as if Javascript is not enabled, then have some Javascript that turns off the HTML-only stuff and turns on the HTML-Javascript stuff.
You could also have the first page hit "phone home" via Javascript to record in the session whether Javascript is enabled or not.

This is a way to check when a form is being submitted.
https://web.archive.org/web/20210428071600/http://www.4guysfromrolla.com/webtech/082400-1.shtml
I dont think there is any Request property that will tell you when the page is first requested. There is a property that will tell you if the browser supports JS, but not if its enabled. See Here

What you need to do is this. After much testing and late nights, I decided to use the most simple solution. Drag a label onto the top of the page and make sure it reads "run at server". Then, for the Text attribute, put Text="This website requires Javascript". That should be the best answer. :D

Related

ASP.NET : What exactly is affected when Javascript is off?

I've heard different stories about ASP.NET and JavaScript: that it works fine with Javascript turned off, that only some parts don't work, and that nothing works at all.
How exactly are ASP.NET applications affected if JavaScript is turned off in a client's browser? What parts don't work (if any)?
For example, will RequiredFieldValidators still work? What about UploadControls? AJAX UpdatePanels and AsyncPostBack's? FileUploads? Do page codebehinds still run?
Forgive my ignorance, I can't seem to find much about the issue that is in-depth.
Client-side validation and Ajax won't work, including async postbacks and any control that requires Javascript in order to work.
Server-side validation (which should always happen anyway) and full postbacks and such should always work, and i think a FileUpload control will as well. The biggest difference would be that someone wouldn't see that the data they entered happened to be invalid til the form was submitted.
LinkButtons don't work because they render out a javascript: target.
If you use GridView controls with ButtonColumns then these won't work as the buttons are javascript too. One way around this is to use a TemplateColumn and add <asp:Button> objects inside it.
Also GridView paging and sorting is JavaScript out-the-box so you'd have to write custom paging and sorting.
Also any control with AutoPostback set to true (e.g. a DropdownList) will not auto-postback. You will be able to catch the SelectedIndexChanged but ONLY when the next postback happens.
Any control that "does something" on the client side without a full page request going back to the server(ie. the whole page reloading) is done via JavaScript , and will not work with JavaScript turned off.
Remember HTML is static, so anything that "changes" in the browser window other than CSS hover effects or anything that calls back to the server without a full page reload, is done via JavaScript, and you cannot expect that to work with JavaScript disabled on the client.

What is the use of JavaScript?

Why do I need script on an aspx page?
Javascript will allow you to perfrorm client side coding, so to avoid having to post back to the server.
From Using JavaScript Along with ASP.NET
Working logic and application
processes on the client-side allows
browser based applications to seem
more responsive and to have more
"snappiness" to them.
For client scripting, i.e. validation. There are many scenarios where you need to execute certain logic on the browser's end.
Javascript runs on the client side. So if you want anything to happen or change without refreshing the whole page you use javascript.
There are a lot of things the server can't really do that well. For example if you want to manipulate the page. You could post the whole thing back to the server with some sort of action and get the server to give you a new page. Or you could just use javascript to change it for you and avoid the trip to the server. It is faster for the client and takes the load off of your server.
It helps in doing things on the client side, which essentially means you can :
reduce burden on your server
by doing less postbacks.
do a round of validations on the client
side itself if they are non critical.
Do some fancy stuff like animations etc with out contacting the server
There are a lot more implications/uses of using JavaScript.
For knowing more, remember google is your friend!
Thanks
I'm not sure if you mean why ASP.NET pages requires Javascript, or if you mean additional scripts on the page.
ASP.NET uses Javascript for several types of postbacks. If you for example have a LinkButton, a script is making a postback instead of the default action of the link.
You can use additional scripts on the page for visual effects and to verify data before doing a postback to prevent unneccessary postbacks. (Of course you should also verify the data on the server side to protect against malicious actions.)

Is javascript reliable for preventing actions on the front end such as form submission?

I have a webservice that I need called, the result of which determines whether or not the user is allowed to submit the form.
Since this call is from javascript and not from code behind is it not reliable? Is there any way the user can get around the check -- by either going in with firebug and enabling the submit button, somehow making the method give a different result than was actually returned by the webservice, any other ways of being able to get around it?
Basically is there any way to call a webservice from javascript and have it's result determine whether or not a form can be submitted, and actually prevent the user from submitting the form at all? -- whether or not they have firebug, etc...
No, not possible.
Just to name a few possible reasons:
what if javascript is disabled?
what if the user submits the raw POST (using libcurl, for example)?
what if the browser, that the user is using interprets javascript in a way, different from your expectations (think, portable devices)?
Javascript validation is there for your users' convenience only and should never ever be used as a means of providing security.
You can never prevent the user from making an HTTP request that mimics submission of the form. While disabling the form via Javascript prevents submission for 95% of the users who both have Javascript enabled and don't want to circumvent your access control, anyone who understands HTTP can make the call and you are correct in showing that anyone with Firebug can do it in a matter of seconds.
Javascript isn't reliable for preventing anything. It shouldn't be seen as a security-wall, as it's too easily disassembled with things like firebug, iedevelopertoolbar, and many other browser toys.
Even if you could prevent them from submitting your form on your page, nothing stops them from creating a brand new form, on their own page, and point it toward the action of your form. Thus they're removing themselves from your "secure" environment, and instead chosing to play in their own.
Your suspicion is correct; the user can easily get around any possible Javascript validation.
You will need to use server-side code.
No, it is not reliable. Try disabling Javascript in your browser to see for yourself how easily you can get around it.
The user could simply disable javascript in their browser, or use something like NoScript. The best you could do is to try setting the form action itself in the return from the Ajax request, that way the form, as loaded, won't submit (except to itself). This will probably stop casual users but would be no impediment to a slightly more determined (or just bored and tech savvy) user. You will need to check on the server side whatever you do.
In general, no. You can make the form hard to submit without going through Javascript. Make the submit button not an actual submit button (<input type="submit">), but a pushbutton (<input type="button">) that submits the form in its onClick handler.
As everyone else said, no you can't do it. The only real solution is to have the web service return some dynamic value which the Javascript inserts in a hidden form input. Then whatever server-side code processes the form submission should reject the request if that value is not present.

ASP.NET code inside Google Maps info window

Clarification:
Put simply, I'd like to put an ASP.NET UpdatePanel inside the info window of Google Maps. This would mean that users could interact with my application from within an info window, without refreshing the page and without closing the currently open info window.
Does anyone know if this is possible?
Update:
Thank you to all those who have so far responded. Very much appreciated.
What I have gleaned from the answers is that:
the update panel has it's own "mysterious mechanics" which might be causing the UpdatePanel to not work correctly inside the InfoWindow. Going down the more direct route of using JQuery to make ajax calls to simple web services should eliminate the hidden complexity of the UpdatePanel and enable the functionality I want.
I am still intrigued as to why the UpdatePanel approach does not work, and as to why using one would "break the model" of Google Maps, when surely an UpdatePanel merely renders as HTML and javascript with a link to the XMLHttpRequest object.
Is it possible to place ASP.NET code inside Google Maps info window?
I'd like to place an UpdatePanel with some AJAXified asp:Button's inside the info window.
AFAICT you simply provide the HTML to place in the info window as a string, so was thinking of rendering a UserControl to a string and placing that string in the info window for the browser to render. Does anyone know if this is likely to work?
If this is not possible in Google maps, does anyone have any idea whether such an implementation would be possible with the corresponding Virtual Earth technology?
José Basilio is right. Instead, use $jquery Live Events and put regular HTML in the Info Window then use Ajax calls with jQuery to get the interactivity you want. Reframe the problem.
VirtualEarth has tigether integration with SilverLight, which should mean that you'll have greater .NET control over your web mapping application. With most Google Maps implementations that we've done we typically just use pure HTML/JavaScript/CSS solutions to create AJAX functionality. Thus we would inject HTML/javascript into the InfoWindow class, for any custom functionality that we needed.
Consider another approach and possibly. What you are really looking for is to be able to respond to a server side event. Maybe something like this if you really and truely need to respond to server side events.
http://windyroad.org/2006/07/25/event-driven-ajax-part-1-pushing-server-side-events/
Once you are handling the events on the client in javascript you can do whatever you like to the map
I'm not sure I'd take this approach, but if you want to get ASP.NET code within an info window, you could use an iframe.
Alternatively have you considered an ASP.NET control such as GoogleMap Control, which would handle all the integration for you?
Rich
UpdatePanel != AJAX. Take a look at using jQuery to make Ajax calls ($.ajax) and creating simple web services/etc. to achieve what you're after. While technically you could wiggle an UpdatePanel into there somehow, it just isn't worth it.
Look here for an excellent example on calling AJAX/web methods with jQuery.
Have you looked at the ExtInfoWindow control? Since you did not state your problem, I can't say whether it is a solution, but it seems that it should be mentioned here.

What is the best way the _DoPostBack javascript method in Asp.net

I want to set a breakpoint on the __DoPostBack method, but it's a pain to find the correct file to set the breakpoint in.
The method __DoPostBack is contained in an auto-generated js file called something like:
ScriptResource.axd?d=P_lo2...
After a few post-backs visual studio gets littered with many of these files, and it's a bit of a bear to check which one the current page is referencing. Any thoughts?
If you using IE7 for testing you can use View -> Script Debugger -> Break on next statement and then just click the button that generates the event(__DoPostBack)
TBH, I dont think there is much value in setting a breakpoint within the Javascript since it pretty much comes straight back to the server anyways.
It would be best to set breakpoints in your server code.. Depending on what you are trying to debug this will be in different places.. Either in the page event cycle or a controls IPostBackEventHandler.RaisePostBackEvent handler.

Resources