crosspost from http://rextang.net/blogs/work/
I am reading Scott Hanselman co-authored, published by Wrox press, titled Professional ASP.NET 2.0 book recently to get a quick pick up of what's new in ASP.NET 2.0 compared to ASP.NET 1.1.
It's a nice book to read and it covered all the topics related to ASP.NET 2.0, including what's already had in ASP.NET 1.1. A comprehensive guide to people who want to enter new ASP.NET 2.0 world, no matter one is already familiar of ASP.NET 1.1 programming or not. A recommand book to read!
I am reading in a fast pace that browsing to those topics I was familiar during my work with ASP.NET 1.1 , and spend time in those which are new in ASP.NET 2.0. just passed chapter 4 and stop at the new ASP.NET 2.0 feature, Client Callback.
The example provided in this book is simple to understand, but the idea behind the design of the new Client Callback feature didn't well show up through the examples. I've also Googled some others and found this one at Code Project written by Invincible Poison. This one used a more easily understandable way trying to explain how Client Callback is working, although I've noticed that the provided sample code seems changed that didn't fit into the sample code snippets provided throughout the article.
The big concept behind the whole Client Callback things is clear enough for me now. basically, I'll consider the new Client Callback feature provided by ASP.NET 2.0 as a simplified version of what I've used the Ajax.Net framework . since Client Callback is actually using .axd to encapsulate the callback logics , while Ajax.Net is actually using a Http handler. but Ajax.Net can actually taking arbitrary parameters while client-side callback and even simulate DataSet and other customized object types from server-side return to client, also Client Callback only use one arg string parameter to pass all the paramaters, which means that when multiple paramaters are needed to pass across XMLHttp, developers need to concatenate all the parameters into one single string in order to pass it. Since this Ajax like feature is now officially provided by ASP.NET 2.0 , although not perfect yet (to let 3rd parties make better Client Callback controls), but should be a good start!
The other things that make me curious is the use of "context" parameter in GetCallbackEventReference function. many examples all stated that the "context" parameter mostly would just doing ok by inserting a string "context" and it will work, but what exactly the "context" string means? how it is used in the whole Client Callback framework? seems not many examples talk about this.
By reading the MSDN Library about this function, the function signature is as follows:
public string GetCallbackEventReference (
Control control,
string argument,
string clientCallback,
string context
)
where as the explaination of context parameter is as follows:
context
Client-side script that is evaluated on the client prior to initiating the callback. The result of the script is passed back to the client-side event handler.
by looking at the example provided from MSDN, it will be clear about what actually the context parameter is doing.
on the server side code:
// Get a ClientScriptManager reference from the Page class.
ClientScriptManager cs = Page.ClientScript;
// Define one of the callback script's context.
// The callback script will be defined in a script block on the page.
StringBuilder context1 = new StringBuilder();
context1.Append("function ReceiveServerData1(arg, context)");
context1.Append("{");
context1.Append("Message1.innerText = arg;");
context1.Append("value1 = arg;");
context1.Append("}");
// Define callback references.
String cbReference1 = cs.GetCallbackEventReference(this, "arg",
"ReceiveServerData1", context1.ToString());
String cbReference2 = cs.GetCallbackEventReference("'" +
Page.UniqueID + "'", "arg", "ReceiveServerData2", "",
"ProcessCallBackError", false);
String callbackScript1 = "function CallTheServer1(arg, context) {" +
cbReference1 + "; }";
String callbackScript2 = "function CallTheServer2(arg, context) {" +
cbReference2 + "; }";
// Register script blocks will perform call to the server.
cs.RegisterClientScriptBlock(this.GetType(), "CallTheServer1",
callbackScript1, true);
cs.RegisterClientScriptBlock(this.GetType(), "CallTheServer2",
callbackScript2, true);
noticed that cbReference1 string variable is actually taking the whole client function definition as the input of context parameter, while at cbReference2, it's actually supplying empty string as context parameter input, because of that the callback function of cbReference2 is actually defined in the aspx page's javascript block. by investigating the sample code as well as reading the description of context parameter again, it should be easiler to understand that the context parameter is actually a place to supply client-side block in server-side code block while defining a Client Callback client-side procedure! thus one can define the callback client function either in the server-side (code-behind file) or in the client side (aspx file), depends on where is proper to put it!
So, if one is putting client callback function in aspx page inside a script block, the context parameter can actually be specified as empty string and the program will still run, avoiding the "context" string input into context parameter that confuse the program readibility.
the other thing that's also interesting to see via the example code in MSDN Library:
<body>
<form id="Form1"
runat="server">
<div>
Callback 1 result: <span id="Message1">0</span>
<br />
Callback 2 result: <span id="Message2">0</span>
<br /> <br />
<input type="button"
value="ClientCallBack1"
onclick="CallTheServer1(value1, alert('Increment value'))"/>
<input type="button"
value="ClientCallBack2"
onclick="CallTheServer2(value2, alert('Increment value'))"/>
<br /> <br />
<asp:Label id="MyLabel"
runat="server"></asp:Label>
</div>
</form>
</body>
CallTheServer1 and CallTheServer2 are actually both with 2 arguments, "arg" and "context", according to previous code block's definitions. now, what's the mean of this "context" argument in these client functions? is this same as the context parameter previously mentions above or even it points to the same memory place?
Actually it's not the case. the context argument here seems not related to the context parameter mentioned above. and the function to this argument is simplely a client script block to let developers specify actions prior to callback to server. noticed that it's actually taking a javascript instance instead of "a string of javascript" as its input, which is also different from those demonstrated in other examples, which normally also just tell readers to supply a "context" string. Therefore, if there are no other client-side actions needed to be done before callback to server, the second optional parameter can actually have a empty string.
Conclusion:
-
the context parameter of GetCallbackEventReference Method is a place to put client-side code block in server-side to specify the callback client function to use. if not specify in server-side, then this parameter can be left to empty string and put the actual client callback function in client side aspx file.
-
the calling server function's second "context" argument is optional (but can not be stripped), which can be supplied by a javascript code block to let it run before Client Callback process prior to actually make the call back to server.
Technorati Tags: programming , asp.net , ajax , callback