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!

Dasha Salo
  • 5,159
  • 5
  • 26
  • 28

6 Answers6

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.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
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.

Leyu
  • 2,687
  • 2
  • 23
  • 27
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.

Noldorin
  • 144,213
  • 56
  • 264
  • 302
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....

Aarsh Thakur
  • 587
  • 3
  • 10
  • 20
-1

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

Artem Koshelev
  • 10,548
  • 4
  • 36
  • 68
  • Well, it has some server events like Load and PreRender, but no postback events. – Guffa May 18 '09 at 11:41
  • Actually, clicking a HtmlInputButton will trigger the ServerClick event during the resulting postback. It has no equivalent of the Command event, though. – stevemegson May 18 '09 at 12:27
-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.

User
  • 30,403
  • 22
  • 79
  • 107
  • 2
    The second option works just fine, and it's also a server control. It definitely has methods. – Guffa May 18 '09 at 11:37
  • way off base, the second form is not only valiud but useful because it isn't abstracted by ASP – annakata May 18 '09 at 11:41
  • Wrong. The second option creates an instance of System.Web.UI.HtmlControls.HtmlInputButton that will be rendered as (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). – Conceptdev May 18 '09 at 11:44
  • 1
    Then it's something new for me. – User May 18 '09 at 11:46
  • -1 - It is a bad idea to comment on something you don't know anything about. – Venemo May 30 '11 at 18:41