Dave Burke : Freelance .NET Web Developer specializing in Online Communities

More on Reflection and usercontrol subforms

I received a comment from Kevin (no URL) about a recent post on using reflection with reusable usercontrols, so for kicks I thought I'd elaborate on the process of using subform usercontrols. 

Before doing so, I wanted to restate that the essence of my earlier post on using reflection in usercontrols was that with reflection you can call a parent page method without casting it to its base class.  You can use the same usercontrol and the same code in any number of parent forms using reflection as long as the parent pages contain the InvokeMember Method (as in the example below, a ShowForm1b method.)

parentType.InvokeMember("ShowForm1b",System.Reflection.BindingFlags.InvokeMethod, null, this.Parent.Page, null);


Back to describing the process of using usercontrols as subforms.  The usercontrol (in our example, a New Location subform) is shown below at left.  The normal form is at right.

The logic starts with adding the usercontrol in the parent form .ASPX.

<tr> <td colspan="3"> <STREAMS:C_Wzaddloc id="uc_wzaddloc" runat="server"></STREAMS:C_Wzaddloc> </td> </tr>


In the parent form.  When the user needs to enter a new location, the "ADD" Linkbutton calls the AddLoc() method in the parent form which displays the usercontrol.

protected void AddLoc(object sender, CommandEventArgs e)
{
 // HTMLTable tblAddLoc is located in the usercontrol with visibility = false set default  
 HtmlTable tblAddLoc = (HtmlTable) uc_wzaddloc.FindControl("tblAddLoc");
 tblAddLoc.Visible = true;
 etc...
}
  
In the usercontrol.  New location data is entered in the subform--the usercontrol--which calls Process() to handle its button click processing.  Still in the usercontrol, Closem() is called to handle the UI display.   New location data is added and the new locationID is passed to a RefreshLocs(int lid) method in the parent form which populates and sets the location dropdown form.  The RefreshLocs method is in a base class inherited by 6 webpages, so with inheritance (coool) the method doesn't even have to be duplicated in the parent pages.

protected void Process(Object Source, EventArgs e)
{
 if (Page.IsValid)
 {
  Closem();  // hide subform. see Closem() method details below
  
  // populate class, pass to Data Layer
  
  Location location = new Location(); 
  location.LocationWorkingID = txtLuid.Text.ToString();
  etc...
  int lid = oSQLLocations.AddLocation(location);
  
  // send new location recordID to parent page using reflection to populate and set location dropdown to new location
  // parent form method RefreshLocs(int lid) is shown below.

  Type parentType = this.Parent.Page.GetType();
  parentType.InvokeMember("RefreshLocs",System.Reflection.BindingFlags.InvokeMethod, null, this.Parent.Page, new object[] {lid});
 }
}

Notice that the calls begin in the usercontrols and passed to the parent form, which handles the display of other page usercontrols.  We're still in the usercontrol here.

private void Closem() 
{
 // hide subform usercontrol so that it no longer appears in form
 tblAddLoc.Visible = false;
 
 // call parent ShowForm1b() method which displays fields temporarily hidden by subform
 
 Type parentType = this.Parent.Page.GetType();
 parentType.InvokeMember("ShowForm1b",System.Reflection.BindingFlags.InvokeMethod, null, this.Parent.Page, null);
}

  

Now back up in the parent form, ShowForm1b() method is called from the usercontrol through reflection which displays the form elements and usercontrols that were temporarily hidden when the subform was displayed.

public void ShowForm1b()
{
 trContacts.Visible = true;
 reqContacts.Enabled = true;
 trLocs.Visible = true;
 HtmlTable tblpropform1a = (HtmlTable) uc_wzpropform1a.FindControl("tblpropform1a");
 tblpropform1a.Visible = true;
}
  
The RefreshLocs() method populates the location dropdown with a fresh SQL call and selects the new location.  sidebar: oUIUtils.DoDropDownList() is a utility method I wrote to simplify populating a dropdown...   
 
public void RefreshLocs(int lid)
{
 int cid = int.Parse(ddClients.Items[ddClients.SelectedIndex].Value.ToString());
 oUIUtils.DoDropDownList(ddLocs, oSQLLocations.GetLocList(cid), "LocationID", "Location", lid);
}

And there you have it, all of the nerdy details of a reusable usercontrol subform fit to print.

 

Comments (0) | Post RSS RSS comment feed

Posted on 11/23/2005 11:50:00 AM by Dave Burke
Categories: .NET
Tags: no tags for this post

Related posts


Powered by BlogEngine.NET 2.0.0.36
Theme by Dave Burke