Sunday, October 18, 2009

Test your WCF services using the WCF Test Client

Shipped with Microsoft Visual Studio is an application called the WCF Test Client (Start Menu > Open Visual Studio Command Prompt, type wcftestclient and press enter). This application can be used to test the methods/operations of any WCF service.

In the application, do File > Add Service, enter the service URL and click OK. You will now see the available service operations. Click on any operation, enter a value for each parameter and click Invoke. The result will be displayed in the response frame.

Monday, October 5, 2009

A recursive FindControl extension method

The System.Web.UI.Control class has a FindControl method for finding a control by its ID. This method searches for a given control in the first level child controls, and not in any child controls of a child. In order to do the latter, a custom method must be implemented. Below is a generic, recursive, extension method that does this:

public static class ControlExtension
 public static T FindControlRecursive(this Control container, string id)
   where T : Control
  if (container.HasControls())
   T foundControl = null;

   foreach (Control control in container.Controls)
    if (control.ID == id && control is T)
     foundControl = (T) control;
     foundControl = FindControlRecursive(control, id);

    if (foundControl != null)
     return foundControl;

  return null;


var control = Page.FindControlRecursive("TxtName");