代码如下:
1 private TextBox Txt; 2 3 protected void Page_Load(object sender, EventArgs e) 4 5 { 6 7 Create(); 8 9 } 10 11 private void Create() 12 13 { 14 15 Button Btn = new Button(); 16 17 Btn.ID = "MyBtn"; 18 19 Btn.Text = "显示"; 20 21 Btn.CommandArgument = "MyBtn"; 22 23 Btn.Command += new CommandEventHandler(this.MyBtn_Command); 24 25 this.Txt = new TextBox(); 26 27 this.Txt.ID = "MyTxt"; 28 29 this.FindControl("form1").Controls.Add(Btn); 30 31 this.FindControl("form1").Controls.Add(Txt); 32 33 //这里还可以写成:Page.Form.Controls.Add(Txt); 34 35 } 36 37 public void MyBtn_Command(object sender, CommandEventArgs e) 38 39 { 40 41 if (e.CommandArgument.ToString() == "MyBtn") 42 43 Response.Write(Txt.Text); 44 }
|
以上就是动态创建和使用空间的示例,希望对你有所帮助。