简体   繁体   English

按钮点击事件不会在首次点击时触发,但始终会在后续点击时触发

[英]Button click event won't fire on first click, but always fires on subsequent clicks

I have a large-scale asp.net project that I'm working on (new to ASP.NET), and I have a problem with some dynamically added buttons not firing their click events on the first click. 我正在处理一个大型的asp.net项目(对ASP.NET来说是新的),并且我遇到了一些动态添加的按钮的问题,这些按钮在首次单击时不会触发其单击事件。 What's weird is that this doesn't always happen; 奇怪的是,这种情况并不总是发生; sometimes they work just fine on the first click, and other times if I run the project and follow exactly the same steps the button won't work on the first click. 有时它们在第一次单击时就可以正常工作,而在其他情况下,如果我运行该项目并按照完全相同的步骤操作,则按钮在第一次单击时将不起作用。

Here's some background... 这是一些背景...

The page that's problematic is used to deploy different versions of some content to clients. 有问题的页面用于将某些内容的不同版本部署到客户端。 The page has an UpdatePanel that contains a bunch of tables, and only one of them is visible at any given time. 该页面具有包含一堆表的UpdatePanel,并且在任何给定时间仅其中一个表可见。 Each table represents a different "screen" of a wizard-like workflow. 每个表代表类似向导的工作流程的不同“屏幕”。 So when the page is loaded, the first table is visible and all others are not, and the first page has a radio button list with deployment types and a Continue button. 因此,在加载页面时,第一个表可见,而其他所有表都不可见,并且第一个页面具有一个单选按钮列表,其中包含部署类型和一个Continue按钮。 When the Continue button is clicked, some data is loaded for the next "screen" and the first table is made invisible and the next table is made visible. 单击“继续”按钮时,将为下一个“屏幕”加载一些数据,并使第一个表不可见,而使下一个表可见。

So now that you have some background, on to the problem... 现在,您已经对问题有了一些了解...

One of these tables has an empty Accordion on it, and when the previous "screen"'s Continue button is clicked, the Accordion is populated dynamically with data from the DB; 其中一个表上有一个空的手风琴,单击上一个“屏幕”的“继续”按钮时,将使用数据库中的数据动态填充手风琴; there's one pane for each major version and each pane has a table in it with one row for each minor version. 每个主要版本都有一个窗格,每个窗格中都有一个表,每个次要版本都有一行。 Each of these rows has the Version Number, the Deployment Status, and an Action button that lets the user Deploy the version, or if its already deployed they can Redeploy. 这些行中的每一行都有版本号,部署状态和操作按钮,该按钮使用户可以部署版本,或者如果用户已经部署了版本,则可以重新部署。 Anyways, these action buttons are the ones that I have a problem with where they sometimes don't work on the first click. 无论如何,这些操作按钮是我有问题的地方,这些按钮有时在第一次单击时不起作用。 Like I said, the buttons are created in the click event from the Continue button on the previous "screen", and I set the ID of each button, and link the event up to the click event at the time the button is created. 就像我说的那样,这些按钮是在单击事件中从上一个“屏幕”上的“继续”按钮创建的,我设置了每个按钮的ID,并将该事件链接到创建按钮时的单击事件。 I can debug over this and see the event handlers being added with no problem, but then when I click the button the first time (and this is actually not consistent; sometimes they work on the first time!) they don't do anything, but on the second click they work normally. 我可以对此进行调试,并看到没有添加任何事件处理程序,但是,当我第一次单击按钮时(这实际上是不一致的;有时它们是第一次工作!),他们什么也没做,但在第二次单击时,它们可以正常工作。 I have a method called something like populateAccordion() that is responsible for actually loading the data from the DB and creating these records with buttons in them. 我有一个称为populateAccordion()之类的方法,该方法负责从数据库实际加载数据并创建带有按钮的记录。 I call this method on page_load if it's a postback, but only if the cached selections from previous "screens" are not null (ie I cache a subjectId and centerId on previous "screens" and only load the Accordion data if those are not null). 如果是回发,则在page_load上调用此方法,但前提是先前的“屏幕”中缓存的选择不为空(即,我在先前的“屏幕”中缓存subjectId和centerId,并且仅当它们不为null时才加载手风琴数据) 。

This is a little too complicated to post code for, but does anyone have any ideas what could be going wrong, or how I could figure this out? 这太过复杂了,无法发布代码,但是没有人知道什么地方可能出什么问题,或者我怎么知道吗? I've done quite a bit of debugging, and my populateAccordion() is always called properly on the previous screen's Continue button click, and on every page_load after that, and the Click events are always wired up right when the buttons are created in that method, so I can't figure out why the click events wouldn't fire on the first try. 我已经进行了很多调试,并且总是在上一个屏幕的“继续”按钮单击以及随后的每个page_load上正确调用populateAccordion(),并且在其中创建按钮时,Click事件始终会正确连接方法,所以我不知道为什么第一次尝试时不会触发click事件。

The problem is similar to the one mentioned in this post... 问题类似于本文中提到的问题...

https://stackoverflow.com/a/2784140/1246574 https://stackoverflow.com/a/2784140/1246574

But I am definitely setting the IDs for all of my buttons, so the solution mentioned in that post isn't the fix for my problem. 但是我绝对要为所有按钮设置ID,因此该文章中提到的解决方案并不是解决我的问题的方法。

I've also read quite a few other posts that say to move your code from Page_Load to Page_PreRender, but that doesn't change anything in this case; 我还阅读了很多其他文章,这些文章说将您的代码从Page_Load移到Page_PreRender,但是在这种情况下并没有改变。 it acts exactly the same. 它的行为完全相同。

I also tried setting the buttons' UseSubmitBehavior = false, but no luck. 我也尝试设置按钮的UseSubmitBehavior = false,但是没有运气。

Thanks! 谢谢!

This is probably a latency issue. 这可能是延迟问题。 It sounds like you're able to click the controls before the page has fully loaded all the JS. 听起来您可以在页面完全加载所有JS之前单击控件。 Is there an onload() function in there? 那里有一个onload()函数吗? Perhaps you could try hiding the controls until the page has finished loading? 也许您可以尝试隐藏控件,直到页面加载完成?

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

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