Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net: difference between runat="server" and server controls

What is the difference in functionality between

<asp:Button id="button1" Text="Click me" runat="server" OnClick="submitEvent" />

and

<input type="button" id="button1" runat="server" value="Click me" />

Does the input with runat="server" attribute has other or limited properties and methods?

Thank you!

like image 753
Dasha Salo Avatar asked May 18 '09 11:05

Dasha Salo


2 Answers

The first one creates a System.Web.UI.WebControls.Button while the second one creates a System.Web.UI.HtmlControls.HtmlInputButton.

Both are server controls, but the controls in the WebControls namespace generally has a bit more functionality than the controls in the HtmlControls namespace. Typically they put some data in ViewState to keep track of their state, and they have server side postback events.

Each controls in the HtmlControls namespace correspond exactly to an HTML element, while the controls in the WebControls namespace may be rendered differently depending on what the browser that is requesting the page can support.

like image 132
Guffa Avatar answered Oct 25 '22 15:10

Guffa


The button represented by <asp:Button runat="server".../> will be converted to a web server control with a rich state model and different properties and methods which has more clear representation in real world like Button.Text = "Click Me".

The button represented by <input type="button" runat="server"..../> will be converted to html server control represented by HtmlInputButton; with has limited properties, methods and events. Most of the properties resemble the html equivalents like Button.Value="Click Me".

Note that elements in a markup page are pre-processed/compiled before being used and will be converted to a class representation where every element is represented by a control. You can access server side controls which are identified by the runat="server" tag from the code behind since they will have the correct matching server control(web/html), other static content including an <input type="button.../> tag with out the runat="server" will be represented as a LiteralControl.

like image 35
Leyu Avatar answered Oct 25 '22 14:10

Leyu