且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

HTML控件和ASP.NET Web控件

更新时间:2023-12-06 09:58:28

首先,如果像示例中那样将Html控件从工具箱"拖到设计图面上,则创建的标记不包含runat ="server".这意味着它是本地HTML标记,而不是.NET控件.没有runat ="server"的本地Html标记没有服务器端功能.因此,您无法在后面的代码中设置"Text1"输入标签的值.

First, if you drag an Html control from the Toolbox onto your design surface as in your example, the tag created does not include runat="server". That means it is native Html tag and not a .NET control. A native Html tag without the runat="server" has no server-side functionality. Thus, you could not set the value of the your "Text1" input tag in the code-behind.

第二,将runat ="server"添加到Html输入标记后,将其从本地Html标记转换为从System.Web.UI.Control派生的HtmlControl.现在,该问题可能会演变为从System.Web.UI.Control和System.Web.UI.WebControl派生的内容之间的差异.但是,为了专门解决您的问题,让我们将标准输入type ="text"控件与TextBox控件进行比较:

Second, once you add the runat="server" to your Html input tag, you convert it from a native Html tag into a HtmlControl which derives from System.Web.UI.Control. Now the question could morph into the differences between something that derives from System.Web.UI.Control and System.Web.UI.WebControl. However, to specifically address your question, let's compare a standard input type="text" control to the TextBox control:

  1. 可以从代码隐藏区访问TextBox控件,而输入控件不能(不容易)访问代码隐藏区,这也意味着您可以为TextBox控件连接服务器端事件,而不能使用标准的HTML控件.
  2. TextBox控件使用ViewState自动保存其值.
  3. 可以使用Theme和.skin文件对TextBox控件进行外观设置,而本机Html控件则不能.
  4. 根据其TextMode属性,TextBox可以将其呈现为输入type ="text"控件或文本区域.
  5. TextBox控件可以使用验证器参与验证.
  6. 最后但并非最不重要的是,如果需要,TextBox控件可以使用控件适配器在不同的浏览器中以不同的方式呈现.请参阅 http://msdn.microsoft.com/en-us/magazine/cc163543.aspx .

现在,所有这些都说明了,如果您不需要任何WebControl功能,那么使用本机的Html控件将更加精简.在您的示例中,您只需将两个空控件拖到设计图面上.如果这是您所需要的,那么使用.NET控件将是多余的.但是,随着您开始添加AutoComplete和服务器端事件等,浏览器获得的全部内容,Javascript以及所有内容都变得更大了.

Now, all that said, if you do not need any of WebControl capabilities, then using an native Html control is substantially leaner. In your example, you simply dragged two empty controls onto your design surface. If that is all you needed then using the .NET control would be overkill. However, as you start adding AutoComplete and server-side events and such, the full content, Javascript and all, of what gets to the Browser is much larger.