Wednesday, April 21, 2010

Passing object[] from a webmethod

A PageMethod returning a object[] will be the ugliest of solutions, you would say. Sometimes, you are forced to code in a not-so-elegant style. And here's one such implementation. If any of you think you can make it better, please leave a comment.



Another solution to this will be to wrap all these objects up into a class and have this as the return type.


public class MyReturnType {
public ClassA A { get;set; }
public ClassB B { get;set; }
public ClassC C { get;set; }
}


and return an instance of MyReturnType (or List for multiple values)

Returning this composite MyReturnType object will be the ideal solution. But in my case, the elements of this class are known only at run time. Typically, this class will need to be build at run time. The easier alternate will be to


[WebMethod]
public object[] GetObjects()
{
...
return new object[] { objA, objB, objC };
}

Tuesday, April 13, 2010

Tidy your HTML code on Visual Studio

I, so absolutely love this shortcut! (Next only to Ctrl + k,c)

There's a way to tidy up your HTML source code view on your Visual Studio
- Ctrl + k,d and Ctrl + k,f

While Ctrl + k,d formats the whole page, Ctrl + k,f formats the selected area on the page

Wednesday, April 7, 2010

Implementing ICallbackEventHandler

Asynchronous communication with the server is also made possible by implementing the ICallbackEventHandler interface (System.Web.UI.ICallbackEventHandler). ICallbackEventHandler sends user defined elements back to the server to process and the output is return in the form of a string back to the client, thus enabling a partial postback without the UpdatePanel.

These are the steps in implementing ICallbackEventHandler interface:

Inherit ICallbackEventHandler

Inherit ICallbackEventHandler on the page or the user control where it needs to be implemented. The code for this will be


public partial class Contributions : System.Web.UI.UserControl, ICallbackEventHandler


As a result, two functions needs to be implemented in the code
  1. public void RaiseCallbackEvent(string eventArgument)
  2. public string GetCallbackResult()
RaiseCallbackEvent() is automatically called when there is a Callback event. The GetCallbackResult() is called in sequence right after the RaiseCallbackEvent() is processed.


public string GetCallbackResult()
{
return result;
}


This method here just ejects HTML code into the page to set the server datetime.


public void RaiseCallbackEvent(string eventArgument)
{
result = "document.getElementById('txtPercentage').innerHTML = '" + DateTime.Now.ToLongTimeString() + "'";
}


The method RaiseCallbackEvent() here, just passes the server datetime back to the client. This can typically be any server process and the result variable being set.


ASPX/ASCX page

Below is the HTML for the usercontrol. The Callback, in this case will be initiated from a user control.


<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Contributions.ascx.cs" Inherits="Contributions" %>










The above code will just create a CallbackEvent on onclick() event of the HTML button control.

Code File Wireup

The code for the server side OnPageLoad() will be,


if (!IsPostBack)
{
string callback = Page.ClientScript.GetCallbackEventReference(this, "arg", "DisplayResults", "context");
string script = "function CallBack(arg,context){" + callback + ";}";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "CB", script, true);
}


GetCallbackEventReference() obtains a reference to the client function that when invoked, initiates a client callback to the server.


public string GetCallbackEventReference(
string target,
string argument,
string clientCallback,
string context,
string clientErrorCallback,
bool useAsync
)


target - The server control that initiates the callback. The control 'must' implement the ICallbackEventHandler interface

argument - Argument that is passed from the client script to the server for processing

clientCallback - The client script method that needs to be called after the server callback is processed

context - Client script that is evaluated on the client before initiating a callback to the server

useAsyn - When true initiates the call in the asynchronous fashion


function CallBack(arg,context) - is the client side script that invokes the GetCallbackEventReference() with the required arguments.

All this should set you going for your implementation of ICallbackEventHandler.