Difference betwen button and asp:button onclick

后端 未结 5 2132
鱼传尺愫
鱼传尺愫 2020-12-14 08:49

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:Bu

相关标签:
5条回答
  • 2020-12-14 09:01

    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)
    
    0 讨论(0)
  • 2020-12-14 09:16

    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"

    0 讨论(0)
  • 2020-12-14 09:17

    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.

    0 讨论(0)
  • 2020-12-14 09:17

    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.

    0 讨论(0)
  • 2020-12-14 09:21

    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.

    0 讨论(0)
提交回复
热议问题