BeanSoftware Logo

ASP.NET Database Search Control

    Write For Us

How To Get Two Button's Click Execute The Same Function?

Sometimes you need to execute same code for two or more button controls. That could be for example if you have long data form and you want to enable Update and Cancel buttons on top and on bottom of web form. You can solve this problem if you call the same function on buttons' events handlers, but it is more professional to use the same event handler for both button controls. You can do it with code like this:

[ C# ]

Hmm, there is no C# code for this. It looks like C# only place this information in markup code. For example, if you want the same function btnFirst_Click to be executed for two buttons, btnFirst and btnSecond, .aspx code will be:

<asp:Button ID="btnFirst" runat="server" onclick="btnFirst_Click" Text="First button" />
<asp:Button ID="btnSecond" runat="server" onclick="btnFirst_Click" Text="Second button" />


On the other side, VB.NET stores this information in source code, like this:

[ VB.NET ]

Protected Sub btnFirst_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnFirst.Click, btnSecond.Click

Related articles:

1. Client Side in ASP.NET: Common Javascript Functions

FAQ toolbar: Submit FAQ  |  Tell A Friend  |  Add to favorites  |  Feedback

Copyright © 2002-2008 Bean Software. All rights reserved.