Event not raising when FCA control used in User Control (.ascx)

Feb 25, 2008 at 3:42 PM
Hallo there,

I downloaded source code to use the treeview control adapter.

I followed all directions, but I found that server events (like SelectedNodeChanged) where not called at all, even declaring method Public in source file and correctly including OnAdaptedSelectedNodeChanged properties in tag declaration.

After a search in this site and in google I decided to debug what was happening under the hood, ending up in what, in my opinion, is the error.

This piece of code in the base class (WebControlAdapterExtender) is the "event forwarder" for the various subclassed controls:

public void RaiseAdaptedEvent(string eventName, EventArgs e)
{
string attr = "OnAdapted" + eventName;
if ((AdaptedControl != null) && (!String.IsNullOrEmpty(AdaptedControl.Attributesattr)))
{
string delegateName = AdaptedControl.Attributesattr;
Control methodOwner = AdaptedControl.Parent;
MethodInfo method = methodOwner.GetType().GetMethod(delegateName);
if (method == null)
{
methodOwner = AdaptedControl.Page;
method = methodOwner.GetType().GetMethod(delegateName);
}
if (method != null)
{
object[] args = new object2;
args0 = AdaptedControl;
args1 = e;
method.Invoke(methodOwner, args);
}
}
}

It simply prefix the event name with the "OnAdapted" string and then try to raise the event, serching for it in the FCA control parent and, if not found, in the page.

What usually happen writing an asxc (User control) is that the event handler is in the ascx code behind page, alas in ascx class, so, the event handler is neither in FCA control parent nor in the page. This make the above method fail.

A workaround could be to search recursively in all FCA Ancestors, starting from parent and ending when the ancestor is a Page Class, but, for my needs I found that changing parent with NamingContainer works well.

Modified line is the following:
Control methodOwner = AdaptedControl.Parent; --> Control methodOwner = AdaptedControl.NamingContainer;

Hope this help,





Coordinator
Feb 25, 2008 at 6:04 PM
Thanks for the feedback.

It seems a reasonable "fix" would be to append an additional "if" clause in the RaiseAdaptedEvent method, as follows.

Control methodOwner = AdaptedControl.Parent;
MethodInfo method = methodOwner.GetType().GetMethod(delegateName);
if (method == null)
{
methodOwner = AdaptedControl.Page;
method = methodOwner.GetType().GetMethod(delegateName);
}
if (method == null)
{
methodOwner = AdaptedControl.NamingContainer;
method = methodOwner.GetType().GetMethod(delegateName);
}

By doing this, we would retain current functionality and probably support your issue. I'll convert this to a work item.
Coordinator
Feb 25, 2008 at 6:05 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.