Question

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

This is roughly what I'm trying to do. Obviously, the implementation is faulty, but I can't find any information on how I'd go about this in practice. Any help is appreciated.

Edit: What I'm trying to do exactly is add a DropDownList for each item in this Repeater, and upon submission of the form, use the ID's of each Yes/No answer to input into a database. The SqlDataReader that I'm using has two fields: The question content and the questionID.

Was it helpful?

Solution

I think you'd be better off using the built in support for IDs inside a Repeater. If the goal is to assign it an ID to make it easy to find the proper control after the data has been bound you might try something like this:

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

Then, in your code you can loop through the items in the Repeater until you find the label you're looking for:

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;
}   

A Repeater basically consists of a collection of RepeaterItems. The RepeaterItems are specified using the ItemTemplate tag. Each RepeaterItem has its own set of controls that are, by the very nature of a Repeater, associated with each other.

Say you're pulling the Repeater data from a database. Each Repeater item represents data from an individual row in the query results. So if you assign the QuestionID to a label and the QuestionName to a DropDownList, the ID in the label would match up with the name in drop down.

OTHER TIPS

Could you remove the control from the markup file, and hook the repeater's onItemDataBound event. In that event, you should be able to create the dropdown control "manually", assigning whatever ID you want.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top