Implementation of client callback

Source: Internet
Author: User

The callback of the client is actually only a method to Implement Asynchronous partial page update,

That is, the page does not have the whole page to be sent back, and javascript can be used.CodeCall the server C # On the client #ProgramCode,

This method is another method relative to Ajax. Of course, you can use ajax to Implement Asynchronous updates,

In some cases, client callback may be a better solution to asynchronous update.

To implement client callback, the following conditions must be met:

First, the class file of your server program code must implement an Interface

System. Web. UI. icallbackeventhandler

This interface is used to indicate that the page can be used as the target for callback events on the server,

That is, to indicate that the current page can be used for client callback,

You must also implement the two methods in this interface in code-behind of the page,

Public void raisecallbackevent (string eventargument)

Public String getcallbackresult ()

The first method is to implement the solution process, that is, after the client executes the callback,

The content in this method will be executed on the server,

The second method is used to return a result of the solution,

This method is executed after raisecallbackevent is executed.

For client callback registration on the serverGetcallbackeventreferenceThis method is implemented

The usage of this method is described in msdn as follows:

Page. clientscript. getcallbackeventreference

Public String getcallbackeventreference (Control, string argument,

String clientcallback, string context );

// Obtain a reference to the client function. When this function is called,

// Initiate a client callback for server events.

// The client function of this overload method contains the specified controls, parameters, client scripts, and context.

// Control:

// The system. Web. UI. Control Server that processes the client callback. This control must be implemented

// System. Web. UI. icallbackeventhandler

// Interface and provide

// System. Web. UI. icallbackeventhandler. raisecallbackevent (system. String) method.

// Argument:

// A parameter passed from the client script to the server

// System. Web. UI. icallbackeventhandler. raisecallbackevent (system. String) method.

// Clientcallback:

// The name of a client event handler. the handler receives the result of a successful server event.

// Context:

// The client script calculated on the client before the callback is started. The result of the script is returned to the client event handler.

// Return result:

// Name of the client function that calls the client callback.

Page. clientscript. getcallbackeventreference (this, "Arg ",

"Callbackclient", "context ");

Because the getcallbackeventreference method obtains a client function,

This method is used to start a client callback, and it is all in the C # code,

You cannot register directly on the client using JavaScript,

I must use JavaScript on the client to initiate a callback,

Therefore, you must register a send callback using the clientscriptmanager class on the server.

Or the JavaScript function that triggers the callback,

Then, use the registered function and the JavaScript function of the processing result specified on the client.

The interaction between the client and the server can be realized.

Let's take a look at an instance first:

Public partial class three _ 4: system. Web. UI. Page,

System. Web. UI. icallbackeventhandler
{
Private string result;
Private dataset mydataset = new dataset ();

Protected void page_load (Object sender, eventargs E)
{

// Getcallbackeventreference is used to reference the client function that triggers the client callback.

// The "Arg", "callbackresult", and "context" defined here"

// When registering the callback function callbackserver, the two parameters Arg and context must be consistent with the preceding

// While the callbackresult parameter indicates that it is in the JavaScript code defined by the client

// This function must accept the callback result and use Dom to update the page.
String callbackreference = This. Page. clientscript.

Getcallbackeventreference (this,
"Arg", "callbackresult", "context ");

String callbackserver = "function callbackserver (ARG, context) {" +

Callbackreference + "}";

String keyblock = "Block ";

If (! Page. clientscript. isclientscriptblockregistered (this. GetType (), keyblock ))
{
Page. clientscript. registerclientscriptblock (this. GetType (),
Keyblock, callbackserver, true );
}

String constr = webconfigurationmanager. connectionstrings ["three"]. connectionstring;
String sqlstr = "select ID card number, Student name from student ";
Using (sqlconnection sqlcon = new sqlconnection (constr ))
{
Using (sqlcommand sqlcom = sqlcon. createcommand ())
{
Sqlcom. commandtype = commandtype. text;
Sqlcom. commandtext = sqlstr;
Using (sqldataadapter sqlda = new sqldataadapter (sqlcom ))
{
Sqlda. Fill (mydataset );
Ddlname. datasource = mydataset. Tables [0];
Ddlname. datatextfield = "Student name ";
Ddlname. datavaluefield = "ID card number ";
Ddlname. databind ();
}
}
}
}

Public void raisecallbackevent (string eventargument)
{< br> for (INT I = 0; I {< br> If (mydataset. tables [0]. rows [I] [1]. tostring () = eventargument)
{< br> result = mydataset. tables [0]. rows [I] [0]. tostring ();
return;
}< BR >}< br> result = "this student has not entered his ID card";
}

Public String getcallbackresult ()
{
Return result;
}
}

HTML Tag

<HTML>
<Head runat = "server">
<Title> </title>

<Script language = "JavaScript" type = "text/JavaScript">

Function callbackresult (result, context ){
Document. getelementbyid ("lblid"). innertext = result;
}

Function btncallback_onclick (){
VaR name = (document. getelementbyid ("ddlname "))

[Document. getelementbyid ("ddlname"). selectedindex].

Innertext;

// Call callbackserver to initiate the client callback and pass a parameter in the callback function,

// This parameter will be received in the raisecallbackevent method of the server
callbackserver (name ,"");
}< br>





select a name & nbsp; & nbsp;



Onclick = "Return btncallback_onclick ()"/> <br/>
<Br/>
<Asp: Label id = "lblid" runat = "server"> </ASP: Label>
<Br/>
</Div>
</Form>
</Body>
</Html>

View the running interface again

Click "execute callback" to obtain the ID number.

The key is that the page will not be refreshed during callback execution, thus implementing asynchronous local update.

 

2010-1-24

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.