I am new to developing in asp.net for making web sites. So I having some trouble understanding the difference between the following. 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>
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.
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"
to my understanding you have to keep in mind that there is a cliend side code that will execute, for example javascript in your browser, and asp/c# code that will execute on the server.
So having this control:
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
Button1_Click will execute on the server, in microsoft world it is IIS, on the other hand you have just a pure html control, where you hook up a javascript function for the click event
<input id="Submit1" type="submit" value="submit" onclick="return Submit1_onclick()" />
So the server side has nothing to do with this part, this code will execute on your machine, in your browser.
A mí me funcionó así:
1.- En el botón:
<input type="submit" id="btnAccept" value="Log In" runat="server" onserverclick="btnAccept_Click">
2.- En el codebehind:
Protected Sub btnAccept_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
来源:https://stackoverflow.com/questions/14306677/difference-betwen-button-and-aspbutton-onclick