Home > Software design >  How do I get the value from the button? blazor
How do I get the value from the button? blazor

Time:07-07

I ran into a bit of a silly problem. I have this piece of code with button values and method when pressed, but how do I get the button value itself to change the calculator screen?

<div >
     <div >
        <div >
            <p>@equation</p>
        </div>
        <div >
            <button  value="AC" @onclick = "GetValue">AC</button>
            <button  value=" /-" @onclick = "GetValue">> /-</button>
            <button  value="%" @onclick = "GetValue">%</button>
            <button  value="/" @onclick = "GetValue">/</button>
            <button  value="7" @onclick = "GetValue">7</button>
            <button  value="8" @onclick = "GetValue">8</button>
            <button  value="9" @onclick = "GetValue">9</button>
            <button  value="X" @onclick = "GetValue">X</button>
            <button  value="4" @onclick = "GetValue">4</button>
            <button  value="5" @onclick = "GetValue">5</button>
            <button  value="6" @onclick = "GetValue">6</button>
            <button  value="-" @onclick = "GetValue">-</button>
            <button  value="1" @onclick = "GetValue">1</button>
            <button  value="2" @onclick = "GetValue">2</button>
            <button  value="3" @onclick = "GetValue">3</button>
            <button  value=" " @onclick = "GetValue"> </button>
            <button  value="0" @onclick = "GetValue">0</button>
            <button  value="," @onclick = "GetValue">,</button>
            <button  value="=" @onclick = "GetValue">=</button>
        </div>
    </div>
</div>
@code{
    private string equation = "0";
    private void GetValue(){
        equation =  "some value of the button";
    }
}

CodePudding user response:

For this type of thing, you need to pass the value into the event handler, like this

<button @onclick=@(_=>GetValue("AC"))>AC</button>

And, of course your handler needs to accept the value

private void GetValue(string value)
{
    equation =  value.ToString();
}

CodePudding user response:

Try something like this:

    private void GetValue(object sender, EventArgs e){
        equation =  sender.value;
    }
  • Related