何时在普通 HTML 上使用 runat="server"

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11460121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 01:37:25  来源:igfitidea点击:

When to use runat="server" on normal HTML

asp.nethtmlwebforms

提问by Benny

Is it ever appropriate to use runat="server" on a standard HTML element instead of a true ASP.NET control? I have full control over setting the html/text of the normal element, so why wouldn't I use it instead of a "clunky" ASP.NET WebForms control?

在标准 HTML 元素上使用 runat="server" 而不是真正的 ASP.NET 控件是否合适?我可以完全控制设置普通元素的 html/text,那么为什么我不使用它来代替“笨拙”的 ASP.NET WebForms 控件呢?

If one is better than the other, some points of interest I would like to know:

如果一个比另一个更好,我想知道一些兴趣点:

  • Performance differences
  • Functionality differences
  • Other differences not so obvious?
  • 性能差异
  • 功能差异
  • 其他的区别不是很明显吗?

An example difference:

示例差异:

<asp:Literal ID="mySpecialHtml" runat="server" />

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

采纳答案by Guffa

Both are ASP.NET server controls. The ones corresponding to HTML elements are in the System.Web.UI.HtmlControlsnamespace, and the web controls are in the System.Web.UI.WebControlsnamespace.

两者都是 ASP.NET 服务器控件。HTML元素对应的在System.Web.UI.HtmlControlsnamespace中,web控件在System.Web.UI.WebControlsnamespace中。

The HTML controls are more light-weight and correspond exactly to an HTML element, while the web controls have more features and can be rendered as different HTML elements depending on the browser capabilities and the settings of the control.

HTML 控件更轻量级,与 HTML 元素完全对应,而 Web 控件具有更多功能,可以根据浏览器功能和控件设置呈现为不同的 HTML 元素。

A HTML control renders as a single HTML element, while a web control is rendered as zero or more HTML elements. The Literalcontrol for example isn't rendered as an element, it only outputs its text. There are other controls that doesn't render any elements by themselves, like the Repeaterand PlaceHoldercontrols. On the other hand, the CheckBoxListcontrol for example is rendered as several HTML element, a tableas container, and inputelements for each checkbox inside it.

HTML 控件呈现为单个 HTML 元素,而 Web 控件呈现为零个或多个 HTML 元素。Literal例如,控件不会呈现为元素,它仅输出其文本。还有其他控件本身不呈现任何元素,例如RepeaterPlaceHolder控件。另一方面,CheckBoxList例如,控件呈现为多个 HTML 元素、一个table作为容器以及input其中每个复选框的元素。

An example of a control that is rendered using different elements is the TextBoxcontrol, which will be rendered either as an inputor a textareaelement depending on its TextModeproperty.

使用不同元素呈现的控件的一个示例是TextBox控件,它将根据其属性呈现为元素inputtextarea元素TextMode

The web controls have more features, but also uses more resources. They have more properties and support things like themes and data binding. Many of the web controls put data in the ViewState, which is sent as part of the page. If you are not careful, the ViewStatecan get quite large, and affect the loading time of the page.

Web 控件具有更多功能,但也使用更多资源。它们具有更多属性并支持诸如主题和数据绑定之类的东西。许多 Web 控件将数据放在 中ViewState,作为页面的一部分发送。如果你不小心,它ViewState会变得很大,并影响页面的加载时间。

回答by jrummell

The only reason I have used server html controls is when I needed the flexibility of writing my own html but still needed to access it's properties in code behind.

我使用服务器 html 控件的唯一原因是我需要编写自己的 html 的灵活性,但仍然需要在后面的代码中访问它的属性。

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

In code behind:

在后面的代码中:

mySpecialHtml.InnerHtml = "something else";

回答by Raman Zhylich

ASP.NET parser interprets standard html elements w/ runat="server" tag as html server controls. Example:

ASP.NET 解析器将带有 runat="server" 标记的标准 html 元素解释为 html 服务器控件。例子:

<a runat="server"></a>

is interpreted as

被解释为

<asp:HtmlAnchor runat="server"></asp:HtmlAnchor>

So, be aware that if you use runat="server" than you are using corresponding HTML Server control.

因此,请注意,如果您使用 runat="server",那么您使用的不是相应的 HTML 服务器控件。

Additionally, ASP.NET contains a set of Web Server controls, i.e. HyperLink control, which provide more functionality, more properties, events etc. but can be slower in some cases.

此外,ASP.NET 包含一组 Web 服务器控件,即 HyperLink 控件,它们提供更多的功能、更多的属性、事件等,但在某些情况下可能会更慢。

回答by jzacharia

I believe ASP.NET uses a different set of Web Server controls which usually has a lot more functionality, but it's really up to your preference. If your work is mostly server-side, I'd stick with ASP.NET controls.

我相信 ASP.NET 使用一组不同的 Web 服务器控件,这些控件通常具有更多功能,但这实际上取决于您的喜好。如果您的工作主要是服务器端,我会坚持使用 ASP.NET 控件。

回答by Waqar Janjua

I prefer to use html controls when there is more work on client side, and use asp.net controls when there is more work on server side ( code behind ).

当客户端有更多工作时,我更喜欢使用 html 控件,当服务器端(代码隐藏)有更多工作时,我更喜欢使用 asp.net 控件。