对于尝试处理复选框的人来说,这些解决方案都不起作用,因为您需要它是一个 HTML 输入(而不是一个 HTML 按钮)。
SPArcheon 的答案在您添加 type="checkbox" 后不起作用,这是 .checked 运算符在代码隐藏中工作所必需的。只要添加type="checkbox",HTML 输入框就没有OnServerClick、ServerClick 和CheckedChanged。你只有ServerChanged,这没用,因为它需要刷新整个页面。
我实际上必须使用 CSS 来使 HTML 按钮类似于一个复选框,使用 Bootstrap 的 btn-outline-secondary 类作为起点。
.btn-checkbox-unchecked {
padding: .35rem .35rem !important;
font-size: .875rem !important;
line-height: .5 !important;
border-radius: .15rem !important;
border-width: .20em !important;
}
.btn-checkbox-checked {
padding: .1rem .1rem !important;
font-size: .6rem !important;
line-height: .5 !important;
border-radius: .15rem !important;
border-width: .20em !important;
border-color: #0060df !important;
background-color: #0060df !important;
}
.btn-checkbox:hover {
border-color: #676774 !important;
background-color: #0250BB !important;
}
在aspx中:
<button class="btn btn-xs btn-outline-secondary" id="btnFakeCheckbox" runat="server" clientidmode="static" autopostback="true" data-bs-toggle="button" autocomplete="off"></button>
然后在您的代码隐藏中,您可以相应地设置属性(类)
检查时:
btnFakeCheckbox.Attributes.Add("class", "btn btn-xs btn-secondary btn-checkbox-checked active")
btnFakeCheckbox.Attributes("aria-pressed") = "true"
btnFakeCheckbox.InnerHtml = "<i class='fas fa-check'></i>"
未选中时:
btnFakeCheckbox.Attributes.Add("class", "btn btn-xs btn-outline-secondary btn-checkbox-unchecked")
btnFakeCheckbox.Attributes("aria-pressed") = "false"
btnFakeCheckbox.InnerHtml = ""
(请注意最后一行的字体真棒检查图标,您必须将其包含在您的 aspx 中)
对于一个更荒谬的框架来说,这是多么荒谬的解决方法。如果有人有 Bootstrap 5 拨动开关的解决方案,请告诉我。