asp.netasp.net-webcontrol

using NameValueCollection how can I just get textbox contrl values?


How do I collect the values of JUST the text box controls?

Is there a way to specify this in ASP.NET 4.0?

ArrayList alAnswerList = new ArrayList();

int iCount = 0;
NameValueCollection frmCollect = Request.Form;

iCount = frmCollect.Count;

for (int i = 0; i <= iCount; i++)
{
    alAnswerList.Add(frmCollect.GetValues(i));
}

I am trying to avoid hardcoding an exact index value to start getting values at.

Thanks!

Update: Control Code

<div id="Layer1" runat="server" style="overflow-x: hidden; overflow-y: hidden; padding: 0px 50px 0 50px">
    <div id="buttonWrapper">
        <asp:Label runat="server" ID="lblStartHunting">text</asp:Label>
        <asp:Button runat="server" ID="btnStart" Text="Start" OnClick="btnStart_Click" BorderColor="WhiteSmoke" />
        <br />
        <br />
        <br style="clear: both;" />
        <asp:Label runat="server" ID="lblName">Name / Team Name to be added:</asp:Label>
        <asp:TextBox runat="server" ID="txtName" Width="280px" BorderColor="WhiteSmoke"></asp:TextBox>
     </div>
    <div id="huntHeaderWrapper"><div id="huntHeaderPoints"><strong>Points</strong></div><div id="huntHeaderQuestion"><strong>Question</strong></div><div id="huntHeaderAnswer"><strong>Answer</strong></div></div>
    <asp:DataList ID="dlHunt" runat="server" Width="740px" DataSourceID="SqlDataSource1">
        <ItemTemplate>
            <asp:Label ID="idLabel" runat="server" Text='<%# Eval("id") %>' Visible="false" />
            <div id="huntDivPoints">
                <asp:Label ID="pointsLabel" runat="server" Text='<%# Eval("points") %>' />
            </div>
            <div id="huntDivQuestion">
                <asp:Label ID="questionLabel" runat="server" Text='<%# Eval("question") %>' />
            </div>
            <div id="huntDivAnswer">
                <asp:TextBox ClientIDMode="Static" ID="huntAnswerText" runat="server" Width="240px" BorderColor="WhiteSmoke"></asp:TextBox><br />
                <span style="color: #395069;">Hint: <asp:Label ID="hintLabel" runat="server" Text='<%# Eval("hint") %>' /></span>
            </div>
        </ItemTemplate>
        <ItemStyle BackColor="White" BorderColor="#5885a3" BorderWidth="1" BorderStyle="Solid" />
    </asp:DataList>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Connection_private %>" SelectCommand="SELECT [id], [question], [hint], [answer], [points] FROM [table]"></asp:SqlDataSource>

    <asp:Button ID="btnShowAnswers" runat="server" Text="Submit Answers" BorderColor="WhiteSmoke" OnClick="btnShowAnswers_Click" /> <asp:Label runat="server" ID="lblSubmit"> &nbsp;&nbsp;text</asp:Label>
</div>

Solution

  • Inside btnShowAnswers_Click, add the following code:

    var answers = new List<string>();
    
    if(dlHunt.Items.Count > 0)
    {
        foreach(DataListItem item in dlHunt.Items)
        {
            var textBox = (TextBox)item.FindControl("huntAnswerText");
            answers.Add(textBox.Text);         
        }
    }
    
    // At this point you have your list of answers and can handle as you see fit