|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
So, you are interested in knowing about the A very consise and correct definition of the interface which explains everything, except I can see you scratch your head & wonder whats in it for you. Stick along with this article & hopefully you would have a new toy to impress your peers at work! The problem: Suppose you need to build a simple ASP.NET 2.0 web page which has a section to show some quotes. You want to randomize these quotes such that the user sees new quotes every few seconds (this is because of the crappy content, but try telling that to the content writer!) A few possible solutions:
Solution I prefer:
I would prefer using the
Show me to code: I know you are excited to see this implemented, so now would be a good time to download the source code for the sample application & use it as a reference for the remaining sections of this article.
All this while, we have been talking about the public interface ICallbackEventHandler { string GetCallbackResult(); void RaiseCallbackEvent(string eventArgument); } Implementing this interface & getting the code to work is as simple as the 3 steps listed below. Step 1: implement the interface on the Page/UserControl class
To keep our code a bit organized, I create a RandomQuotesUserControl.ascx user control which would, surprisingly, contain the logic to show a random quote. This user control is then registered on the page which needs to show these quotes. In our sample application, I have registered this control on the default.aspx page itself. Alternatively you could create a
The inheritence of the public partial class RandomQuoteUserControl : System.Web.UI.UserControl , ICallbackEventHandler Step 2: Writing implementation for the interface methods
I start by implementing the public void RaiseCallbackEvent(string eventArgument) { m_callbackResult = "document.getElementById('randomQuote').innerHTML ='" + this.getQuote() + "'"; }
In this method, I am setting a string to the a local member which will contain the final output which needs to be placed on the UI when the public string GetCallbackResult() { return m_callbackResult; } Step 3: Register the client callback reference
In this step, we will be registering the client callback javascripts with the Page so that our client side will understand what to do to get our code working. This is coded in the protected void Page_Load(object sender, EventArgs e) { // Creating the scripts which need to be called string clientScript = "function serverRequest(args, context){{ " + Page.ClientScript.GetCallbackEventReference(this, "args", "getResult", "context") + "; }} function getResult(result, context){{ eval(result); callServer(); }} function callServer(){ setTimeout('serverRequest();', 3000);} callServer();"; // Register the client script Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "RandomQuoteCallback", clientScript, true); } Thats it, compile & run the application to see the quotes changing every 3 seconds.
This is a very simple example of what can be done using the This works for me & hopefully it works for you too. Happy programming!
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||