How to make your own Postback pages
First let’s see our post backs work. Click on one of the tasks below and see the
relevant data returned from the server without any visible reload of the page
Get HTTP_COOKIE
Get HTTP_USER_AGENT
Now lets have a look at the client code.
From the window onload event we are calling the Callserver function with the argument "5"
The Callserver function is added from the server code, we will show how to do that below.
Next we have a function called ReceiveServerData, this function receives the data returned from the server.
Very simple we call a function to pass a argument to the server, and we recive an answer from the server in anouther function.
<script language="javascript" type="text/javascript">
<!--
function window_onload() {
var args = 5
CallServer(args, "")
}
function ReceiveServerData(rValue)
{
alert(rValue)
}
// -->
</script>
Now for the server code
First we implement the System.Web.UI.ICallbackEventHandler.
Look for this line in your code behind page "Inherits System.Web.UI.Page"
straight below this line insert "Implements System.Web.UI.ICallbackEventHandler".
Declare the variable "returnValue" As String.
GetCallbackResult() : sends the data back to the client, this can be copied as is into your page.
setupClientScript() : creates the CallServer function in our client code that we mentioned earlier, this also
can be copied into your page as is.
RaiseCallbackEvent() receives the arguments from the client code and assigns the return value
to the "returnValue" variable to be sent back to the client.
Be Sure to fire setupClientScript() in the page load event.
Implements System.Web.UI.ICallbackEventHandler
Dim returnValue As String
Public Function GetCallbackResult() _
As String Implements _
System.Web.UI.ICallbackEventHandler.GetCallbackResult
Return returnValue
End Function
Sub setupClientScript()
Dim cbReference As String
cbReference = Page.ClientScript.GetCallbackEventReference(Me, "arg", _
"ReceiveServerData", "context")
Dim callbackScript As String = String.Empty
callbackScript &= "function CallServer(arg, context) { " & cbReference & "} ;"
Page.ClientScript.RegisterClientScriptBlock(Me.GetType(), "CallServer", callbackScript, True)
End Sub
Public Sub RaiseCallbackEvent(ByVal eventArgument As String) _
Implements System.Web.UI.ICallbackEventHandler.RaiseCallbackEvent
returnValue = CInt(eventArgument) * 5
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles Me.Load
setupClientScript()
End Sub
|