页面代码:
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Panel ID="Panel1" runat="server">
</asp:Panel>
</div>
</form>
</body>
后台代码:
protected void Page_Load(object sender, EventArgs e)
{
TextBox t = new TextBox();
t.ID = "代码创建的文本框";
t.AutoPostBack = true;
t.TextChanged += this.TextBox1_TextChanged;
Panel1.Controls.Add(t);
CheckBox c = new CheckBox();
c.ID = "c1";
c.Text = "代码创建的复选框";
c.Checked = false;
c.CheckedChanged += this.CheckBox1_CheckedChanged;
Panel1.Controls.Add(c);
Button bt = new Button();
bt.ID = "bt1";
bt.Text = "按钮1" ;
bt.Command += new CommandEventHandler(this.OnButton);
Panel1.Controls.Add(bt);
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
TextBox t = sender as TextBox;
Label1.Text = t.ID + "改变了" + DateTime.Now.ToString();
}
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
CheckBox c = sender as CheckBox;
Label1.Text = c.ID + "改变了" + DateTime.Now.ToString();
}
public void OnButton(Object Sender, CommandEventArgs e)
{
Button but1 = Sender as Button;
but1.Text = "一点就" + but1.Text;
}