In the previous post I talked about passing callbackresults collection from web adf controls as DataItems on the ScriptManager back to the client-side code for processing (updating client-side rendered controls). What if we didn't want to piggyback on a web adf control and append to its callbacks, what if we just want to pass a javascript as callbacks without piggybacking?
This is possible using a JSON serializer, and which is what i talk about in this post.
Usually this is what we do:
1) take a map control or task control and add callbacks to it, e.g.
Map1.Callbacks.AddRange(TaskResults1.Callbacks)
OR
Map1.Callbacks.Add(CallbackResult.CreateJavaScript("alert('hi');")
2) Then we serialize it to json: Map1.Callbacks.ToString()
3) Then we register it as DataItems on the ScriptManager control
myScriptMgr.RegisterDataItem(Page, Map1.Callbacks.ToString())
4) The asp.net ajax framework sends that dataItem string to the client, and we
have a js handler that takes all DataItems from the Page ("__Page") and
processes it, usually by passing the dataItem string to the js function,
ESRI.ADF.System.processCallbackResult(dataItemString)
Now if we skip step(1), and add callbacks to an empty callbackresultcollection, e.g.
new CallbackResultCollection.add(CallbackResult.CreateJavascript("alert('hi');")),
it won't be serialized properly and will not work.
There's two ways to pass javascript back to the client without a web adf component:
1) We can make use of the backward compatibility for AGS9.2 callback framework in the ESRI.ADF.System.js file, method: processCallbackResult. But note that we are not using the callback framework, we are just passing a string that makes use of that old format, e.g.
pass the below as a dataitem or scriptblock:
"///:::///:::javascript::: alert('hi');"
2) We can pass the new format, which is a json formatted data, for example:
'[{"id":null,"type":"","action":"javascript","params":["alert(\u0027 --hi-- \u0027);"]}]'
Dim jsonCallback As String = "[{{ {0}id{0}:null,{0}type{0}:{0}{0},{0}action{0}:{0}javascript{0},{0}params{0}:[{0}{1}{0}]}}]"
ScriptManager1.RegisterDataItem(Page, String.Format(jsonCallback, ControlChars.Quote, "alert('hi);"))
''' <summary>
''' To create an ESRI ADF usable json formatted response that can be processed by
''' client-side js function:
''' ESRI.ADF.System.processCallbackResult(string),
'''
''' where an example of string is:
''' [{"id":null,"type":"","action":"javascript","params":["alert(\u0027 --hi-- \u0027);"]}]
'''
''' </summary>
''' <param name="paramValue">depends on the action type, e.g. js code snippet, html code snippet etc
'''</param>
''' <param name="action">valid values:
''' javascript, content, innercontent, set, invoke, image, include
'''</param>
''' <returns>json formatted callback result to be handled by ESRI.ADF.System.processCallbackResult(result)</returns>
''' <remarks></remarks>
Public Function createJsonCallbackResult(ByVal paramValue As String, ByVal action As String) As String
Dim params() As String = {paramValue}
Dim hash As Hashtable = New Hashtable()
hash.Add("id", Nothing)
hash.Add("type", "")
hash.Add("action", action)
hash.Add("params", params)
Dim jsonConverter As System.Web.Script.Serialization.JavaScriptSerializer = New System.Web.Script.Serialization.JavaScriptSerializer()
Dim jsonResponseStr As String = jsonConverter.Serialize(hash)
'Dim jsonCallback As String = "[{{ {0}id{0}:null,{0}type{0}:{0}{0},{0}action{0}:{0}javascript{0},{0}params{0}:[{0}{1}{0}]}}]"
Return jsonResponseStr
End Function

No comments:
Post a Comment