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.

3 Answers 3

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> 
Sign up to request clarification or add additional context in comments.

1 Comment

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

Comments

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

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.