Friday 19 April 2013

Control with id XXX could not be located or a different control is assigned to the same ID after postback

 

I came across this error while developing a page with so many user controls in it.

Some of them are loaded dynamically in the code behind like image buttons in a ListView.

While filtering the ListView, sometimes I would get the error and sometimes not.

Problem

Server Error in '/YYY Application.


An error has occurred because a control with id ‘XXX' could not be located or a different control is assigned to the same ID after postback. If the ID is not assigned, explicitly set the ID property of controls that raise postback events to avoid this error.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
Exception Details: System.Web.HttpException: An error has occurred because a control with id 'ctl00$contentMain$lstGoalList$ctrl4$ctl00' could not be located or a different control is assigned to the same ID after postback. If the ID is not assigned, explicitly set the ID property of controls that raise postback events to avoid this error.
Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[HttpException (0x80004005): An error has occurred because a control with id 'XXX' could not be located or a different control is assigned to the same ID after postback. If the ID is not assigned, explicitly set the ID property of controls that raise postback events to avoid this error.]

System.Web.UI.Page.ProcessPostData(NameValueCollection postData, Boolean fBeforeLoad) +988

System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3294

Solution

I forgot to assign id’s to the dynamically generated Image controls.It used to work fine even with no id’s. Error was very rare and sometimes unable to reproduce it.

I added unique IDs to each Image control when I created them and it solved the problem.