为什么 ASP.NET 网络表单需要 Runat=“Server"?属性?

Why does ASP.NET webforms need the Runat=quot;Serverquot; attribute?(为什么 ASP.NET 网络表单需要 Runat=“Server?属性?)
本文介绍了为什么 ASP.NET 网络表单需要 Runat=“Server"?属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Why do I have to specify runat="server" on all my ASP.NET controls when it is a mandatory attribute and server is the only option available in my limited knowledge of ASP.NET, and I get an error if I don't use it?

我明白我可以选择在我的 HTML 标记上使用它,而且我明白客户端/服务器范式及其实际指定的内容.

I do understand that I can optionally use it on my HTML tags, and I do understand the client/server paradigm and what it is actually specifying.

它是一个冗余标签,可能只是因为控件是 ASP.NET 控件而暗示的,还是有潜在的原因?

Is it a redundant tag that could just be implied by the control being an ASP.NET control, or is there an underlying reason?

推荐答案

我一直认为,你可以混合使用 ASP.NET 标签和 HTML 标签,而 HTML 标签有两种选择runat="server" 与否.将标签留在其中并不会造成任何伤害,并且会导致编译器错误将其取出.你对 Web 语言的暗示越多,一个初露头角的程序员就越不容易进入并学习它.这也是详细说明标签属性的一个很好的理由.

I've always believed it was there more for the understanding that you can mix ASP.NET tags and HTML Tags, and HTML Tags have the option of either being runat="server" or not. It doesn't hurt anything to leave the tag in, and it causes a compiler error to take it out. The more things you imply about web language, the less easy it is for a budding programmer to come in and learn it. That's as good a reason as any to be verbose about tag attributes.

此对话是在 Mike Schinkel 的 博客 上,他与 Microsoft National Services 的 Talbot Crowell 进行的.相关信息如下(第一段因原文语法错误而改写):

This conversation was had on Mike Schinkel's Blog between himself and Talbot Crowell of Microsoft National Services. The relevant information is below (first paragraph paraphrased due to grammatical errors in source):

[...]<runat="server"> 的重要性更多是为了一致性和可扩展性.

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

如果开发人员必须标记一些标签(即 <asp:/>)以供 ASP.NET 引擎忽略,那么还有标签和标签之间命名空间冲突的潜在问题未来的增强.通过要求 <runat="server"> 属性,这被否定了.

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.

继续:

如果所有客户端标签都需要 <runat=client>,则解析器需要解析所有标签并去除 <runat=client> 部分.

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.

他继续说:

目前,如果我的猜测是正确的,解析器只是忽略所有文本(标签或没有标签),除非它是带有runat=server 属性或<%"前缀或 ssi <!– #include... (...)此外,由于 ASP.NET 旨在允许分离网页设计师(foo.aspx) 来自 Web 开发人员(foo.aspx.vb),网页设计师可以使用他们自己的网页设计工具放置 HTML 和客户端 JavaScript无需了解 ASP.NET特定的标签或属性.

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 网络表单需要 Runat=“Server"?属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
how do i pass parameters to aspnet reportviewer(如何将参数传递给aspnet report查看器)
Bind multiple parameters from route and body to a model in ASP.NET Core(在ASP.NET Core中将路由和主体中的多个参数绑定到一个模型)
Custom model binding in AspNet Core WebApi?(AspNet Core WebApi中的自定义模型绑定?)
How to minify in .net core mvc view?(如何在.Net核心MVC视图中缩小?)