Anothercoder
Anothercoder

Reputation: 49

Use string as a button value

Is there any way i could use string or int as a button value?

I'm using MS visual studio and i have various buttons on my form.

I would like to do something like this:

string value = "1";

if (button(value).Text == "X")
{
    //Do something
}

Upvotes: 0

Views: 654

Answers (2)

Mouni
Mouni

Reputation: 59

<input type="button" value="one" id="One" onclick= "click(this.value)'"/>

where click(this.value) is function on javascript

function click(x)
{
    //x is value from onclick button
}

Upvotes: 0

TheGeneral
TheGeneral

Reputation: 81493

I assume you want to do an action on click

You can handle multiple buttons like this and use your if there, or just make separate events for them

btn1.Click += new System.EventHandler(this.btn_Click);
btn2.Click += new System.EventHandler(this.btn_Click);
btn3.Click += new System.EventHandler(this.btn_Click);

...

void btn_Click(object sender, EventArgs e)
{

    var button = sender as button;
    // if here if you need it
    //if(button.Tag == 'X') or if (button.Text == 'X') 
    //{
    //    do something
    //}
    //else
    //{
    //}
}

Button.OnClick Method (EventArgs)

Upvotes: 1

Related Questions