0

In my master page I have two buttons:

<asp:Button ID="btnPrev" runat="server" CssClass="btn btn-prev" Text="Prev" />
<asp:Button ID="btnNext" runat="server" CssClass="btn btn-next" Text="Next" />

The user clicks on the and of course the page fires, great, except I want to do an action based on which button is pressed. I can go back and reference the Master Page as the first thing on the Content Page

Call btnNext_clickassessment(sender, e)

and

Protected Sub btnNext_clickassessment(sender As Object, e As System.EventArgs)
       Me.Master.btnNext_Click(sender, e)
End Sub

However this does not determine which button was actually pressed.

I have looked at onclick and If CType(sender, Button).ID = "btnNext_Click" Then but nothave not found a solution yet (or one of those is an I've set it wrong!

Maybe even some javascript?

indofraiser
  • 1,014
  • 3
  • 18
  • 50
  • 1
    possible duplicate of [How to determine which button caused postback](http://stackoverflow.com/questions/10229995/how-to-determine-which-button-caused-postback) – Mauro Aug 27 '15 at 15:14
  • That's not got Master Pages..? – indofraiser Aug 27 '15 at 15:16
  • should work in the same way - you can check the sender or use a command and command argument, look at the comments in the accepted answer. – Mauro Aug 27 '15 at 15:17
  • 1
    There does not appear to be an accepted answer. The one with three upvotes (not trying to be difficult want to look at the right one :-) ) – indofraiser Aug 27 '15 at 15:21
  • 2
    http://stackoverflow.com/a/10230165/2208 - this one – Mauro Aug 27 '15 at 15:22
  • This fires the content page first still, I'll try the below answer and confirm :-) – indofraiser Aug 27 '15 at 15:52

1 Answers1

1

You can also declare a public property on your master page and then handle the events on the content page as below

MasterPage

Public btnPrev As Button
Public btnNext As Button

ContentPage

'in page load
AddHandler Me.Master.btnPrev.Click, AddressOf Me.btnPrevNext_Click
AddHandler Me.Master.btnNext.Click, AddressOf Me.btnPrevNext_Click

and then your handler

Private Sub btnPrevNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    ' Handle your Button clicks here
    dim btnName as string
    btnName = sender.ID
    ' do stuff with the button name
End Sub

Alternatively use two distinct handlers to simplify your code

ContentPage

'in page load
AddHandler Me.Master.btnPrev.Click, AddressOf Me.btnPrev_Click
AddHandler Me.Master.btnNext.Click, AddressOf Me.btnNext_Click

and then your handler

Private Sub btnPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    ' Handle your Previous Button click here
End Sub

Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    ' Handle your Next Button click here
End Sub
Mauro
  • 4,531
  • 3
  • 30
  • 56