[英]Multiple labels/textboxes based on what the user entered
我正在asp.net和C#中創建一個Web窗體。
該表單詢問用戶該用戶有多少個孩子,然后在單擊繼續按鈕后,應用程序將提示輸入每個孩子的名字。
我知道使用控制台應用程序,代碼將是:
for (int i = 0 ; i < noOfChildren ; i++)
{
Console.Write("Child's Name: ");
name = Console.ReadLine();
}
我將如何編碼asp.net/C#應用程序以執行相同的操作,但僅以用戶輸入的相同次數顯示標簽和文本框...?
提前致謝!
在下面的示例中,它僅動態創建Textbox
但您可以根據需要動態添加樣式和標簽控件。
在Aspx
頁面中,在html主體中創建form
標簽。
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
No of Text boxes
</td>
<td>
<asp:TextBox ID="txtNumbers" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Submit" />
</td>
</table>
<br />
</div>
在您的C#文件后面的代碼中,
protected void btnSubmit_Click(object sender, EventArgs e)
{
int noofcontrols = Convert.ToInt32(txtNumbers.Text);
for (int i = 1; i <= noofcontrols; i++)
{
TextBox NewTextBox = new TextBox();
NewTextBox.ID = "TextBox" + i.ToString();
NewTextBox.Style["Clear"] = "Both";
NewTextBox.Style["Float"] = "Left";
NewTextBox.Style["Top"] = "25px";
NewTextBox.Style["Left"] = "100px";
//form1 is a form in my .aspx file with runat=server attribute
form1.Controls.Add(NewTextBox);
}
}
我想這就是你想要的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.