动态添加ASP.NET按钮点击处理被忽略 [英] Dynamically added ASP.NET button click handler being ignored

查看:196
本文介绍了动态添加ASP.NET按钮点击处理被忽略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我添加事件处理程序,以这样的按钮:

I am adding event handlers to a button like this:

btn.Click += new EventHandler(btn_Click);

然而, btn_Click 函数没有被调用(从未击中断点吧)和按钮只需重新加载页面。在我过去的经验,ASP按钮通常重新加载页面之前执行点击code,让我怎么得到那个时动态添加事件发生?

However the btn_Click function is not being called (never hits the breakpoint in it) and the button just reloads the page. In my past experience, asp buttons usually perform the click code before reloading the page, so how do I get that to happen when the event is dynamically added?

我还设置的CausesValidation =假,虽然有在页面上没有验证,所以我不认为这将有影响呢。

I also set CausesValidation = false, although there's no validation on the page so I don't think that would have influence anyway.

推荐答案

事件处理程序需要绑定为每个请求无论网页是否被调回。的事件处理程序的结合在每个页面请求的开始都将丢失。对于按钮的事件处理程序通常绑定在Page_Load中。

The event handler needs to be bound for every request regardless of whether or not the page is being posted back. The binding of the event handler is lost at the start of each page request. Event handlers for buttons are typically bound in Page_Load.

这篇关于动态添加ASP.NET按钮点击处理被忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆