本文介绍了不触发动态创建的按钮单击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好
iam在我的要求中动态创建按钮,并在Web应用程序上工作,当我单击按钮时,它基于按钮单击事件而工作.
IAM创建一个动态按钮,但单击和命令事件未触发
这是我的代码...
hi all
iam working on web application in my requirement creating dynamically button and when i click an button it''s worked based on button click event.
Iam creating a dynamically button but click and command event not firing
here my code is............
public partial class Default11 : System.Web.UI.Page
{
private Button BtnServices;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
}
protected void Button1_Click(object sender, EventArgs e)
{
BtnServices = new Button();
BtnServices.ID = "BtnServices";
BtnServices.Text = "Click Me";
BtnServices.Click += new EventHandler(BtnServices_Click);
BtnServices.Command += new CommandEventHandler(BtnServices_Command);
form1.Controls.Add(BtnServices);
}
void BtnServices_Click(object sender, EventArgs e)
{
Response.Write("hi");
}
void BtnServices_Command(object sender, CommandEventArgs e)
{
Response.Write("hi");
}
}
点击事件和命令事件不会触发.
让我知道我做错了吗?
在此先感谢.........
here click event and command event not firing.
let me know were i went wrong?
thanks in advance.........
推荐答案
private Button BtnServices;
protected void Page_Load(object sender, EventArgs e)
{
if (ViewState["falg"] != null)
{
Create();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Create();
ViewState.Add("falg", true);
}
void BtnServices_Click(object sender, EventArgs e)
{
Response.Write("hi");
}
void BtnServices_Command(object sender, CommandEventArgs e)
{
Response.Write("hi");
}
void Create()
{
BtnServices = new Button();
BtnServices.ID = "BtnServices";
BtnServices.Text = "Click Me";
BtnServices.Click += new EventHandler(BtnServices_Click);
BtnServices.Command += new CommandEventHandler(BtnServices_Command);
form1.Controls.Add(BtnServices);
}
最好的问候
M.Mitwalli
Best Regards
M.Mitwalli
这篇关于不触发动态创建的按钮单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!