开发者

Html control and asp.net web control

开发者 https://www.devze.com 2022-12-20 22:00 出处:网络
i would like to know what exactly the difference between Html control and asp.net web control. why do we need 开发者_C百科these two types of controls?

i would like to know what exactly the difference between Html control

and asp.net web control. why do we need 开发者_C百科these two types of controls?

i have placed one html input text ,html button and asp.net text box AND ASP.NET BUTTON on my web page

    <input id="Text1"    type="text" />

    <input id="Button2" type="button" value="button" />







    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

    <asp:Button ID="Button1" runat="server" Text="Button" />

when i take view source, both are rendered similarly

    <input id="Text1"    type="text" />

    <input id="Button2" type="button" value="button" />




    <input name="TextBox1" type="text" id="TextBox1" />

    <input type="submit" name="Button1" value="Button" id="Button1" />

what is the advantage of web control over html control.

I got some links in the internet,but not clear what exactly

they are used for.

http://www.extremeexperts.com/Net/FAQ/DiffBetweenServerandHTMLControls.aspx.

Could any one please explain the difference between these two controls.


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.

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 control can be access from the code-behind where an input control cannot (not easily) which also means that you can wireup server-side events for a TextBox control whereas you cannot with a standard Html control.
  2. A TextBox control automatically saves its value using ViewState.
  3. A TextBox control can be skinned using a Theme and .skin file whereas a native Html control cannot.
  4. A TextBox can render as either an input type="text" control or a textarea depending on its TextMode property.
  5. A TextBox control can participate in validation using validators.
  6. Last but not least, the TextBox control can use control adapters to render differently in different browsers if required. See http://msdn.microsoft.com/en-us/magazine/cc163543.aspx.

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.


In short HTML controls don't persist their state while Postbacks. On the other hand ASP.Net control provides you to luxury to have their state saved while several Postbacks automatically. Different while using ASP.Net control instead of HTML element is:

<input type="hidden" name="__VIEWSTATE" value="dDwtNTI0ODU5MDE1Ozs+.................." />

This hidden field is auto generated by ASP.Net and it contains all you controls state in value attribute.


The server controls have a runat="server" attribute which enables you to provide server-side logic for these controls in the code-behind. You can also add this attribute to existing HTML controls to gain this same functionality.


The HTML controls are simple controls that correspond directly to HTML elements.

The ASP.NET Web Controls abstract the HTML elements, and generally provide more control over styling (though some would call this a bad thing).

0

精彩评论

暂无评论...
验证码 换一张
取 消