For a while now I've been using pop-out subforms like the one circled below. It's there when you need it, then poof, gone again when you don't. These are standalone usercontrols that I've re-used to a limited degree in various web pages, but was complicating re-use by not using reflection from the get-go. So I went that route today and wanted to slap some code on this blog again.
A quick and dirty approach to re-using the user controls is to simply grab the parent page name, cast to it's base class, and call the parent method, like so:
string parent = HttpContext.Current.Request.ServerVariables["SCRIPT_NAME"].ToString().ToLower();
switch (parent)
{
case "/wizprojadd.aspx":
((wizprojadd) Parent.Page).ShowForm();
break;
case "/wizprojupd.aspx":
((wizprojupd) Parent.Page).ShowForm();
break;
etc...
}
That works fine with a limited number of parent pages, like 2, but I now wanted to use the usercontrol in 4 other pages. The switch approach wasn't going to cut it.
Enter reflection.
Reflection allows for a single statement to obtain the parent object type and invoke a method. The only caveat is that all parent pages have the same method, which in my case they do.
Type parentType = this.Parent.Page.GetType();
parentType.InvokeMember("ShowForm",System.Reflection.BindingFlags.InvokeMethod, null, this.Parent.Page, null);
Mikey likes reflection.