What is the difference between a script tag with and without a runat=server attribute

纵饮孤独 提交于 2019-12-10 18:56:29

问题


What is the difference between <script runat="server"> and <script> ?


回答1:


When you add runat="server" attribute the tag will become available in server side code like any other asp.net control.

Then you will be able to manipulate/add c#/javascript code within the blocks directly.

If you don't add the runat attribute you will be able to only have client side scripting.




回答2:


The runat="server" tag tells the .NET compiler to execute the tag on the server. it can be added to any html tags which make them available on server side code.

eg if you declare a div like so:

<div runat="server" id="mydiv"></div>

from the code behind you can do this:

mydiv.Visible = false;

<script runat="server"> is used to include server-side code (C# or VB.NET) on the aspx or ascx file without having to add a code-behind (.cs) file.

This article has some info: http://msdn.microsoft.com/en-us/library/f0111sbh(v=vs.100).ASPX


<script> is used in order to include client-side code (usually javascript)

http://www.w3schools.com/tags/tag_script.asp



来源:https://stackoverflow.com/questions/21327704/what-is-the-difference-between-a-script-tag-with-and-without-a-runat-server-attr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!