问题描述
在标准 HTML 元素上使用 runat="server" 而不是真正的 ASP.NET 控件是否合适?我可以完全控制设置普通元素的 html/text,那么为什么我不使用它来代替笨拙"的 ASP.NET WebForms 控件?
如果一个比另一个好,我想知道一些兴趣点:
- 性能差异
- 功能差异
- 其他差异不那么明显?
示例差异:
<asp:Literal ID="mySpecialHtml" runat="server"/><div id="mySpecialHtml" runat="server"/>
两者都是 ASP.NET 服务器控件.HTML元素对应的在System.Web.UI.HtmlControls
命名空间,web控件在System.Web.UI.WebControls
命名空间.>
HTML 控件更轻量级,与 HTML 元素完全对应,而 Web 控件具有更多功能,可以根据浏览器功能和控件设置呈现为不同的 HTML 元素.
HTML 控件呈现为单个 HTML 元素,而 Web 控件呈现为零个或多个 HTML 元素.例如,Literal
控件不会呈现为元素,它仅输出其文本.还有其他控件本身不呈现任何元素,例如 Repeater
和 PlaceHolder
控件.另一方面,例如 CheckBoxList
控件被呈现为几个 HTML 元素,一个 table
作为容器,以及每个复选框的 input
元素
使用不同元素呈现的控件示例是 TextBox
控件,它将呈现为 input
或 textarea
> 元素取决于其 TextMode
属性.
Web 控件具有更多功能,但也使用更多资源.它们具有更多属性并支持诸如主题和数据绑定之类的东西.许多 Web 控件将数据放在 ViewState
中,它作为页面的一部分发送.如果不小心,ViewState
会变得很大,影响页面的加载时间.
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?
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" />
Both are ASP.NET server controls. The ones corresponding to HTML elements are in the System.Web.UI.HtmlControls
namespace, and the web controls are in the System.Web.UI.WebControls
namespace.
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.
A HTML control renders as a single HTML element, while a web control is rendered as zero or more HTML elements. The Literal
control 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 Repeater
and PlaceHolder
controls. On the other hand, the CheckBoxList
control for example is rendered as several HTML element, a table
as container, and input
elements for each checkbox inside it.
An example of a control that is rendered using different elements is the TextBox
control, which will be rendered either as an input
or a textarea
element depending on its TextMode
property.
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 ViewState
can get quite large, and affect the loading time of the page.
这篇关于何时使用 runat="server"在普通 HTML 上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!