如何收集JUST文本框控件的值?
有没有办法在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));
}
我试图避免硬编码确切的索引值以开始获取值。
谢谢!
更新:控制代码
<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"> text</asp:Label>
</div>
答案 0 :(得分:1)
试试这个:
// You might not be able access the controls directly here.
//As it might be on a master page,or Simple Page or a
//Content Page or an User Control. Either you have to use FindControl()
//or NamingContainer
foreach(Control c in Page.Controls)
{
if(c is TextBox)
{
var val = ((TextBox)c).Text; // or any other properties of textbox
}
}
答案 1 :(得分:1)
在btnShowAnswers_Click
内,添加以下代码:
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