最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Why put javascript in asp.net? - Stack Overflow

programmeradmin3浏览0评论

I have been asked about JavaScript and am unsure on a few points that I mentioned.

After use of ASP I have found that the term used for handling events it the code behind method.

But in other cases I have found that JavaScript is used in asp 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.

I have been asked about JavaScript and am unsure on a few points that I mentioned.

After use of ASP I have found that the term used for handling events it the code behind method.

But in other cases I have found that JavaScript is used in asp 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.

Share Improve this question edited Apr 4, 2011 at 16:18 Ken Pespisa 22.3k3 gold badges57 silver badges63 bronze badges asked Apr 4, 2011 at 16:02 RJFRJF 1411 gold badge3 silver badges12 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 12

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.

发布评论

评论列表(0)

  1. 暂无评论