Tuesday, October 23, 2012

Sys.InvalidOperationExceptio in UpdatePanel in asp.net ajax due to control structure



I recently got this error: Sys.InvalidOperationException: Could not find UpdatePanel with ID 'ctl00_QuoteContent_UpdatePanelSiteAddress'. If it is being updated dynamically then it must be inside another UpdatePanel. And I fixed it too! I was using updatepanel in my page to update certain part from the page. I googled much. [I could sagoon too!]. They suggest that disabling event validation in the page would solve the problem. But nope. Some even told that removing UpdateMode="Conditional" for the upatepanel would solve the problem. And nope again!

Why the Sys.InvalidOperationException with the above mentioned error occurred?

This happened to occur because I had tried to access (and modify property of) a control outside the updatepanel's contenttemplate. Let me show this.

Here goes the design page:
1. I have one dropdownlist (ddlAddressType).
2. Now I have a table with a row [row id trAddress]
3. Within this row I have one updatepanel.
4. In the updatepanel's ContenTemplate I have another table in which I have a gridview.
5. I have added ddlAddressType as the asychronous postback control for the updatepanel.

That's all. For quick preview, I want to hide the row trAddress which is outside the updatepanel itself. Got it?
Now see here the code behind page:

//on selected index changed
protected void ddlAddressType_SelectedIndexChanged(object sender, EventArgs e)
{
if(ddlAddressType.SelectedValue.Contains("--"))
{
//Here I had tried to access the row (set visibility false)
trAddress.Visible=false;
}
else
{
//Here I had tried to access the row (set visibility true)
trAddress.Visible=true;
}
}

Clearly, the row I am trying to make invisible is out of the scope of the updatepanel. So the error occurred.

Solution? In my case, I just set the table visible/invisible. Solved! Happy Programming