对于使用标准 asp:RadioButtonList 等的任何人。如果您将其添加到您的 .master 中,那么这将成为一次性工作,并且会自动选择 asp: 元素并为您设置样式:
放置控件时,您只需要定义
CssClass="custom-form-style"
用于列表和单独的项目。如果要在复选框上使用开关样式,则需要使用
CssClass="custom-form-style-switch"
脚本会为您添加所有包装器和类。要注意的另一件事是您必须定义标签。不过,您不需要将标签添加为单独的元素。要定义标签,您需要使用
Text="FooBar"
在控件中。
<asp:RadioButtonList ID="rdGender" runat="server" CssClass="custom-form-style">
<asp:ListItem Value="M" Text="Male" Selected="True"></asp:ListItem>
<asp:ListItem Value="F" Text="Female"></asp:ListItem>
</asp:RadioButtonList>
<asp:CheckBoxList ID="cbGender" runat="server" CssClass="custom-form-style">
<asp:ListItem Value="M" Text="Male" Selected="True"></asp:ListItem>
<asp:ListItem Value="F" Text="Female"></asp:ListItem>
</asp:CheckBoxList>
<asp:CheckBox ID="CheckBox1" runat="server" Text="FooBar" CssClass="custom-form-style" />
<asp:CheckBox ID="CheckBox2" runat="server" Text="FooBar" CssClass="custom-form-style-switch" />
<asp:RadioButton ID="RadioButton1" runat="server" Text="FooBar" CssClass="custom-form-style" />
默认情况下,放置在同一父 div 中的元素将垂直显示。如果你想要一个水平布局,你将不得不玩弄它。
处理格式化的脚本在一个函数中。这样做是为了在您动态构建元素时可以调用样式,而无需重新加载页面。
调用函数使用
<script type="text/javascript">
BootStrapChecksRadios()
</script>
这是页面加载脚本。
<script type="text/javascript">
$(document).ready(function () {
BootStrapChecksRadios()
});
function BootStrapChecksRadios() {
$('input', '.custom-form-style').each(function () {
if ($(this).parent().attr('class') == 'custom-form-style')
{
$(this).unwrap();
}
$(this).addClass("custom-control-input " + $(this).attr('id'))
$("label[for='" + $(this).attr('id') + "']").addClass("custom-control-label " + $(this).attr('id'))
if ($(this).attr('type') == 'radio') {
$("." + $(this).attr('id')).wrapAll("<div class='custom-control custom-radio' />");
} else {
$("." + $(this).attr('id')).wrapAll("<div class='custom-control custom-checkbox' />");
}
});
$('input[type=checkbox]', '.custom-form-style-switch').each(function () {
if ($(this).parent().attr('class') == 'custom-form-style-switch')
{
$(this).unwrap();
}
$(this).addClass("custom-control-input " + $(this).attr('id'))
$("label[for='" + $(this).attr('id') + "']").addClass("custom-control-label " + $(this).attr('id'))
$("." + $(this).attr('id')).wrapAll("<div class='custom-control custom-switch' />");
});
}
</script>