Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need id of calling object

Status
Not open for further replies.

lunchbox88

Programmer
Feb 17, 2004
208
US
C#

I have a form with 2 textboxes. I want to add a button by each text box to count the characters. I can do this the really easy way by creating 2 buttons, and coding an OnClick for each button. I'd rather check to see which button is clicked, and count the characters in the appropriate textbox.

How can I get the ID of the button that was clicked?

Thanks!
 
Hi,
When u click on Button,U will get Button click event.That button click event has first parameter as Sender of type Object.Sender specifies the source of the object whic raises this click event.
Hope this helps to You
 
I know I can use that object, but I'm not sure how to get the name of the object.

I know that GetType will return System.Web.UI.WebControls.Button, but I'm not sure how to get the name/ID of the button.

Thanks
 
Simply create an event that handles both of the Button's Click event and then use the sender object to get a reference to the Button and it's ID/Name.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Ok, that's what I said I wanted to do, but I'm not sure HOW to do it. How do I get the button ID/Name?
 
Simply create an event that handles both of the Button's Click event and then use the sender object to get a reference to the Button
If you re-read the above, it should be fairly self-explanatory e.g.
Code:
    Private Sub Button1_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click
       Dim btn as New Button
       btn = Ctype(sender, Button)
       ' Do whatever you want with the Button here...
    End Sub


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top