2

I have a Repeater which loads some data from a SQL database

<asp:Repeater ID="Repeater" runat="server" OnItemDataBound="Repeater_ItemDataBound">
    <ItemTemplate>
        <asp:Label ID="QuestionLabel" runat="server" Text=""></asp:Label>
        <asp:TextBox ID="AnswerLabel" runat="server"></asp:TextBox>
    </ItemTemplate>
</asp:Repeater>

<asp:Button ID="AnswerSubmit" runat="server" Text="Insert"/>

In code behind, i assign to get the questions on another button click to load and bind the Repeater. In ItemDataBound i find the controls and assign the questions to the label.

How should i get the answers that the user enters and store the ID of the question with the answer they enter in a button click event?

At first, I tried this in the button click event

foreach (RepeaterItem item in Repeater.Items)
{
    Label QuestionLabel = (Label)item.FindControl("QuestionLabel");
}

but it couldnt find the QuestionLabel. Looking at the page source i believe thats due to the label having a different value (QuestionLabel_0, QuestionLabel_1 etc), so struggling to find a way to approach this?

Edit

    protected void QuestionButton_Click(object sender, EventArgs e)
    {
        Repeater.DataSource = QuestionsByID(ID);
        Repeater.DataBind();
    }
Computer
  • 2,149
  • 7
  • 34
  • 71
  • what are you trying to achieve probably you don t even need .net controls in repeater. Just try to use html elements. – Onur Topal Oct 02 '14 at 10:59

1 Answers1

1

You see

foreach (RepeaterItem item in Repeater.Items)
{
  if (item.ItemType == ListItemType.Item 
        || item.ItemType == ListItemType.AlternatingItem) 
    { 
      Label QuestionLabel = (Label)item.FindControl("QuestionLabel");
    }
}
Community
  • 1
  • 1
Fabio
  • 1,890
  • 1
  • 15
  • 19