Changing the WebControl id inside the relay
<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 am trying to do. The implementation is obviously flawed, but I can't find any information on how I did it in practice. Any help is appreciated.
Edit: what I'm trying to do is add a DropDownList for each item in this repeater, and after submitting the form, use the ID of each Yes / No response to enter into the database. The SqlDataReader I am using has two fields: question content and question id.
a source to share
I think you're better off using the built-in support for identifiers inside Repeater. If the goal is to assign an ID to it so that it is easy to find the correct control after the data has been linked, you can 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 elements in the Repeater until you find the shortcut you are 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 mainly consists of a collection of RepeaterItems. RepeaterItems are specified using the ItemTemplate tag. Each RepeaterItem has its own set of controls, which by the very nature of a repeater are related to each other.
Say you are fetching the Repeater data from the database. Each Repeater element represents data from a separate row in the query results. Therefore, if you assign QuestionID to the label and QuestionName in the DropDownList, the ID in the label will match the dropdown name.
a source to share