Passing a value from external class to a asp.net page and binding it to a control

Off late, I was working in a WCF application which involves all the crazy WCF stuffs like duplex contract, callback contract, asynchronous delegates etc. At one point I was in a position where I need to pass an item from an external class to an asp.net page and bind it to the control.

To call a function in a asp.net page from another class we have to either declare the function to be static (so that we can invoke it directly with the class name) or create an instance for the page class and call it through the object.

Both of them wont work because of the following reasons –

  1. If the function is static, it can’t operate on the controls present in the page (since the controls are not static)
  2. If you create an instance for that page class all the controls will be lost as a new instance of the page is created

Finally i achieve it through events and by declaring the function to be static.

//Create a delegate for the event
private delegate void DelegateHandler (type object);

//Create the event
private static event DelegateHandler RaiseEvent event;

//Create an object for the delegate
private static DelegateHandler oHandler = null;

//Add the function to be called from the static function to the delegate
oHandler = new DelegateHandler(OriginalFunctionToBeCalled);

//Add the delegate to the event
event += oHandler;

public static void StaticFunctionToBeCalled(Type obj)
{
    event(obj); //Raise the event
}
private void OriginalFunctionToBeCalled(Type obj)
{
    //bind the value to the control
}

Thats it. Hope it would be useful for someone.