<ItemTemplate>
    <asp:Label runat="server"><%#DataBinder.Eval(Container.DataItem, "Question")%></asp:Label>
    <asp:DropDownList runat="server" id="<%#DataBinder.Eval(Container.DataItem, "QuestionID")%>">>
        <asp:ListItem value="1" text="Yes" />
        <asp:ListItem value="0" text="No" />
    </asp:DropDownList>
<ItemTemplate>

这大致就是我想要做的事。显然,实施有故障,但我找不到,我怎么会去了解这个在实践中的任何信息。任何帮助表示赞赏。

编辑:我试图做的恰恰是添加一个DropDownList在此Repeater的每个项目,在提交表单的,每个使用回答是/否的ID的输入到数据库中。我使用SqlDataReader中有两个字段:此问题含量和questionID

有帮助吗?

解决方案

我想你会更好使用内置的一个中继器内的ID的支持。如果我们的目标是给它分配一个ID,可以很容易找到合适的控制数据已绑定,你可以尝试这样的事情后:

<asp:Repeater ID="Repeater1" runat="server>
    <ItemTemplate>
        <asp:Label ID="QuestionID" Visible="False" Runat="server"><%#DataBinder.Eval(Container.DataItem, "FieldContent")%></asp:Label>
        <asp:DropDownList ID="MyDropDownList" Runat="server"></asp:DropDownList>
    </ItemTemplate>
</asp:Repeater>

然后,在你的代码,你可以通过在直放站项目循环,直到你找到你要找的标签:

foreach (RepeaterItem curItem in Repeater1.Items)
{
    // Due to the way a Repeater works, these two controls are linked together.  The questionID
    // label that is found is in the same RepeaterItem as the DropDownList (and any other controls
    // you might find using curRow.FindControl)
    var questionID = curRow.FindControl("QuestionID") as Label;
    var myDropDownList = curRow.FindControl("MyDropDownList") as DropDownList;
}   

一个中继器基本上由RepeaterItems的集合。该RepeaterItems使用的ItemTemplate标签中指定。每个的RepeaterItem具有其自己的一组是,通过中继器的性质控制,彼此相关联。

假设您正在从数据库中提取的Repeater数据。每个转发器项目代表在查询结果中的各个行数据。所以,如果你指定QuestionID的标签和QuestionName到一个DropDownList,在标签的ID将匹配在下拉菜单的名称。

其他提示

你能删除标记文件的控制,和钩中继器的onItemDataBound事件。在这种情况下,你应该能够为“手动”创建下拉控制,分配任何你想要的ID。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top