asp.net 调用我创建的按钮单击事件
我正在 asp.net 中编写一个网络应用程序。 我有一个 aspx 页面,它调用一个生成按钮并返回该按钮的类(测试)。 类构造函数获取按钮单击事件应激活的函数(userClickOnButton) 和 插入到button.click += EventHandler("函数名称(userClickOnButton)");
问题是,在aspx后面的代码中,我使用IsPostBack(无法将其关闭,需要此条件),当我单击按钮时,程序不会转到我为按钮创建的事件,但是当我关闭IsPostBack时条件是程序确实转到我创建的事件(函数 userClickOnButton)。
我的代码是: 后面的 aspx 代码
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Test test = new Test(userClickOnButton);
Button button = test.AddButton();
cell.Controls.Add(button);
}
}
private void userClickOnButton(object sender, EventArgs e)
{
this.ModalPopupExtenderSelectFilds.Show();
}
我的类
public class Test
{
Action<object, EventArgs> m_ButtonClickActivateFunction;
public Test(Action<object, EventArgs> func)
{
m_ButtonClickActivateFunction = func;
}
public Button AddButton()
{
Button button = new Button();
button.Text = "select";
button.ID = "buttonID";
button.Click += new EventHandler(m_ButtonClickActivateFunction);
return button;
}
需要帮助来激活事件,而不需要取出 IsPostBack 条件,
谢谢
I am writing a web app in asp.net.
I have an aspx page that call a class (Test) that Generates a button and return the button.
The class constructor get the function that the button click event should activate (userClickOnButton)
and
insert to the button.click += EventHandler("the name of the function (userClickOnButton)");
the problem is that in the aspx behind code i use IsPostBack (cant take it off , need this Condition) and when i click on the button the progrem does not go to the event i created for the button, but when i take off the IsPostBack Condition the program does go to the event i created (the function userClickOnButton).
my code is:
aspx code behind
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Test test = new Test(userClickOnButton);
Button button = test.AddButton();
cell.Controls.Add(button);
}
}
private void userClickOnButton(object sender, EventArgs e)
{
this.ModalPopupExtenderSelectFilds.Show();
}
my class
public class Test
{
Action<object, EventArgs> m_ButtonClickActivateFunction;
public Test(Action<object, EventArgs> func)
{
m_ButtonClickActivateFunction = func;
}
public Button AddButton()
{
Button button = new Button();
button.Text = "select";
button.ID = "buttonID";
button.Click += new EventHandler(m_ButtonClickActivateFunction);
return button;
}
need help to activate the event without taking out the IsPostBack Condition
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为 asp.net 不处理对不再存在的按钮的点击。由于在回发时您不会重新创建按钮,因此该按钮不再存在于页面的控件集合中,因此不会处理该事件。
该事件的处理发生在
Page_Load
之后,因此Page_Load
应该重新创建了该按钮。也看看这里:Asp.Net 页面生命周期
I think asp.net doesn't handle clicks on buttons that no longer exist. Since on postback you don't recreate the button and the button therefore does no longer exist in the controls collection of the page, the event is not handled.
Handling of the event happens after
Page_Load
, soPage_Load
should have recreated the button.Have a look here as well: Asp.Net Page Life Cycle