2009-05-20 60 views
0
<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> 

这大致就是我想要做的。很明显,实现是错误的,但我无法找到任何有关我在实践中如何去做的信息。任何帮助表示赞赏。更改直放站内部的WebControl ID

编辑:我想要做的就是为此Repeater中的每个项目添加一个DropDownList,并在提交表单时,使用每个Yes/No答案的ID输入到数据库中。我使用的SqlDataReader有两个字段:问题内容和问题ID。

+0

你不能像这样给一个控件一个动态ID。告诉我们你在做什么(为什么它需要一个动态ID?),我们可以回答这个问题。干杯 – 2009-05-20 18:31:15

回答

4

我认为你最好使用Repeater内置的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; 
} 

一个Repeater基本上包含一个RepeaterItems集合。 RepeaterItems是使用ItemTemplate标签指定的。每个RepeaterItem都有自己的一组控件,它们本身就是一个Repeater,彼此相关联。

假设你从数据库中提取Repeater数据。每个Repeater项目代表来自查询结果中单个行的数据。因此,如果您将QuestionID分配给标签,将QuestionName分配给DropDownList,则标签中的ID将与下拉菜单中的名称匹配。

+0

我需要在加载表单时将ID设置为每个记录的QuestionID,以便我可以在存储区中使用这些ID和相应的值。 – 2009-05-20 18:41:05

0

你可以从标记文件中删除控件,并挂钩中继器的onItemDataBound事件。在这种情况下,您应该能够“手动”创建下拉控件,指定您想要的任何ID。