日期:2014-05-20 浏览次数:21062 次
public partial class Form1 : Form
{
TextBox _mtextbox = null;
public Form1()
{
InitializeComponent();
button1.Text = "点击创建控件";
}
private void button1_Click(object sender, EventArgs e)
{
if (_mtextbox == null)
{
_mtextbox = new TextBox();
this._mtextbox.Location = new System.Drawing.Point(150, 149);
this._mtextbox.Name = "textBox1";
this._mtextbox.Size = new System.Drawing.Size(100, 21);
this._mtextbox.TabIndex = 1;
this.Controls.Add(_mtextbox);
button1.Text = "在输入框中输入信息以后点击";
}
else
MessageBox.Show(this._mtextbox.Text);
}
}
------解决方案--------------------
protected void Page_Load(object sender, EventArgs e)
{
if (ViewState["isok"] != null)
{
CreateTextBox();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
CreateTextBox();
}
private void CreateTextBox()
{
TextBox tb = new TextBox();
tb.ID = "TextBox1";
tb.AutoPostBack = true;
tb.TextChanged += new EventHandler(tb_TextChanged);
this.form1.Controls.Add(tb);
ViewState["isok"] = true;
}
void tb_TextChanged(object sender, EventArgs e)
{
Response.Write((sender as TextBox).Text);
}