在下面的示例中,它仅动态创建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);
}
}
我想这是你想要的。
Reference
你想要所有的文本框在同一页,不是吗? –
是的,我做...相同的页面,相同的格式 – user2625790