I have been asked about JavaScript and am unsure on a few points that I mentioned.
After use of ASP.net I have found that the term used for handling events it the code behind method.
开发者_如何学JAVABut in other cases I have found that JavaScript is used in asp.net pages.
My question is, is this done as the javascript file is an external .js file and could be accessed from any where or is there a different reason for it?
Thanks for any reply's.
Javascript runs on the client machine. The event handling is done on that clients machine without talking to the server.
ASP.NET code behind event handling is done on the server. When an event happens the client talks to the server, the server handles the event and talks back to the client.
The latter requires a round trip over the network and most likely a page postback (unless it's an async webmethod).
Doing it with JavaScript means it's done locally without the page refreshing, it's done faster and there less stress on the server.
Of course if your event handling is manipulating the database then it should be handled by the server. If it's manipulating the page it should be handled by the client.
If you view the page source in your browser for your ASP.NET app, you will see all the generated .axd (javascript resource files) that ASP.NET creates on the fly.
As Raynos said, Javascript is run on the client machine and ASP.NET runs on the server.
ASP.NET requires the use of the client-side Javascript, as that's how ASP.NET handles its Events via PostBacks. Like I said though, this is the auto-generated Javscript that is done for you on-the-fly in temporary external .axd files.
Now, on top of the auto-generated Javascript, you can make your own Javscript methods that lessen the need for Http Requests / Round Trips / PostBacks. You could create and include a foo.js file and put whatever functionality you want to handle there. Or, you could just put your Javascript inline with your HTML by putting it inside of <script type="javascript"></script>
tags. Also, you can move server-side functionality to the client by the use of page methods, which basically creates a Javascript function out of your server-side method and allows you to use it client-side.
Personally, I like to use a Javascript framework called, jQuery for my client-side needs. I suggest googling some of the above and see what fits best for you.
精彩评论