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

6

11

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!

Bethina answered 18/5, 2009 at 11:19 Comment(0)
S
21

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.

Seward answered 18/5, 2009 at 11:35 Comment(0)
F
1

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.

Forethoughtful answered 18/5, 2009 at 11:50 Comment(0)
N
0

The former line is ASP.NET, the latter simple XHTML.

The former gets parsed and interpreted on the server side, after which the HTML code is generated, which pretty much corresponds to your second example. The ASP.NET Button is really little more than light wrapper over th HTML input button functionality, and should be used wherever you need to handle the Click event on the server side (or in the general case any events), and is usually the way to go, since you're letting ASP.NET abstract the idea of a button on your page for you.

Nomenclator answered 18/5, 2009 at 11:25 Comment(0)
F
0

functionality of both the controls is same with the difference that first one is .net control and second one is html control that can be made servercontrol by using

runat="server".

and first one is rich in evants and metods thn the second one....

Fagen answered 18/5, 2009 at 12:55 Comment(0)
U
-1

There is no server events associated with such a controls, but you can use it in codebehind to change it's properties.

Usable answered 18/5, 2009 at 11:24 Comment(2)
Well, it has some server events like Load and PreRender, but no postback events.Seward
Actually, clicking a HtmlInputButton will trigger the ServerClick event during the resulting postback. It has no equivalent of the Command event, though.Pavyer
B
-3

Your second option won't probably even work. runat="server" will be rendered directly to the HTML output where it will have no functionality and will only break HTML validation.

input is an HTML element which has only HTML properties, and definitely no methods of any kind.

Bowdlerize answered 18/5, 2009 at 11:23 Comment(5)
The second option works just fine, and it's also a server control. It definitely has methods.Seward
way off base, the second form is not only valiud but useful because it isn't abstracted by ASPMagus
Wrong. The second option creates an instance of System.Web.UI.HtmlControls.HtmlInputButton that will be rendered as <input type="button" id="button1" value="Click me" /> (unless inside an INamingContainer which will munge the id). In the codebehind you can modify (among other things) its Style collection, Visibility; use FindControl() and attach handlers to the ServerClick event. IMO the main advantage of HtmlControls over WebControls is control over the markup (for styling & compliance).Buyers
Then it's something new for me.Bowdlerize
-1 - It is a bad idea to comment on something you don't know anything about.Gorton

© 2022 - 2024 — McMap. All rights reserved.