73

I am trying to Enable/Disable a group of time inputs in Blazor based on a checkbox ; while for inputs of type button the below solution works ,for inputs of type time it doesn't :

Solution for button input that works:

<button type="button" class="@this.SetButton"></button>

[Parameter] public bool state { get; set; }

public string SetButton() {
    string result = state ? "" : "disabled";
    return result;
}

Attempt for time inputs that does not work:

<input bind="@IsDisabled" type="checkbox" />                      
<input class="@this.GetGroupState()" type="time" />

protected bool IsDisabled { get; set; }

public string GetGroupState() {
    return this.IsDisabled ? "disabled" : "";
}

P.S.: In the first scenario the bool comes as a parameter from another component so I don't bind it. In the second case, however, it is bound to the checkbox.

Massimiliano Kraus
  • 3,638
  • 5
  • 27
  • 47
Bercovici Adrian
  • 8,794
  • 17
  • 73
  • 152

9 Answers9

144

To disable elements you should use the disabled attribute.

I've modified your code a bit and this will do what you're after. Blazor will automatically add or remove the disabled attribute based on the IsDisabled value.

You should use the disabled attribute on your button as well. It's a much better practice.

<button type="button" disabled="@IsDisabled"></button>
<input @bind="IsDisabled" type="checkbox" />

<input disabled="@IsDisabled" type="time" />

@code{
    protected bool IsDisabled { get; set; }
}

You can still combine this with applying a CSS class for styling the disabled element. It's up to you.

Chris Sainty
  • 7,861
  • 2
  • 37
  • 59
  • 37
    Yes, Blazor makes `disabled="false"` disappear. Takes a little getting used to. – H H Mar 05 '19 at 12:35
  • 6
    this answer is certainly correct- i just want to point out that using disabled attributes is not a safe way to prevent form data from being edited or saved as users can easily modify the html client side to remove the disabled attribute then modify the field – GregH Mar 05 '19 at 16:50
  • 3
    @GregH is correct, you should render your control as a non-editable element, like a label if it is disabled, this will prevent client side fiddling. – Mister Magoo Mar 06 '19 at 10:58
  • 3
    @Sergey Fixed. Just for any future readers, it actually doesn't matter if you use quotes or not around variables in attributes. You're not writing HTML its Razor. So `disabled="@IsDisabled"` and `disabled=@IsDisabled` are both perfectly valid. – Chris Sainty Mar 26 '19 at 09:50
  • 1
    Thanks Chris, dunno why I didn't just try it, tried everything but. I had no idea that when false, Blazor made the attribute go-away :-) been scratching my head for hours trying to figure out how to make it work. – shawty Sep 16 '19 at 21:04
  • 1
    If you get a "is not a valid value" warning, that's just a bug of Blazor 3.x, see here: https://github.com/aspnet/AspNetCore/issues/16833 – Paolo Fulgoni Dec 24 '19 at 14:35
  • 1
    I'm sure I'm abusing HTML, but I've got an @onclick on an
  • and disabling this doesn't seem to work. Is it only buttons and inputs that can be disabled in Blazor?
  • – Uxonith Apr 01 '20 at 17:02
  • 2
    @Uxonith Disabled only works if the element does something that can be disabled, like a click. li’s don’t have a action function they are just a way of displaying something. I’d put a button in your li and remove the OnClick. It’s best not to mess with HTML semantics for the sake of accessibility and such. – Chris Sainty Apr 01 '20 at 17:19
  • 1
    Kind of obvious in hind sight but in case it helps anyone get past a brain fart: A string value of "false" won't work, it's got to be a boolean. – Gabe Dec 23 '20 at 00:32
  • 1
    For net 6 (I guess net5 too), this part should be rewritten: `bind = "@IsDisabled"` as `@bind = "IsDisabled"` – Sith2021 Dec 12 '21 at 15:35
  • Don't forget you might need to call `StateHasChanged()` after changing the bool value that drives the `Disabled` state of the input/button. – Paul Suart Sep 02 '22 at 11:26