【问题标题】:How to use Repeater inside RadioButtonList to repeat ListItem of RadioButtonList如何在 RadioButtonList 中使用 Repeater 来重复 RadioButtonList 的 ListItem
【发布时间】:2016-11-14 07:27:35
【问题描述】:

首先,repeater with in a repeater 可以用吗? 如果是,那么我如何在以下场景中使用嵌套中继器。

<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

中继绑定

rp_Question.DataSource = _question.GetAll();
rp_Question.DataBind();

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

【问题讨论】:

    标签: c# asp.net repeater


    【解决方案1】:

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

    <asp:Repeater ID="rp_Question" runat="server" OnItemDataBound="rp_Question_ItemDataBound">
    

    然后在代码后面。

    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】:

      很遗憾,您不能在asp:RadioButtonList 中使用中继器。它只允许ListItem 在里面。你会得到一个错误,那个中继器是一个未知的元素。但是你可以在后面的代码中绑定asp:RadioButtonList

      【讨论】:

      • 你能分享一些代码来绑定'asp:RadioButtonList'放在中继器内吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-20
      • 2012-03-13
      • 1970-01-01
      • 2010-11-07
      • 2023-03-11
      • 1970-01-01
      相关资源
      最近更新 更多