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 (续)弹出新窗口,关闭父窗口 用reques.QueryString传值问题 ASP.NET中怎么处理异常 一个有点难度的排序问题 没有分啦~好心人帮忙看看吧 关于在线编辑器FCKeditor2的问题 rss是怎么实现的? .net操作Exchange的问题 关于连接数据库语句中需要语句结束的错误 各位大虾那位碰到过如此的问题??80分送上 网站域名经常无法访问,望高手支招,急急急 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