将新事件添加到按钮(add new event to button)

编程入门 行业动态 更新时间:2024-10-22 21:25:31
将新事件添加到按钮(add new event to button) protected void Page_Load(object sender, EventArgs e) { Button a = new Button(); a.Width = 100; a.Height = 100; a.Text = "one"; a.Click += new EventHandler(test); form1.Controls.Add(a); } protected void test(object sender, EventArgs e) { Button b = new Button(); b.Width = 100; b.Height = 100; b.Text = "two"; b.Click += new EventHandler(test2); form1.Controls.Add(b ); Response.Write("aaaaaaaaaaaaaaaaaa"); } protected void test2(object sender, EventArgs e) { Response.Write("bbbbbbbbbbbbbbb"); }

当点击one显示按钮two但是当点击two按钮时不运行test2 ???? 点击two页刷新时。

我想首先点击'one',然后'two'只在点击按钮a后显示按钮

protected void Page_Load(object sender, EventArgs e) { Button a = new Button(); a.Width = 100; a.Height = 100; a.Text = "one"; a.Click += new EventHandler(test); form1.Controls.Add(a); } protected void test(object sender, EventArgs e) { Button b = new Button(); b.Width = 100; b.Height = 100; b.Text = "two"; b.Click += new EventHandler(test2); form1.Controls.Add(b ); Response.Write("aaaaaaaaaaaaaaaaaa"); } protected void test2(object sender, EventArgs e) { Response.Write("bbbbbbbbbbbbbbb"); }

when click on one show Button two but when click on twobutton does not run test2???? when click on two page refresh.

i want first click on 'one' and then 'two' show the button only after button a has been clicked

最满意答案

尝试这个:

protected void Page_Init(object sender, EventArgs e) { Button a = new Button(); a.Width = 100; a.Height = 100; a.Text = "one"; a.Click += new EventHandler(test); form1.Controls.Add(a); Button b = new Button(); b.Visible = false; b.Width = 100; b.Height = 100; b.Text = "two"; b.Click += new EventHandler(test2); form1.Controls.Add(b); } protected void test(object sender, EventArgs e) { b.Visible = true; Response.Write("aaaaaaaaaaaaaaaaaa"); } protected void test2(object sender, EventArgs e) { Response.Write("bbbbbbbbbbbbbbb"); }

Try this:

protected void Page_Init(object sender, EventArgs e) { Button a = new Button(); a.Width = 100; a.Height = 100; a.Text = "one"; a.Click += new EventHandler(test); form1.Controls.Add(a); Button b = new Button(); b.Visible = false; b.Width = 100; b.Height = 100; b.Text = "two"; b.Click += new EventHandler(test2); form1.Controls.Add(b); } protected void test(object sender, EventArgs e) { b.Visible = true; Response.Write("aaaaaaaaaaaaaaaaaa"); } protected void test2(object sender, EventArgs e) { Response.Write("bbbbbbbbbbbbbbb"); }

更多推荐

本文发布于:2023-08-01 04:11:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1352837.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:按钮   事件   add   event   button

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!