首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在RadioButtonList中使用直放站重复ListItem of RadioButtonList

如何在RadioButtonList中使用直放站重复ListItem of RadioButtonList
EN

Stack Overflow用户
提问于 2016-11-14 07:27:35
回答 2查看 1.6K关注 0票数 1

首先,可以使用中继器和中继器吗?如果是的话,我将如何在下面的场景中使用嵌套中继器。

代码语言:javascript
运行
复制
<div class="row">
    <asp:Repeater ID="rp_Question" runat="server">
        <ItemTemplate>
            <p class="_100">
                <h2 id="h4_Question" runat="server"><%# Eval("question_text") %></h2>
            </p>
            <p class="left">
                <asp:RadioButtonList ID="rb_Question" runat="server">
                    <asp:ListItem Text="Option1" Value="1"></asp:ListItem>
                    <asp:ListItem Text="Option2" Value="2"></asp:ListItem>
                    <asp:ListItem Text="Option3" Value="3"></asp:ListItem>
                    <asp:ListItem Text="Option4" Value="4"></asp:ListItem>
                </asp:RadioButtonList>
            </p>
        </ItemTemplate>
    </asp:Repeater

中继器绑定

代码语言:javascript
运行
复制
rp_Question.DataSource = _question.GetAll();
rp_Question.DataBind();

每个问题的选项都保存在数据库中,最小选项可以是3,最大选项可以是6。如何使用rp_Question中的其他中继器重复每个问题的选项。我想表现得像这样。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-11-14 13:15:40

扩展KateCute给出的答案,您可以使用ItemDataBound事件。

代码语言:javascript
运行
复制
<asp:Repeater ID="rp_Question" runat="server" OnItemDataBound="rp_Question_ItemDataBound">

然后在后面的代码中。

代码语言:javascript
运行
复制
protected void rp_Question_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    //find the radiobuttonlist with findcontrol and cast back to it's original type
    RadioButtonList rb_Question = e.Item.FindControl("rb_Question") as RadioButtonList;

    //get the current datarow
    DataRowView row = e.Item.DataItem as DataRowView;

    //get the id from the datarow object
    string questionID = row["question_id"].ToString();

    //get the answers from the db with questionID and bind them as listitems just like in the loop below

    //just a loop to add some listitems for demo
    for (int i = 0; i < 5; i++)
    {
        rb_Question.Items.Insert(i, new ListItem("Option " + i.ToString(), i.ToString(), true));
    }
}
票数 2
EN

Stack Overflow用户

发布于 2016-11-14 08:20:51

不幸的是,您不能在asp:RadioButtonList中使用中继器。它只允许内部的ListItem。您将得到一个错误,该中继器是一个未知的元素。但是您可以在后面的代码中绑定asp:RadioButtonList

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40583778

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档