简体   繁体   English

ASP.NET控件事件处理程序未在回发时触发?

[英]ASP.NET control event handler not firing on postback?

I have a control which has an ImageButton which is tied to an OnClick event... Upon clicking this control, a postback is performed and the event handler is not called. 我有一个控件,该控件的ImageButton与OnClick事件相关联...单击此控件后,将执行回发,并且不会调用事件处理程序。 AutoEventWireup is set to true, and I've double checked spelling etc.... We haven't touched this control in over a year and it has been working fine until a couple of weeks ago. AutoEventWireup设置为true,并且我已经仔细检查了拼写等。...一年多来我们一直没有碰过此控件,直到几周前它一直工作良好。

We have made changes to controls which load this control... so I'm wondering, what kind of changes could we have made to stop this event handler from being called? 我们已经对加载该控件的控件进行了更改...所以我想知道,我们可以进行哪些更改以阻止该事件处理程序被调用? There is quite a bit of Javascript going on, so this could be the culprit too... 有大量的Java语言在进行,因此这也可能是罪魁祸首...

Edit: Some clarification... we are dynamically loading the parent control of the ImageButton in the OnLoad event of the page... if that makes sense. 编辑:澄清一下...我们正在页面的OnLoad事件中动态加载ImageButton的父控件。

AutoEventWireup is irrelevant. AutoEventWireup无关紧要。 Is your ImageButton loaded dynamically, ie not written out in mark up? 您的ImageButton是否动态加载,即没有写成标记? If it is loaded onto the page late in the Page lifecycle eg in PreRender then the event will not fire. 如果在页面生命周期的后期(例如在PreRender中)将其加载到页面上,则不会触发该事件。

If there is a JavaScript issue your page will not even PostBack. 如果出现JavaScript问题,则您的页面甚至都不会回发。 Is that happening? 那是真的吗

您是否给ImageButton提供了ID?

I def agree with what BritishDeveloper said. 我不同意BritishDeveloper所说的话。 I had a similar problem where I was dynamically loading controls, but I couldn't get a reference to the control using Page.FindControl("controlName") Someone pointed out that I needed to keep the page lifecycle in mind. 我在动态加载控件时遇到了类似的问题,但是我无法使用Page.FindControl("controlName")引用该控件,有人指出我需要牢记页面生命周期。 I found out I needed to make sure to load the control in the PageInit because after doing an async postback the control was still there, but not loaded in the postback so there was no way to find it. 我发现我需要确保将控件加载到PageInit中,因为在执行异步回发之后,该控件仍然存在,但未加载回发中,因此无法找到它。 This was all in csharp codebehind and ajax though, but I'm guessing the control isn't getting reloaded. 虽然这些都在csharp codebehind和ajax中,但是我猜想控件没有被重新加载。

I can go into a little more detail ... I just lost several hours fixing my own issue similar to the issue described here. 我可以再详细一点...我只花了几个小时来修复自己的问题,类似于此处描述的问题。 In the course of creating some search controls, I added a pair of ImageButtons and set the PostbackUrl properties on them. 在创建一些搜索控件的过程中,我添加了一对ImageButton,并在它们上设置了PostbackUrl属性。 A few days later while testing new code, I noticed that none of my other buttons on the webform were posting back properly. 几天后,在测试新代码时,我注意到该网络表单上的其他按钮均未正确发布。 They would fire a postback, but the code-behind was behaving as if every postback was an initial page request and none of the event handlers were firing. 他们会触发回发,但是后面的代码的行为似乎是每个回发都是初始页面请求,并且没有事件处理程序在触发。

After several hours of troubleshooting, I came across this post. 经过几个小时的故障排除,我发现了这篇文章。 When I went back and removed the PostbackUrl from those two ImageButtons, everything went back to normal. 当我返回并从这两个ImageButton中删除PostbackUrl时,一切恢复正常。 I don't know why it was causing this issue, but the fix mentioned here worked for me. 我不知道为什么会导致此问题,但是这里提到的修复程序对我有用。 I want to add that my ImageButtons were not dynamically added ... they were in the markup and this issue still cropped up. 我想补充一点,我的ImageButtons不是动态添加的……它们在标记中,并且这个问题仍然出现。 Search your markup for any controls with PostbackUrl set ... remove that (program around it if needed) ... see if your event handlers will fire properly. 在您的标记中搜索设置了PostbackUrl的任何控件...删除该控件(如果需要,在其周围的程序)...查看事件处理程序是否将正确触发。

因此,事实证明,我们在控件A中的一个按钮上设置了PostbackUrl属性...这导致控件B的事件处理程序在按下控件B中的按钮时不会触发。

If you create a control dynamically. 如果动态创建控件。 Any time you fire a postback using the new created control, you need to recreated it. 每当您使用新创建的控件触发回发时,都需要重新创建它。 Just think that your application are running at a server. 只需认为您的应用程序正在服务器上运行即可。 How can the server hold information on controls created dynamically? 服务器如何保存有关动态创建的控件的信息? Don't use Page.IsPostBack to create postback. 不要使用Page.IsPostBack创建回发。 PostbackUrl is bad solution. PostbackUrl是错误的解决方案。 Workarround will be need. 需要Workarround。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM