Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the text value of the button that was clicked

I am trying to get the text value from a button that was clicked. In my head, it looks something like this:

private void button2_Click(object sender, EventArgs e)
{
    string s =  thisbutton.text
}
like image 617
user3755946 Avatar asked Oct 23 '15 10:10

user3755946


People also ask

How do I get the value of a button clicked?

Select all the buttons and store in a variable using the querySelectorAll() method. let buttonList = document. querySelectorAll("button"); buttonList variable returns a nodeList that represents all the buttons.

Can I get value from Button JavaScript?

Use the value property to get the value of a button in JavaScript.

How do you get the ID of an element in JavaScript from a clicked?

The buttonPressed() callback function will have a returned event object which has all the data about the HTML element that is clicked on. To get the clicked element, use target property on the event object. Use the id property on the event. target object to get an ID of the clicked element.

How do you call a button click event?

How can I call SubGraphButton_Click(object sender, RoutedEventArgs args) from another method? private void SubGraphButton_Click(object sender, RoutedEventArgs args) { } private void ChildNode_Click(object sender, RoutedEventArgs args) { // call SubGraphButton-Click(). }


2 Answers

The object which fired the event is sender, so:

private void button2_Click(object sender, EventArgs e)
{
    string s = (sender as Button).Text;
}
like image 96
Alex Avatar answered Oct 21 '22 03:10

Alex


Just cast the sender Object to a Button Object and access the text attribute :

protected void btn_Click (object sender, EventArgs e){
   Button btn = sender as Button;
   string s= btn.Text
}
like image 35
Wael Sakhri Avatar answered Oct 21 '22 05:10

Wael Sakhri