asp.net - 为什么 ASP.NET Webforms 需要 Runat ="Server"属性?

标签 asp.net runatserver

为什么我必须在所有 ASP.NET 控件上指定 runat="server",因为它是强制属性,并且 server 是我的控件中唯一可用的选项。对 ASP.NET 的了解有限,不使用就会报错?

我确实了解我可以选择在 HTML 标记上使用它,并且我确实了解客户端/服务器范例及其实际指定的内容。

它是一个冗余标签,可能只是由该控件是 ASP.NET 控件所暗示,还是有根本原因?

最佳答案

我一直相信,您可以混合使用 ASP.NET 标记和 HTML 标记,并且 HTML 标记可以选择 runat="server" 。或不。保留该标记不会造成任何损害,但将其删除会导致编译器错误。你暗示的关于网络语言的东西越多,对于一个崭露头角的程序员来说,学习它就越不容易。这是对标签属性进行详细说明的一个很好的理由。

此对话是在 Mike Schinkel 的 Blog 上进行的他本人和微软国家服务部的塔尔博特·克罗威尔 (Talbot Crowell) 之间的对话。相关信息如下(由于来源中的语法错误,第一段被解释):

[...] but the importance of <runat="server"> is more for consistency and extensibility.

If the developer has to mark some tags (viz. <asp: />) for the ASP.NET Engine to ignore, then there's also the potential issue of namespace collisions among tags and future enhancements. By requiring the <runat="server"> attribute, this is negated.

继续:

If <runat=client> was required for all client-side tags, the parser would need to parse all tags and strip out the <runat=client> part.

他继续说道:

Currently, If my guess is correct, the parser simply ignores all text (tags or no tags) unless it is a tag with the runat=server attribute or a “<%” prefix or ssi “<!– #include(...) Also, since ASP.NET is designed to allow separation of the web designers (foo.aspx) from the web developers (foo.aspx.vb), the web designers can use their own web designer tools to place HTML and client-side JavaScript without having to know about ASP.NET specific tags or attributes.

关于asp.net - 为什么 ASP.NET Webforms 需要 Runat ="Server"属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/304290/

相关文章:

c# - &lt;script runat ="server"> 与代码隐藏文件

asp.net - 如何在 asp.Net 的脚本标签上使用 runat ="server"

c# - 遵循 ASP.NET 中的 DRY 原则

asp.net - 无法加载文件或程序集 microsoft.sqlserver.management.sdk.sfc

c# - 无法在 ASP.NET Webforms 中运行异步任务

c# - 如何使用 C# 在用户控制页面中获取父页面名称

.net - ASP.NET MVC 中 runat ="server"标签的状态是什么?

c# - 必须放在带有 runat=server 的表单标签内

html - <表单 ID ="form1"runat ="server">,警告

asp.net - Web.config自定义错误模式冲突