我可以将不是 ListItems 的控件添加到 RadioButtonList 中吗?

发布于 2024-08-30 01:17:23 字数 526 浏览 8 评论 0原文

我需要提供一种方法来允许用户选择 1 个主题,但这些主题被分组为资格,因此我想为每个“组”显示一个标题。我的第一个想法是 asp:RadioButtonList,因为它提供了一个选项列表,从中只能选择一个,但无法添加标题来分解列表。

所以我尝试了以下代码 - 但我看不到正在添加 LiteralControl,这让我认为这不是一个有效的方法。

For Each item As Subject In Qualifications
    If item.QualCode <> previousQualCode Then
        rblSelection.Controls.Add(New LiteralControl(item.QualName))
    End If

    rblSelection.Items.Add(New ListItem(item.SubjectName, item.SelectionCode))
    previousQualCode = item.QualCode
Next

I need to provide a way to allow users to select 1 Subject, but the subjects are grouped into Qualifications and so I want to show a heading for each 'group'. My first thought is an asp:RadioButtonList as that provides a list of options from which only one can be selected but there's no means to add break the list up with headings.

So I tried the following code - but I can't see that the LiteralControl is being added, which makes me think it's not a valid approach.

For Each item As Subject In Qualifications
    If item.QualCode <> previousQualCode Then
        rblSelection.Controls.Add(New LiteralControl(item.QualName))
    End If

    rblSelection.Items.Add(New ListItem(item.SubjectName, item.SelectionCode))
    previousQualCode = item.QualCode
Next

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终止放荡 2024-09-06 01:17:23

不,你不能这样做。仅具有ListItems才有效。另一种方法是为每个主题设置一个 RadioButton。为它们提供相同的组名,这意味着您只能设置一个,但您可以在页面上(也许在标题下)按照您喜欢的方式排列它们。

    <h3>Qualification 1</h3>
    <asp:RadioButton id="rbSubject" runat="server" />
    <asp:RadioButton id="rbSubject1" runat="server" />
    <asp:RadioButton id="rbSubject2" runat="server" />
    <h3>Qualification 2</h3>
    <asp:RadioButton id="rbSubject3" runat="server" />
    <asp:RadioButton id="rbSubject4" runat="server" />

代码隐藏:

        rbSubject.GroupName = "grp1";
        rbSubject1.GroupName = "grp1";
        rbSubject2.GroupName = "grp1";
        rbSubject3.GroupName = "grp1";
        rbSubject4.GroupName = "grp1";

在您的情况下,使用占位符控件并将动态生成的控件和标头添加到占位符控件集合中。

For Each item As Subject In Qualifications
    If item.QualCode <> previousQualCode Then
        placeholder.Controls.Add(New LiteralControl(item.QualName))
    End If
    create new button here:- sorry Im a C# dev, I dont know the VB syntax and I am editing from my phone, but you should be able to work it out.

    placeholder.Controls.Add(btn)
    previousQualCode = item.QualCode
Next

No you can't do this. It is only valid to have ListItems. Another approach would be to have a RadioButton for each subject. Give them all the same GroupName which will mean you can only set one, but you can arrange them however you like on the page, under headings, maybe.

    <h3>Qualification 1</h3>
    <asp:RadioButton id="rbSubject" runat="server" />
    <asp:RadioButton id="rbSubject1" runat="server" />
    <asp:RadioButton id="rbSubject2" runat="server" />
    <h3>Qualification 2</h3>
    <asp:RadioButton id="rbSubject3" runat="server" />
    <asp:RadioButton id="rbSubject4" runat="server" />

Code Behind:

        rbSubject.GroupName = "grp1";
        rbSubject1.GroupName = "grp1";
        rbSubject2.GroupName = "grp1";
        rbSubject3.GroupName = "grp1";
        rbSubject4.GroupName = "grp1";

In your case, use a placeholdercontrol and add the dynamically generated controls and headers to the placeholders Controls collection.

For Each item As Subject In Qualifications
    If item.QualCode <> previousQualCode Then
        placeholder.Controls.Add(New LiteralControl(item.QualName))
    End If
    create new button here:- sorry Im a C# dev, I dont know the VB syntax and I am editing from my phone, but you should be able to work it out.

    placeholder.Controls.Add(btn)
    previousQualCode = item.QualCode
Next
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文