1

I am building an ASP.NET website that allows users to create and take tests. Tests can contain various types of questions (multiple choice, true/false, essay, etc.). Because of the dynamic nature of the tests, I am creating the "Take Test" page with repeaters.

My problem now is: how can I get the user's answers? With a fixed number/type of questions this would be simple, but I'm not sure how to grab answers from items with dynamically created IDs or how to pass a variable number of answers back to my database.

Edit: I found my answer here.

Community
  • 1
  • 1
Brian Koser
  • 439
  • 1
  • 12
  • 28

3 Answers3

1

You can use Request.Form

But Here is Another approach using FindControl and Repeater:

    For Each item As RepeaterItem In Me.RptItems.Items

        Dim value = CType(item.FindControl("TxtName"), TextBox).Text

    Next

you can use FindControl method with each RepeaterItem and find desired control inside it by ID.

ASPX file:

<asp:Repeater ID="RptItems" runat="server">
    <HeaderTemplate>
        <table>
            <tr>
                <td>
                    Name
                </td>
            </tr>
    </HeaderTemplate>
    <ItemTemplate>
        <tr>
            <td>
                <asp:TextBox ID="TxtName" runat="server" Text='<%# Eval("Name")%>'></asp:TextBox>
            </td>
        </tr>
    </ItemTemplate>
    <FooterTemplate>
        </table>
    </FooterTemplate>
</asp:Repeater> 
Afshin Gh
  • 7,918
  • 2
  • 26
  • 43
  • I basically did this, but slightly differently. I needed a hidden field with my text boxes, and I'm using C#. Thanks for the help! – Brian Koser Apr 09 '11 at 04:22
0

If you are using Asp.Net MVC you can reference this article. Model Bind To Collection

If its webforms you can always access each of the inputs submitted via the Request.Form collection.

0

Here is what I ended up doing for each question type, based on links including this one.

foreach (RepeaterItem item in myRptr.Items)
        {
            if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
            {
                OracleCommand cmd = new OracleCommand();
                cmd.Connection = conn;
                cmd.CommandText = "myPackage.myProcedure";
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Parameters.Add("user_id", OracleType.VarChar).Value = Session["UserId"].ToString();
                cmd.Parameters.Add("question_id", OracleType.Number).Value = ((HiddenField)item.FindControl("myHidden")).Value;
                cmd.Parameters.Add("answer", OracleType.VarChar).Value = ((TextBox)item.FindControl("myTxt")).Text;
                cmd.ExecuteNonQuery();
            }
        }
Community
  • 1
  • 1
Brian Koser
  • 439
  • 1
  • 12
  • 28
  • This is exactly what i was talking about. you can use any control that you need (HiddenField, TextBox, DropDown, ...) in your grid and get it's value by looping through repeater items. – Afshin Gh Apr 09 '11 at 09:06