ASP.NET动态绑定控件并绑定其对应的点击事件 把创建控件的代码放到 if (!Page.IsPostBack) 之外,每次加载 页面都要创建 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://www.cnblogs.com/chenxizhang/archive/2009/05/19/1460544.html 而你的代码要把if (!Page.IsPostBack) 去掉,就ok了。原因是页面回传的时候,button客户端生成的是一个submit,提交到后台之后,不执行任何动作,所以弹不出提示。而去掉的话,就会注册执行事件。 这样,你设置一个断点,按F10一步一步的执行看看究竟执行了哪些方法。尤其是你加!Page.IsPostBack跟不加的情况,就知道执行的结果了。 bt.Click += new EventHandler(bt1_Click); 这种绑定事件要放在!Page.IsPostBack外面 public partial class _Default : System.Web.UI.Page{ protected System.Web.UI.WebControls.Button bt; protected System.Web.UI.WebControls.Button bt1; protected System.Web.UI.WebControls.CheckBox cb; protected void Page_Load(object sender, EventArgs e) { //if (!Page.IsPostBack) //{ cb = new CheckBox(); cb.Text = "xingming"; cb.ID = "cb1"; this.Panel1.Controls.Add(cb); bt = new Button(); bt.Text = "确定"; bt.ID = "bt1"; bt.Click += new EventHandler(bt1_Click); this.Panel1.Controls.Add(bt); //} } protected void bt1_Click(object sender, EventArgs e) { //Response.Write("<script>alert('123')</script>"); bt1 = new Button(); bt1.Text = "确定"; bt1.ID = "bt1"; bt1.Click += new EventHandler(bt2_Click); this.Panel1.Controls.Add(bt1); } protected void bt2_Click(object sender, EventArgs e) { Response.Write("<script>alert('123')</script>"); }}真麻烦你了,我现在稍微改了下代码,点击bt 能触发bt1_Click方法,为何点击bt1却不能调用bt2_Click方法呢? 弄错了, bt1_Click中 bt1.ID = "bt1";应该为 bt1.ID = "bt2";但是为什么点击bt1 这个按钮就消失了呢? 页面回发了,又重新执行了page_load事件,你里面的代码不就是一个checkbox一个button吗 学习一下这个吧。http://blog.csdn.net/wonsoft/article/details/4351406 public partial class _Default : System.Web.UI.Page{ protected System.Web.UI.WebControls.Button bt; protected System.Web.UI.WebControls.Button bt1; protected System.Web.UI.WebControls.CheckBox cb; protected void Page_Load(object sender, EventArgs e) { //if (!Page.IsPostBack) //{ cb = new CheckBox(); cb.Text = "xingming"; cb.ID = "cb1"; this.Panel1.Controls.Add(cb); bt = new Button(); bt.Text = "确定"; bt.ID = "bt1"; bt.Click += new EventHandler(bt1_Click); this.Panel1.Controls.Add(bt); if (Session["asd"] != null) { this.Panel1.Controls.Add((Button)Session["asd"]); } //} } protected void bt1_Click(object sender, EventArgs e) { //Response.Write("<script>alert('123')</script>"); bt1 = new Button(); bt1.Text = "确定"; bt1.ID = "bt2"; bt1.Click += new EventHandler(bt2_Click); this.Panel1.Controls.Add(bt1); Session["asd"] = bt1; } protected void bt2_Click(object sender, EventArgs e) { Response.Write("<script>alert('123')</script>"); }}bt2_Click这个方法不会调用,不知道为什么,哎!悲剧啊。 like this:http://www.cnblogs.com/insus/archive/2011/11/18/2253605.html 文件上传到远程服务器的问题???? 求助!世界城市数据库 <%# 请大家介绍使用VS+ASP.NET制作网页的技巧和心得 比如在页面制作 还有各方面良好的习惯 谢谢 %> 是VS2005 Login控件只能用于SQL Server,还是我代码写错了? 上传文件提示问题 关于窗体验证的奇怪问题 一个下午没解决了 急!关于.NET安装的问题!...在线等.. 网站老是出现 server is too busy 有关事务 我想在程序里面产生templateColunm,怎么写? 网站域名经常无法访问,望高手支招,急急急 asp.net程序中执行PowerShell
这种绑定事件要放在!Page.IsPostBack外面
{
protected System.Web.UI.WebControls.Button bt;
protected System.Web.UI.WebControls.Button bt1;
protected System.Web.UI.WebControls.CheckBox cb; protected void Page_Load(object sender, EventArgs e)
{
//if (!Page.IsPostBack)
//{
cb = new CheckBox();
cb.Text = "xingming";
cb.ID = "cb1";
this.Panel1.Controls.Add(cb);
bt = new Button();
bt.Text = "确定";
bt.ID = "bt1";
bt.Click += new EventHandler(bt1_Click);
this.Panel1.Controls.Add(bt);
//}
} protected void bt1_Click(object sender, EventArgs e)
{
//Response.Write("<script>alert('123')</script>");
bt1 = new Button();
bt1.Text = "确定";
bt1.ID = "bt1";
bt1.Click += new EventHandler(bt2_Click);
this.Panel1.Controls.Add(bt1);
} protected void bt2_Click(object sender, EventArgs e)
{
Response.Write("<script>alert('123')</script>");
}}
真麻烦你了,我现在稍微改了下代码,点击bt 能触发bt1_Click方法,为何点击bt1却不能调用bt2_Click方法呢?
但是为什么点击bt1 这个按钮就消失了呢?
{
protected System.Web.UI.WebControls.Button bt;
protected System.Web.UI.WebControls.Button bt1;
protected System.Web.UI.WebControls.CheckBox cb; protected void Page_Load(object sender, EventArgs e)
{
//if (!Page.IsPostBack)
//{
cb = new CheckBox();
cb.Text = "xingming";
cb.ID = "cb1";
this.Panel1.Controls.Add(cb);
bt = new Button();
bt.Text = "确定";
bt.ID = "bt1";
bt.Click += new EventHandler(bt1_Click);
this.Panel1.Controls.Add(bt);
if (Session["asd"] != null)
{
this.Panel1.Controls.Add((Button)Session["asd"]);
}
//}
} protected void bt1_Click(object sender, EventArgs e)
{
//Response.Write("<script>alert('123')</script>");
bt1 = new Button();
bt1.Text = "确定";
bt1.ID = "bt2";
bt1.Click += new EventHandler(bt2_Click);
this.Panel1.Controls.Add(bt1);
Session["asd"] = bt1;
} protected void bt2_Click(object sender, EventArgs e)
{
Response.Write("<script>alert('123')</script>");
}}
bt2_Click这个方法不会调用,不知道为什么,哎!悲剧啊。
http://www.cnblogs.com/insus/archive/2011/11/18/2253605.html