Today I want to talk you about a problem I faced in these last days working on a ASP.NET web form project. In this project I used the library “UI for ASP.NET AJAX” from Telerik, and in particular the problem occurred using the RadGrid control.
What was the problem? In a nutshell, without an explanation, the pagination of the RadGrid control stopped working, with the control stuck on page 2. After trying several changes to the source code, I realized that the problem was in a function whose purpose was get the reference to a control in an ASP.NET page or control in a recursive way (a kind of recursive FindControl).
The “offending” function was taken on the Internet and looks like the following:
public static Control FindControlRecursive(Control container, string name)
{
if ((container.ID != null) && (container.ID.Equals(name)))
return container;
foreach (Control ctrl in container.Controls)
{
Control foundCtrl = FindControlRecursive(ctrl, name);
if (foundCtrl != null)
return foundCtrl;
}
return null;
}
There is nothing wrong in the function above, and you can find many similar on the Internet which do same task. Moreover the function is not modifying anything, it just recursively reads the control collection of each control, and looks for the control ID passed as a parameter.
What’s the solution?
As already said there was no explanation to this phenomenon, and so the only solution I found is in fact a workaround, which consists in modifying the function as follow:
public static Control FindControlRecursive(Control container, string name)
{
if ((container.ID != null) && (container.ID.Equals(name)))
return container;
foreach (Control ctrl in container.Controls)
{
if (ctrl is RadGrid)
continue;
Control foundCtrl = FindControlRecursive(ctrl, name);
if (foundCtrl != null)
return foundCtrl;
}
return null;
}
For some reason the RadGrid object doesn’t like that you read its control collection, so adding the two lines of code in bold did the trick.
It’s a very simple workaround, but I assure you that I spent a lot of time to find it, and I’m very happy to share this solution with you.
Hope this helps 😉