如何触发按钮事件的单击事件,该事件是动态添加到asp.net中的stringbuilder的? [英] how to fire click event for button control which is dymaically added to stringbuilder in asp.net?
本文介绍了如何触发按钮事件的单击事件,该事件是动态添加到asp.net中的stringbuilder的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
void exit(object sender,EventArgs e)
{
Response.Write("exit");
}
protected void Button1_Click(object sender, EventArgs e)
{
Button b = new Button();
b.Click += new EventHandler(exit);
b.Text = "click";
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
HtmlTextWriter ht = new HtmlTextWriter(sw);
b.RenderControl(ht);
Response.Write("<br>" + sb);
}
}
在这里,我无法调用按钮的单击事件,如何实现此
here i am unable call click event of button,how to achieve this
推荐答案
您不能在另一个控件的事件处理程序中动态添加控件,在Page_Load中添加任何动态控件.
要了解引发的事件以及以什么顺序发生的事件,请查看此 [ ^ ].
因此,这意味着您需要在Page_Load中添加控件,并使用Button1的事件处理程序使第二个按钮可见.
像这样;
You can''t dynamically add controls in the event handler of another control, you have to add any dynamic controls in Page_Load.
To understand the events that fire and in which order have a look at this[^].
So, what this means is you need to add the control in the Page_Load and use the event handler for Button1 to make the second button visible.
Like so;
public partial class _Default : System.Web.UI.Page
{
private Button b = new Button();
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == true)
{
b.Click += new EventHandler(b_Click);
b.Text = "click";
b.ID = "button2";
b.Visible = false;
Page.Form.Controls.Add(b);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
b.Visible = true;
}
void b_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Write("exit");
Response.Flush();
Response.End();
}
}
这篇关于如何触发按钮事件的单击事件,该事件是动态添加到asp.net中的stringbuilder的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文