Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
566 views
in Technique[技术] by (71.8m points)

asp.net - Difference between button and asp:button onclick

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...