for example i have a situation where two control's are dynamically created . one is a link button and another is a span control just to display some warnings. When the link button is clicked some actions occur in the server and the warnings of that action are displayed in the span control. Here i could not use the update panel's since the controls are dynamically created and they are not in same position . They are in some parts of
the page.
Here comes the rescue in the form of the ICallbackEventhandler !.
We can do the partial postback Synchronous and Asynchronous. Here i have explained the Synchronous way of doing that . We can achieve this Asynchrnously by means of IAsync. I will discuss about this in another post.
We have to implement this interface ICallbackEventhandler in the page class.
Partial Class EditClass
Inherits System.Web.UI.Page
Implements ICallbackEventHandler
Once we implement we get these methods GetCallbackResult() and RaiseCallbackEvent(ByVal eventArgument As String) implemented.
We have a set of JavaScript functions to be defined for using this Icallbackeventhandler. They are
1.The function to be called from the control from client side.
This is the function that is called from the control which needs the partial postback.
Here we are using a asp.net server control. So we are calling a javascript funtion from the onClientClick.This function has to be written in the page's markup as follows.
function CallServer() {
var justExample = '';
MakePostBack(justExample,'');
}
2.The functions that are generated from the code Behind files. They are as follows
Dim cs As ClientScriptManager = Page.ClientScript
Dim cbref As String = cs.GetCallbackEventReference(Me, "arg", "ReceiveServerData", "context")
Dim cbscr As String = "function MakePostBack(arg, context){" + cbref + ";}"
cs.RegisterClientScriptBlock(Me.GetType(), "MakePost", cbscr, True)
We are getting the callback reference of the page from this method GetCallbackEventReference. This will get generated something like "WebForm_DoCallback
('__Page',arg,ShowPop,context,null,false);". We make this call in another javascript function to make this postback call. This we make by means of the registering hte script to the page's clientscriptmanager.
3. The server methods GetCallbackResult() and RaiseCallbackEvent(ByVal eventArgument As String) have to be defined.
Define this string _str in page level
Dim _str As String
'This function returns the data to the client
Public Function GetCallbackResult() As String Implements System.Web.UI.ICallbackEventHandler.GetCallbackResult
Return _str
End Function
'This is the Callback function from the control. Here we can do the needed manipulation with the arguments which we receive from the client page.
Public Sub RaiseCallbackEvent(ByVal eventArgument As String) Implements System.Web.UI.ICallbackEventHandler.RaiseCallbackEvent
_str="Hello World"
End Sub
4. Then we have to define the function ReceiveServerData in Markup page. This is the function that receives the data from the server. We can use this for our specific needs. You can even get a full data
source by means of the XML string's and manipulating in the client side XMLDom javascript objects.
function ReceiveServerData(result, context) {
var strResult = new String();
strResult = result;
alert(strResult );
}
I have referred the following Microsoft Article to be very very useful for this .
0 comments:
Post a Comment