在傳統的ASP循環THROU一個未知inputfields當我可以這樣做:循環THROU文本框的隨機量和得到的值
<input id="textbox1" type="text">
<input id="textbox2" type="text">
<input id="textbox3" type="text">
<input id="textbox4" type="text">
<input id="textbox5" type="text">
For i = 1 To 5
strTextbox = request.form("textbox" & i)
If strTextbox <> "" Then
// Do the magic!
End If
Next
利用該用戶可輸入值的文本框1,3,4和5或也許只有1和2,我可以在For循環中收集值輸入。
我怎麼能在C#中做到這一點?
我不能這樣做,因爲它的劑量就像我在中間添加一個我om textbox.Text;
for (int i = 1; i < 6; i++)
{
strTextbox = textbox[i].Text;
if (!string.IsNullOrEmpty(strTextbox)
{
// Do the magic!
}
}
我現在有很多如果:檢查循環內的每個文本框,但它必須是一個easyer方式?
也許在if子句中加入'&& txt.Text!= String.Empty'。雖然這當然可以作爲OP的練習... – user1429080
@ user1429080:完成 –
好吧,看起來像它會工作。我試圖實現這一點,但我無法從文本框中獲取值,我想這是由於我嵌套的主頁!..?! 我使用asp:textboxes並試圖使用像這樣的findcontrol: TextBox txt = this.Master.Master.FindControl(strTextbox)as TextBox; txt apears在調試時甚至包含文本。我也試着用這個:TextBox txt = this。FindControl(strTextbox)作爲TextBox;並finaly也試過這個:strTextbox =「MainContent_MainContent_textbox」+ i.ToString();沒有運氣。 :( – Slint