JavaScript-使用ASP.Net时使用Javascript的频率如何? [英] JavaScript-How often to use Javascript when using a ASP.Net ?

查看:89
本文介绍了JavaScript-使用ASP.Net时使用Javascript的频率如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在学习ASP.Net,我正在制作一个示例电子商务网站。

我在后面的代码中编写了大部分代码(用于例如:隐藏和显示控件,设置cookie等)也可以通过Javascript完成,但我是javascript的新手,我只知道它是一个客户端脚本语言,可以执行各种事情,如弹出窗口,设置cookie,浏览器检测等



所以,我想问一下应该使用多少Javascript代码和多少代码?是否广泛使用Javascript并仅在需要时使用代码;当我在某处读到Javascript是一种客户端技术,所以它可以节省往返服务器,从而提高应用程序性能吗?

Hi,
I am learning ASP.Net and I am making a sample E-commerce site.
I write most of the code in the code behind(for example: hiding and showing of controls,setting cookies etc ) which can be done from Javascript too, but I am a newbie to javascript and I only know that it is a client side scripting language which can perform various things like Popups, setting cookies, browser detection etc.

So ,I want to ask how much Javascript code and how much code behind should be used? Does one use Javascript extensively and use code behind only when it''s needed; as I read somewhere that Javascript is a client side technology, so its save round trips to the server, thereby increasing application performance?

推荐答案

我必须同意 Sergey Alexandrovich Kryukov 。这完全取决于你打算做什么,即你的项目和要求。任务。然而,为了帮助您了解图片,这里是我的观点:

1. Javascript主要用作优秀的眼睛糖果增强剂。你有华而不实的控件,弹出窗口和什么不是让网站更具互动性,并且大多给它一种很好的连续流。

2.背后的代码主要用于控制页面的核心逻辑:是否应该为此用户提供此页面?如果用户显示按钮以擦除所有内存并强制水变为红色......您将获得漂移:影响系统/应用程序的行为/事物。

3.您还可以在客户端和服务器端执行冗余工作,例如验证;或者可能是并发工作,比如使用AJAX调用来获取数据而不是将数据与页面一起发送(用于分页等)。

在客户端验证使用户(当然还有服务器)更容易,因为非法字符,最小/最大长度等不需要服务器交互;并且连续重复页面生成用于录制额外的**&!^%,并且在服务器端验证也是必要的,因为,让我们面对它,Javascript受客户端的支配。



好​​吧,我希望能够描绘出足够好的画面!
I have to agree with Sergey Alexandrovich Kryukov. It depends completely on what you are intending to do, i.e the requirements of your project & task. However, just to help you get the picture, here''s my point of view:
1. Javascript is mostly used as an excellent eye candy enhancer. You have the flashy controls, pop-ups and what not that make the web site more interactive and mostly give it a kind-of nice continuous flow.
2. Code behind is mostly used to control the page''s core logic: Should this page be delivered for this user? Should the user be shown the button to erase all memory and force water to turn red...you get the drift: actions/things that affect the system/application as such.
3. And you also do redundant work, like validating, on both client and server end; or maybe concurrent work, like having an AJAX call to fetch data rather than having the data sent with the page (for pagination etc).
Validating on the client side makes it easier for the user(and the server of course), since illegal characters, min/max length etc won''t require server interaction; and consecutively repeated page generations for taping that extra **#&!^%, and validating on the server side is ALSO necessary since, lets face it, Javascript is at the mercy of the client.

Well, I hope that paints a good enough picture!


当看来有两种不同的语言时,我可以看出为什么会有人混淆在服务器上,客户端上的一个基本上做同样的事情(修改网页)。简短版本是您应该使用最适合特定情况的那个。实际上有一些非常重要的考虑因素,这里有一些(为简单起见,我假设您在服务器端使用C#):

I can see why one might be confused, when it appears that there are two different languages (one on the server, one on the client) that basically do the same thing (modify a webpage). The short version is that you should use whichever is most appropriate for a particular situation. There are actually some very important considerations, and here are a few of them (for simplicity, I will assume you are using C# on the server side):


  1. 有效期。当你必须绝对确定某些东西是有效的时,它应该用C#来完成。这是因为客户可以随意修改JavaScript。
  2. 混淆。当您不希望检查代码时(例如,由于某些专有算法),C#是更好的选择。
  3. 简单。如果有一些C#代码可以简单地完成你的目标(例如,数据绑定),那就使用它(所有其他条件相同)。如果JavaScript更简单,请使用它。
  4. 可理解性。如果您与一群了解C#的人合作,但不了解JavaScript,请坚持使用C#。
  5. 可移植性。如果您使用的是ASP.NET,但可能会切换到Ruby on Rails,也许可以考虑使用JavaScript,因为在将代码库移动到新的服务器端语言时不必重写。当然,这是一种不常见的情况。
  6. 发展速度。如果您的IDE(例如,Visual Studio)更擅长C#,并且您更了解C#,那么您可以在C#中更快地工作。
  7. 执行速度。如果您想避免回发,可能适合使用JavaScript。
  8. 网络流量。您可以使用C#来最小化发送到浏览器的HTML数量。类似地,您可以将JavaScript放在一个只在客户端上加载一次的文件中,并且可以在回发之后和跨页面使用它,而无需再次从服务器加载。如果您的数据库与Web服务器位于同一网络上,C#将最大限度地减少流量。如果您有点对点应用程序(例如,Facebook中的信使),JavaScript可能会减少网络服务器的网络流量。
  9. 组合。有时,您可以使用它们来改善用户体验。例如,您可以使用JavaScript验证输入字段(例如,确保有效的电子邮件地址),然后再次在服务器上验证(以确保有效性)。
  10. 自定义性。有时使用C#进行自定义操作很困难,您可以使用JavaScript代替。
  11. 标准。以标准方式使用内置控件可以简化未来的更改。例如,绑定在表单加载事件(在C#中)的转发器是显示数据列表的简单方法。它也可以用JavaScript完成,但这不是在ASP.NET中执行它的标准方法。
  12. 兼容性。如果用户没有JavaScript,可能您更愿意用C#编写所有代码。
  13. AJAX 。如果您想要AJAX的好处(例如,部分页面更新),JavaScript是必要的,但通常也需要一些C#。
  14. 未来校对。 C#更适合在编译时发现错误,因此将来的任何重构都很容易验证是否正确。
  15. 可维护性。大型JavaScript代码库很难维护,因此C#可能更适合大部分代码。

  1. Validity. When you must be absolutely sure something is valid, it should be done with C#. This is because the client can modify the JavaScript as they please.
  2. Obfuscation. When you don''t want the code to be inspected (e.g., because of some proprietary algorithm), C# is the better option.
  3. Simplicity. If there is some C# code that will accomplish your goal simply (e.g., data binding), use that (all other things being equal). If the JavaScript is simpler, use it.
  4. Understandability. If you work with a bunch of people who know C#, but don''t know JavaScript, stick to the C#.
  5. Portability. If you are on ASP.NET, but may switch to Ruby on Rails, maybe consider using JavaScript, as that won''t have to be rewritten when moving the code base to a new server side language. Granted, this is an uncommon scenario.
  6. Speed of Development. If your IDE (e.g., Visual Studio) is better at C#, and you know C# better, you may work faster in C#.
  7. Speed of Execution. If you want to avoid postbacks, JavaScript may be appropriate.
  8. Network Traffic. You can use C# to minimize the amount of HTML that gets sent over to the browser. Similarly, you can put JavaScript in a file that will only load on the client once, and it can be used after postbacks and across pages without loading from the server again. If your database is on the same network as your web server, C# will minimize traffic. If you have a peer to peer application (e.g., the messenger in Facebook), JavaScript may reduce network traffic to your web servers.
  9. Combination. Sometimes, you can use both to improve the user experience. For example, you can validate input fields with JavaScript (e.g., to ensure a valid email address) and then again on the server (to ensure validity).
  10. Customizability. Sometimes it''s hard to do custom things with C#, and you can use JavaScript to do that instead.
  11. Standards. Using built-in controls in standard ways can ease future changes. For example, a repeater that is bound on the form load event (in C#) is an easy way to show a list of data. It could also be done with JavaScript, but that isn''t the standard way of doing it in ASP.NET.
  12. Compatibility. If a user doesn''t have JavaScript, maybe you would prefer to write all the code in C#.
  13. AJAX. If you want the benefits of AJAX (e.g., partial page updates), JavaScript is necessary, but some C# is usually necessary too.
  14. Future Proofing. C# is better for finding errors at compile time, so any refactoring in the future is easy to verify for correctness.
  15. Maintainability. Large JavaScript codebases can be hard to maintain, so C# may be more appropriate for the bulk of the code.



我确定列表可以扩展十次。单独考虑每种情况,并将代码库作为一个整体考虑。


I''m sure that list could be expanded ten times. Consider each situation individually, and the codebase as a whole.


这篇关于JavaScript-使用ASP.Net时使用Javascript的频率如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆