C# 动态创建控件
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 }
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 }
下面是转贴的
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System;
2 using System.Data;
3 using System.Configuration;
4 using System.Collections;
5 using System.Web;
6 using System.Web.Security;
7 using System.Web.UI;
8 using System.Web.UI.WebControls;
9 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11
12 public partial class Test : System.Web.UI.Page
13 {
14 protected void Page_Load(object sender, EventArgs e)
15 {
16 //动态创建文本框控件
17 TextBox txt = new TextBox();
18 txt.ID = "txtTest";
19
20 //加入到WEB窗体中
21 Page.Form.Controls.Add(txt);
22
23 //动态创建按钮控件
24 Button btn = new Button();
25 btn.ID = "btnTest";
26 btn.Text = "提交";
27
28 //为按钮动态添加事件
29 btn.Click += new EventHandler(btnTest_Click);
30
31 //加入到WEB窗体中
32 Page.Form.Controls.Add(btn);
33 }
34 //为动态创建的按钮事件写一个方法
35 protected void btnTest_Click(object sender, EventArgs e)
36 {
37 //获取在WEB窗体中动态创建的文本框控件
38 TextBox txt = new TextBox();
39 txt = Page.Form.FindControl("txtTest") as TextBox;
40
41 //输出在WEB窗体中动态创建的文本框控件的值
42 Response.Write(txt.Text);
43 }
44 }
2 using System.Data;
3 using System.Configuration;
4 using System.Collections;
5 using System.Web;
6 using System.Web.Security;
7 using System.Web.UI;
8 using System.Web.UI.WebControls;
9 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11
12 public partial class Test : System.Web.UI.Page
13 {
14 protected void Page_Load(object sender, EventArgs e)
15 {
16 //动态创建文本框控件
17 TextBox txt = new TextBox();
18 txt.ID = "txtTest";
19
20 //加入到WEB窗体中
21 Page.Form.Controls.Add(txt);
22
23 //动态创建按钮控件
24 Button btn = new Button();
25 btn.ID = "btnTest";
26 btn.Text = "提交";
27
28 //为按钮动态添加事件
29 btn.Click += new EventHandler(btnTest_Click);
30
31 //加入到WEB窗体中
32 Page.Form.Controls.Add(btn);
33 }
34 //为动态创建的按钮事件写一个方法
35 protected void btnTest_Click(object sender, EventArgs e)
36 {
37 //获取在WEB窗体中动态创建的文本框控件
38 TextBox txt = new TextBox();
39 txt = Page.Form.FindControl("txtTest") as TextBox;
40
41 //输出在WEB窗体中动态创建的文本框控件的值
42 Response.Write(txt.Text);
43 }
44 }