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

When to use runat=quot;serverquot; on normal HTML(何时使用 runat=server在普通 HTML 上)
本文介绍了何时使用 runat="server"在普通 HTML 上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在标准 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 控件不会呈现为元素,它仅输出其文本.还有其他控件本身不呈现任何元素,例如 RepeaterPlaceHolder 控件.另一方面,例如 CheckBoxList 控件被呈现为几个 HTML 元素,一个 table 作为容器,以及每个复选框的 input 元素

使用不同元素呈现的控件示例是 TextBox 控件,它将呈现为 inputtextarea> 元素取决于其 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 上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

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

相关文档推荐

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视图中缩小?)