按钮中的禁用选项不会动态更改



我正试图根据设置OnInitialized的值在按钮中设置禁用的属性。但是,即使EnableRegistration设置为true OnInitialized,该按钮仍处于禁用状态。如何根据EnableRegistration切换禁用的属性?

@page "/course/register"
<h3 class="my-5">Course Registration</h3>
<div class="container">
<div class="alert alert-info">
<p class="fw-bold">People who register for full course will be given priority over individual module registrations. 
After registration, look for an email from us to see if you got in and instructions to pay the course fee.
Registrations will open at 6:00 PM on March 20th.
</p>
</div>
<div class=row>
<div class="col-6">
<button onclick="Register" id="fullcourse" disabled="@(EnableRegistration == true ? "false": "true")" class="btn btn-primary">Register for Full Course</button>

</div>
<div class="col-6">
<button onclick="Register" id="fullcourse" disabled="@(EnableRegistration == true ? "false": "true")" class="btn btn-secondary">Register for First Module</button>
</div><br/>
@if (!string.IsNullOrEmpty(Message))
{
<p>@Message</p>
}
</div>
</div>

@code {
public bool EnableRegistration;
public bool Registered = false;
public string Message = "";
protected override void OnInitialized()
{
var registrationDateTime = new DateTime(2022, 3, 15, 6, 33, 0);
if(Registered == false && (DateTime.Compare(DateTime.Now, registrationDateTime) == 1 || DateTime.Compare(DateTime.Now, registrationDateTime) == 0))
{
EnableRegistration = true;
}
}
public void Register()
{
Message = "Registered";
}
}

谢谢。

更换

disabled="@(EnableRegistration == true ? "false": "true")"

带有

disabled="@(!EnableRegistration)"

HTML不支持="true",但Blazor支持。最终,您只需要disabled或一个空字符串。剃刀编译器为您安排。

您需要

<button id="fullcourse" disabled="@(!EnableRegistration)"  class="btn btn-primary">Register for Full Course</button>

相关内容

  • 没有找到相关文章

最新更新