【发布时间】:2023-03-16 02:25:02
【问题描述】:
我有一个 ASP.NET WebForms 应用程序,它有一个 RadioButtonList,我想将 Bootstrap 的 CSS 类应用到它。
使用 RepeatLayout="Flow" 和 RepeatDirection="Vertical" 设置,RadioButtonList 将呈现如下:
<span id="PageContentPlaceHolder_RadioButtonList1">
<input id="PageContentPlaceHolder_RadioButtonList1_0" type="radio" name="ctl00$PageContentPlaceHolder$RadioButtonList1" value="1" />
<label for="PageContentPlaceHolder_RadioButtonList1_0">One</label>
<br />
<input id="PageContentPlaceHolder_RadioButtonList1_1" type="radio" name="ctl00$PageContentPlaceHolder$RadioButtonList1" value="2" />
<label for="PageContentPlaceHolder_RadioButtonList1_1">Two</label>
<br />
<input id="PageContentPlaceHolder_RadioButtonList1_2" type="radio" name="ctl00$PageContentPlaceHolder$RadioButtonList1" value="3" />
<label for="PageContentPlaceHolder_RadioButtonList1_2">Three</label>
</span>
我希望它像这样呈现(使用 Bootstrap 的 CSS 类):
<span id="PageContentPlaceHolder_RadioButtonList1">
<div class="form-check">
<input id="PageContentPlaceHolder_RadioButtonList1_0" type="radio" name="ctl00$PageContentPlaceHolder$RadioButtonList1" class="form-check-input" value="1" />
<label for="PageContentPlaceHolder_RadioButtonList1_0" class="form-check-label">One</label>
</div>
<div class="form-check">
<input id="PageContentPlaceHolder_RadioButtonList1_1" type="radio" name="ctl00$PageContentPlaceHolder$RadioButtonList1" class="form-check-input" value="2" />
<label for="PageContentPlaceHolder_RadioButtonList1_1" class="form-check-label">Two</label>
</div>
<div class="form-check">
<input id="PageContentPlaceHolder_RadioButtonList1_2" type="radio" name="ctl00$PageContentPlaceHolder$RadioButtonList1" class="form-check-input" value="3" />
<label for="PageContentPlaceHolder_RadioButtonList1_2" class="form-check-label">Three</label>
</div>
</span>
我意识到,如果我使用 Repeater 并将单个 RadioButton 和 Label 控件放在 ItemTemplate 中,我可以在其中应用 CssClass 值,这可能更容易做到。但是,我还需要附加一个RequiredFieldValidator 以确保用户做出选择。基本上这是一个选择题考试,我不能预先选择任何单选按钮项目。我没有太多运气让验证器使用它单独的单选按钮。如果我能提供客户端和服务器端验证代码,我想 CustomValidator 可能会起作用。
获得我想要的结果的最佳方法是什么?
【问题讨论】:
标签: css asp.net bootstrap-4 webforms radiobuttonlist