Asp – Custom Control Event Not Firing First Time

asp.net

My application runs from a single page (default.aspx) and loads custom controls based on what the user selects (this is done in the LoadPage event).

The problem is that the FIRST time some new controls are loaded onto the page and a button is clicked, the page is NOT firing the Click event. This also happens when using a GridView and other controls.

However, if I simply reload the page a second time, the events are fired properly. (this works successfully every time)

Two questions:
1) Why would this occur?
2) How can I troubleshoot it better? I've tried checking the Request.Form(__EVENTTARGET) (which is blank)

There are times when it works properly which suggests a PostBack issue – but it's not obvious.

Best Answer

I have identified the source of the problem and wanted to note it here so that it may help others.

The code in question had three different methods that assigned the Custom controls ID property. Two of those methods were doing it dynamically so the first time in, the custom control's ID was "UserProfile" - the second time (during the post-back), it became "uc1" (user Control 1). As a result, since the two IDs were not the same, ASP.Net did not recognize them as being the same control.

The SECOND time it posted back, it used the second method and thus the controls were the same and did get updated.

Related Topic