How to register two different controls (buttons) to the same event in ASP.net
I am currently in the process of learning ASP.net and thus reading a book named “ASP.NET 4 Unleashed” by Pearson Publications. I found an example in the book in which it implements 2 buttons with different Ids to work on a particular event. I will try to implement the same and describe what I could understand.
Let’s create a new website in Visual Studio (I am using the 2010 version and created an empty website).
Now add a new item i.e New Form to the empty website.
I hope you have seen to it that the file created has a code behind file as well.
Personally I prefer to go to the source view and add controls to the form. This gives me raw control in the manipulations I tend to do. It also gives me good understanding of syntax. I would suggest you do the same. With Visual studio you always have another way of doing things. I know, many would just like to use the drag and drop functionality. Well anyway go ahead. Spoil Yourself J .
Now, in the source view of Default.aspx (This will be the default name if you have not changed the name during creation of the form) add the following code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="https://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> This is updated. The test time is : <asp:Label ID="lblServerTime" runat="server"/> </asp:Label> <asp:Button ID="btn1" runat="server" Text="0" OnClick="Button_click"/> <asp:Button ID="btn2" runat="server" Text="1" OnClick="Button_click"/> </div> </form> </body> </html>
I am not going to the basics and explain everything.
I have added two ASP.NET button controls with the Ids btn1 and btn2 with text values 0 and 1 respectively.
As you can see both the buttons in the form are assigned to a single click event of button (OnClick). This event is bound to the routine “Button_Click“.
Next you go into the code behind file and add the following code in it:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { lblServerTime.Text = DateTime.Now.ToString(); } protected void Button_click(object sender, EventArgs e) { Button btn= (Button)sender; btn.Text = (Int32.Parse(btn.Text) + 1).ToString(); } }
Button_Click will be called when any of the two buttons with Ids btn1 and btn2 is clicked. What we intend to do is whenever a user clicks on any of the two buttons the text of the particular button clicked will be incremented by 1.
Such functions are passed mostly with two parameters. “sender “in the parameter represents that control (i.e the button that you clicked) that raised the event.
Button btn= (Button)sender;
In the above line, we are actually creating an instance of button named btn. We assign btn with the sender that is casted into an instance of button. Now whatever be the button that is pressed the instance of the same button will be passed to btn. Thus any change in the property of the btn will reflected in the button that was actually clicked.
btn.Text = (Int32.Parse(btn.Text) + 1).ToString();
In the above line we get the text of the button which would be of type string. This value is then converted into System.Int32 with the help of Int32.Parse method(). The converted value is then incremented by 1.
The incremented value is of type Integer and thus is converted again back to string with the help of ToString() method.
This converted value is then assigned to the text property of btn (The instance of the control that was clicked initially).
We hope we could make things a bit clear to you. Please feel free to comment. It motivates usJ.