.aspx代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="创建文本框.aspx.cs" Inherits="创建文本框" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<p>请输入要创建的文本框数:
<asp:TextBox ID="txt1" runat="server"></asp:TextBox>
<asp:Button ID="btn1"
runat="server" Text="生成文本框" οnclick="btn1_Click" /></p>
</div>
<asp:Panel ID="Panel1" runat="server">
</asp:Panel>
</form>
</body>
</html>
.aspx.cs代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class 创建文本框 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn1_Click(object sender, EventArgs e)
{
int i = 0;
try
{
i = Int16.Parse(txt1.Text);
}
catch { };
TextBox txt;
for (int _i = 0; _i < i; _i++) {
txt = new TextBox();
Panel1.Controls.Add(txt);
txt.Text = (_i+1).ToString();
}
}
}
运行效果如图所示: