locked
How to determine which button was pressed RRS feed

  • Question

  • User1466833786 posted

    VS2008. I have the following buttons and code:

    <td>

    <asp:LinkButton ID="lbtnA" runat="server" Text="A" />

    </td>

    <td>

    <asp:LinkButton ID="lbtnB" runat="server" Text="B" />

    </td>

    <td>

    Protected Sub lbtnA_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbtnA.Click, lbtnB.Click

    End Sub

    Is there a way to determine which link button was clicked?

    Thanks,

    Friday, May 23, 2008 9:52 AM

Answers

  • User-1066334067 posted
    Yep, something like this: 
        Protected Sub lbtnA_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbtnA.Click, lbtnB.Click
    
            Dim btn As LinkButton = CType(sender, LinkButton)
    
            If btn.ID = "lbtnA" Then
                ' Do stuff
            End If
    
            If btn.ID = "lbtnB" Then
                ' Do other stuff
            End If
    
        End Sub
    
     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 23, 2008 10:01 AM
  • User139975600 posted

    Yes, there sure is... the sender As Object passes you a reference to the object that sent it here.

    Dim buttonName as String = CType(sender, LinkButton).ID

    this will tell you the name of the button.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 23, 2008 10:02 AM

All replies

  • User-818936891 posted

    hi,

     sender will indicate the button clicked.

    Friday, May 23, 2008 10:00 AM
  • User509596457 posted

    You could have two seperate subs...

    Protected Sub lbtnA_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbtnA.Click
         'lbtnA clicked
         'execute some code
    End Sub

    Protected Sub lbtnB_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbtnB.Click
         'lbtnB clicked
         'execute some code
    End Sub

    Friday, May 23, 2008 10:00 AM
  • User1828997928 posted

    cast the sender object to button and check the name.  I don't use vb.net but the following should do it

    Dim test as Button

    test = CType(sender, Button)

    if test.Name...do whatever
     

    Friday, May 23, 2008 10:01 AM
  • User-1066334067 posted
    Yep, something like this: 
        Protected Sub lbtnA_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbtnA.Click, lbtnB.Click
    
            Dim btn As LinkButton = CType(sender, LinkButton)
    
            If btn.ID = "lbtnA" Then
                ' Do stuff
            End If
    
            If btn.ID = "lbtnB" Then
                ' Do other stuff
            End If
    
        End Sub
    
     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 23, 2008 10:01 AM
  • User139975600 posted

    Yes, there sure is... the sender As Object passes you a reference to the object that sent it here.

    Dim buttonName as String = CType(sender, LinkButton).ID

    this will tell you the name of the button.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 23, 2008 10:02 AM