84

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.

10 Answers 10

162

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.

11
  • 40
    Yes, Blazor makes disabled="false" disappear. Takes a little getting used to. Commented Mar 5, 2019 at 12:35
  • 7
    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
    Commented Mar 5, 2019 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. Commented Mar 6, 2019 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. Commented Mar 26, 2019 at 9:50
  • 3
    @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. Commented Apr 1, 2020 at 17:19
16

There is an alternative way you can achieve this.

<fieldset disabled=@ShouldBeDisabled>
  Your input controls in here will be disabled/enabled by the browser
</fieldset>
0
16

Quotes can make all the difference, or at least during server prerendering:

a) Without quotes - The disable parameter will be removed when it's evaluated as false. This will work as expected:

<input disabled=@(IsDisabled) ...

b) With quotes - It will add a value of "True" or "False" to the parameter eg. disabled="True" or disabled="False". It will remain disabled as the browser is on the hunt for the parameter rather than a value.

<input disabled="@(IsDisabled)" ... 
13

You can also get the value to disable the button directly as an expression

<input disabled="@(MyCondition ? true : false)" type="checkbox" />     
4
  • 6
    What is the purpose of true : false? Isnt that the value of MyCondition to begin with?
    – jonas
    Commented Jul 23, 2021 at 7:30
  • 4
    Yeah, I don't see the point of the ternary operator. Just @MyCondition should be fine. Commented Sep 14, 2021 at 10:49
  • Great example of code for codes sake. You basically wrote If(MyCondition == true) { return true; } else { return false; }. I would suggest you delete this answer, but for some reason you have some upvotes, god only knows how... Commented Feb 26 at 11:29
  • no button referenced in the code either...
    – mxmissile
    Commented Mar 21 at 16:46
5

Adding disabled is not really a good solution because the client can inspect the page and remove the disabled attribute. I think the best solution is to add disabled and remove the two way binding by using value instead of @bind.

@if (IsDisabled)
{
    <input type="checkbox" value="@Value" disabled />
}
else
{
    <input type="checkbox" @bind="Value" />
}

Now even if the user removes the disabled attribute the value can be changed but it won't be updated.

I prefer to put all my input components into their own component that inherits InputBase for two way binding while handling IsDisabled as described.

4

With the reference of the above answer a) Without Quotes

<input disabled=@(IsDisabled) ...

Incase you set IsDisabled to true, the above line will not disable the input

This solves by adding !IsDisabled

Blazor Input Control

<InputText @bind-Value="UserModel.UserName" id="userName" class="form-control" disabled=@(!IsDisabled) />

HTML Input Control

<input disabled=@(!IsDisabled) ...

0
1

Blazor makes disabled="false" disappear. This statement is not true! The only way is to use if statement like this.

@if (IsDisabled)
{
    <input type="checkbox" @bind="Value" disabled />
}
else
{
    <input type="checkbox" @bind="Value" />
}
3
  • Should be @bind = "IsDisabled"
    – Sith2021
    Commented Dec 12, 2021 at 15:37
  • @Sith2021 you are totally not following this entire discussion. They are talking about adding the disabled attribute to a HTML element.
    – JohnB
    Commented Aug 7, 2022 at 17:44
  • @Vesko Use @value instead of @bind on the disabled input to prevent client side manipulation as seen here.
    – clamchoda
    Commented Dec 21, 2023 at 14:33
1

I have simplified the complete code - Working!

Test Cases:
By default checkbox is unchecked, and submit button is disabled.
When checkbox checked, submit button enabled
When checkbox un-checked, submit button disabled 
<div class="card">
    <div class="card-header">Final Submission</div>
    <div class="card-body">

        <div class="form-check">
            <input id="xbrlfinal" class="form-check-input" type="checkbox" bind="@IsAcknowledged" 
            @onchange="@((args) => IsAcknowledged = (bool)args.Value)">
            <label class="form-check-label" for="flexCheckDefault">
                I hereby declare that I have checked and verified.
            </label>
        </div>

    </div> <!-- Main Card body ends -->
    <div class="card-footer text-muted">
        <div class="d-grid gap-2 d-md-flex justify-content-md-end">
            <button type="submit" class="btn btn-primary me-md-3" @onclick="OnSubmissionProcess" disabled="@(IsAcknowledged?false:true)">Process</button>

        </div>
    </div>
</div> <!-- Main Card ends --> 
 protected bool IsAcknowledged { get; set; }
1

For me neither of these worked

<input disabled="@(IsDisabled)" type="radio" />
<input disabled=@(IsDisabled) type="radio" />

I had to remove the quotes and the braces, and this finally worked

<input disabled=@IsDisabled type="radio" />
0

HTML disabled property cannot get value true or false. Only way is add it or not: https://www.w3schools.com/tags/att_input_disabled.asp

2
  • 2
    This answer is quite misleading since Blazor works a little differently when it comes to handling HTML attributes. Consider having a look at this answer: stackoverflow.com/a/69016913/6877769 Commented Nov 15, 2022 at 10:36
  • It's not misleading, it's simply wrong. Commented Mar 2, 2023 at 1:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.