Monday, February 6, 2012

ASP.Net Control two buttons with a single Event

Hack the Gibson

Let’s take a look at what we are trying to accomplish exactly.  We have a web form that we have created and have two buttons on that form.  However, both buttons perform similar functions with the same data.  For instance,
We have a TextBox name txtNumber.  Then we have a Button that takes the value of txtNumber and multiplies it by two.  We have another button that will take the txtNumber value and multiply it by three.  Obviously, the only difference between these two events is what we want to multiply it by.  Why write two different Events to perform this functionality?  We can write a single even because we can name the event whatever we want.  It’s the HANDLES keyword that gives us the power of an Event as you will see…

The first thing you will want to do is check and see what type of button you have (a regular button as opposed to a link button).  So in our example, I will use both, a button and a link button.  This is accomplished by checking the condition of the TYPEOF keyword against the SENDER parameter (If you click a button, the SENDER parameter for the event is naturally – the button that you clicked).

So fire up VS2010.  Add a website and then add a form (default.aspx).  On the form, add a textbox (so the user can give us a variable to multiply) call it ID=”txtMultiply”.  Add a button called ID=”btnMutiplyBy2” and a linkbutton called ID=”lbtnMultiplyBy3”
Now we have the front end setup.  Let’s move to the code behind -

Mist likely what you will want to do is add a click event for one of the buttons and then just tweak it so that it will handle (hence the HANDLES keyword) both buttons.

Protected Sub btnMultiplyBy2Or3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnMultiplyBy2.Click, lbtnMultiplyBy3.Click

    Dim result As Integer = 0

    If Not (txtMultiply.Text = "") Then

        If TypeOf sender Is Button Then

            Dim b As Button = CType(sender, Button)

            id = b.ID.ToString()


            Dim b As LinkButton = CType(sender, LinkButton)

            id = b.ID.ToString()

        End If

        If (id = "btnMultiplyBy2") Then

            result = Cint(txtMultiply.Text) * 2

        ElseIf (id = "lbtnMultiplyBy3") Then

            result = Cint(txtMultiply.Text) * 3


            Response.Write("Unable to determine button click.")

            Exit Sub

        End If


        Response.Write("Please enter a value to multiply.")


        Exit Sub

    End If


End Sub

Looking at the code line by line -

First line – we can call our sub whatever we want to.  We could rename it to JoeSmithLivesOnElmStreetAndFlipsHamburgersOnSunday and it would still function correctly.  Why? Well because like I mentioned about, the Subs power is in the HANDLES keyword are the end of the line.  Notice we created a comma delimited list there.  Where if this was a normal event it would just have one .click value.  Here we have added a comma and added our linkbutton.  Now this sub also handles the click event for the linkbutton.

We dimension a result integer varaible – Self explanatory.

Then we want to make sure our user input field is populated, otherwise there is no reason to proceed. 

From there we need to type cast the sender so we can figure out which button was actually clicked (and thus what to multiply by).  So we check to see what type of button was clicked and then grab its ID and save it to a variable (called ID).

Then we Check the ID variable against known names so that we can tell it what to multiply by (Cint is a .Net framework method that allows us to cast an object as an integer).  Then we write our result.

So as you can see, We can add multiple buttons to a single event and capture who the sender was so that we can separate the logic path dependent on which button was clicked.

UPDATE: Obviously, late night and blogging do not make good bedmates.  We need to dimension the id variable as a string.  Right under the first line where we dimension result, add the following line-

Dim id as string = “”

Code will work now.  My bad, yo.

Happy .Netting…Saleh

Hack the Gibson

No comments:

Post a Comment