asp.net 动态添加控件不触发响应事件 10
在一个webpage中点击第一个按钮创建第二个按钮,可是点击第二按钮的时候不仅不触发它的响应时间,第二个按钮本身也会消失。求解释。代码如下:test.aspx:<%@Pa...
在一个webpage中点击第一个按钮创建第二个按钮,可是点击第二按钮的时候不仅不触发它的响应时间,第二个按钮本身也会消失。求解释。代码如下:
test.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="WebApplication.test" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title></head><body> <form id="form1" runat="server"> <div> <asp:Panel ID="panel1" runat="server"></asp:Panel> </div> <div> <asp:Panel ID="panel2" runat="server"></asp:Panel> </div> </form></body></html>
test.aspx.cs:
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls; namespace WebApplication{ public partial class test : System.Web.UI.Page { int i = 1; protected void Page_Load(object sender, EventArgs e) { LinkButton btn1 = new LinkButton(); btn1.Text = "1"; btn1.Click += lb_Click1; panel1.Controls.Add(btn1); } void lb_Click1(object sender, EventArgs e) { LinkButton btn2 = new LinkButton(); btn2.Text = "2"; btn2.Click += new EventHandler(lb_Click2); panel2.Controls.Add(btn2); } void lb_Click2(object sender, EventArgs e) { Response.Write("<script>alert('Btn2 clicked')</script>"); } }} 展开
test.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="WebApplication.test" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title></head><body> <form id="form1" runat="server"> <div> <asp:Panel ID="panel1" runat="server"></asp:Panel> </div> <div> <asp:Panel ID="panel2" runat="server"></asp:Panel> </div> </form></body></html>
test.aspx.cs:
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls; namespace WebApplication{ public partial class test : System.Web.UI.Page { int i = 1; protected void Page_Load(object sender, EventArgs e) { LinkButton btn1 = new LinkButton(); btn1.Text = "1"; btn1.Click += lb_Click1; panel1.Controls.Add(btn1); } void lb_Click1(object sender, EventArgs e) { LinkButton btn2 = new LinkButton(); btn2.Text = "2"; btn2.Click += new EventHandler(lb_Click2); panel2.Controls.Add(btn2); } void lb_Click2(object sender, EventArgs e) { Response.Write("<script>alert('Btn2 clicked')</script>"); } }} 展开
展开全部
你要知道服务器控件的button点击之后是先刷新页面再执行事件代码的,所以你现在就是第一次显示了btn1然后点击btn1之后生成了btn2再点击btn2时候页面就先刷新了又一次执行了pageload事件所以btn2的事件没有执行。
那么你可以换种方法来做
LinkButton btn2 = new LinkButton();
protected void Page_Load(object sender, EventArgs e)
{
LinkButton btn1 = new LinkButton();
btn1.Text = "111111111111111111111111";
btn1.Click += lb_Click1;
panel1.Controls.Add(btn1);
btn2.Text = "22222222222222222222222222";
btn2.Click += new EventHandler(lb_Click2);
btn2.Visible = false;
panel2.Controls.Add(btn2);
}
void lb_Click1(object sender, EventArgs e)
{
btn2.Visible = true;
}
void lb_Click2(object sender, EventArgs e)
{
Response.Write("<script>alert('Btn2 clicked')</script>");
}
再就是一般这种操作最好别从后台往前台输出控件,js来做还是很方便的
展开全部
回发的问题,楼主记得加上isPostBack的判断
if(!isPostBack)
{
...(your dynamic control`s declaration)
}
关于postback的概念
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询