VB getting ID of imagebutton onclick

nickCroat 106 Reputation points
2021-10-16T22:20:34.23+00:00

Hi

In my Web site I use VB and I have a sub where I have 5 onClick imageButtons that can trigger that sub.

How can I get ID of the button which triggered that sub so I can use it in If statement?

aspx:
<asp:ImageButton ID="gb" runat="server" ImageUrl="gb.png" />
<asp:ImageButton ID="de" runat="server" ImageUrl="de.png" />
<asp:ImageButton ID="hr" runat="server" ImageUrl="hr.png" />
<asp:ImageButton ID="cz" runat="server" ImageUrl="cz.png" />
<asp:ImageButton ID="hu" runat="server" ImageUrl="hu.png" />

vb
Public sub subLanguage_onClick() Handles gb.onclick, de.onclick, hr.onclick, cz.onclick, hu.onclick

Dim strClickedButton as String

strClickedButton = ????

End sub

I wrote code on my mobile phone so maybe I made some error since I don't have an example in front of me

Thanks for helping out ;)

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,254 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,568 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Viorel 112.1K Reputation points
    2021-10-17T05:25:57.463+00:00

    Try something like this:

    Sub subLanguage_onClick( sender As Object, e As ImageClickEventArgs ) Handles gb.onclick, de.onclick, hr.onclick, cz.onclick, hu.onclick
    
       Dim b As ImageButton = CType(sender, ImageButton)
       Dim id As String = b.ID
       . . .
    
    End Sub
    
    0 comments No comments

  2. Albert Kallal 4,651 Reputation points
    2021-10-17T18:33:27.88+00:00

    You have a good answer. If you going to pass the button value say to client js, or need a referance when using a master page, then you might need/want the client id.

    But, if just looking for the id, then btn.ID will work, so this:

      Protected Sub gb_Click(sender As Object, e As ImageClickEventArgs) Handles gb.Click, de.Click, hr.Click, cz.Click
    
            Dim Btn As ImageButton = sender
    
            Debug.Print("ID of button = " & Btn.ID)
            Debug.Print("ID of button = " & Btn.ClientID)
            Debug.Print("image url = " & Btn.ImageUrl)
    
    
        End Sub
    

    So, in some cases, you might need/want to use client id if the control is used for anything more then just some case or if/then to act for each button clicked.

    Regards,
    Albert D. Kallal (Access MVP 2003-2017)
    Edmonton, Alberta Canada

    0 comments No comments

  3. Yijing Sun-MSFT 7,066 Reputation points
    2021-10-18T02:08:24.867+00:00

    Hi @nickCroat ,
    According to your descriptions,I suggest you could get ID or ClientID just like this:

    Public sub subLanguage_onClick() Handles gb.onclick, de.onclick, hr.onclick, cz.onclick, hu.onclick  
        Dim ID As String = (CType(sender, ImageButton)).ID  
        Dim CLientID As String = (CType(sender, ImageButton)).ClientID  
    End Sub  
    

    Note: Every controls have ID, but if you have Repeater control or Updatepanel control,the ID is a long string,so you need ClientID.

    Best regards,
    Yijing Sun


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments