Blazor server side - set diferent buttons state based on a method

João Coelho 41 Reputation points
2021-01-04T11:34:33.647+00:00

How can i set diferent buttons state an colors, based on a method thar runs before rendering?

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,387 questions
0 comments No comments
{count} votes

Accepted answer
  1. Michael Wang-MSFT 1,051 Reputation points
    2021-01-05T07:56:33.353+00:00

    +Update

    Hi, @João Coelho , you can override the OnInitialized method. It will execute when page load.
    55666-image.png

    @code {  
        private int state = 0;  
        private string readBg { get; set; }  
        private string greenBg { get; set; }  
        private string blueBg { get; set; }  
      
        protected override void OnInitialized() // = On PageLoad  
        {  
            readBg = "red";  
            greenBg = "green";  
            blueBg = "blue";  
        }  
    }  
    

    55645-image.png

    Hi, @João Coelho ,

    You could try as below.

    53507-image.png

    @code {  
        private int state = 0;  
        private string Background { get; set; } = "red";  
      
        private void ChangeValues()  
        {  
            state++;  
            if (state % 3 == 0)  
                Background = "red";  
            if (state %3 == 1)  
                Background = "green";  
            if (state % 3 == 2)  
                Background = "yellow";  
        }  
    }  
    

    The screenshots of test:

    53557-333.gif

    ------
    If the answer doesn’t solve your issue, please provide more details of error that will help us track down what’s happening.
    If the answer is helpful, please click "Accept Answer" and upvote it.
    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.

    Best Regards,
    Michael Wang


0 additional answers

Sort by: Most helpful