I am new to developing in asp.net for making web sites.
What is the difference between an asp:Button
and an input button?
Code 1
aspx code
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
Codebehind
protected void Button1_Click(object sender, EventArgs e) { //Do somthing }
Code 2
aspx code
<input id="Submit1" type="submit" value="submit" onclick="return Submit1_onclick()" /> <script language="javascript" type="text/javascript"> // <![CDATA[ function Submit1_onclick() { //Do somthing } // ]]> </script>
asp:Button is an asp.net server control which fire an event on the server side. <input id="Submit1" type="submit" is a client side button of type submit, but it can act as a server side button as well by adding. runat="server" and onserverclick="eventname"
OnClick will work on server side , OnClientClick will execute on client side before control passed to server. If the client side code returns TRUE then it will go to server. Generally programmers use onClientClick to validate the controls like textbox,etc.
The Click event is raised when the Button control is clicked. This event is commonly used when no command name is associated with the Button control (for instance, with a Submit button). Raising an event invokes the event handler through a delegate.
The first code it is a server side code. When you add a tag asp:button
in your webform, the asp.net will render an input type="button"
for you and when you click this button, it will submit a post to the same page (this is called postback
) and will processing the life cycle and asp.net event associated with this button. The same is valid for every server control such as TextBoxes
(render input type='text'
), Panels
(renders divs), Checkboxes
(render input type='checkbox'
), Labels
(render spans
) etc...
In the second code, you have the code that first one will render (an input type = 'button'
), but associated with an event in the client-side javascript.
You can also associate a client-side event in a tag asp:button
, take a look at the OnClientClick
property.
The first is a server side control and the event handler is executed on the server in C#. Clicking the button will cause a postback and all information in the form will be posted to the server for processing, including a call to the click event handler.
The second is fully client side and the event handler is executed in the browser in JavaScript.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With