The getcallbackeventreference method implements partial refresh and callback is not implemented by webserice.

Source: Internet
Author: User

Why not use updatepanel? Is it too complicated to do so?

 

This section describes the clientscriptmanager. getcallbackeventreference method.
Development Environment: Asp. net2.0
In this example, the new ASP. net2.0 feature is used to implement client callback server events to asynchronously change the selection list in the dropdownlist without refreshing the changes. Compared with Ajax, WebService or local update can be used.

Gets a reference to a client function. When this function is called, a client callback for server-side events is started.

Parameter description
Control
The server that processes the client callback. This control must implement the icallbackeventhandler interface and provide the raisecallbackevent method.

Target (obtained using control. uniqueid)
The name of the server control that processes the client callback. This control must implement the icallbackeventhandler interface and provide the raisecallbackevent method.

Argument (used to pass a String object to the server, which can be obtained by receiving parameters in the raisecallbackevent method)
A parameter passed from the client script to the server

Clientcallback (this script function is triggered after the callback is successful, which is used to register this name)
The name of a client event handler that receives the results of successful server events.

Context (a script function or client object can be passed in. The following describes two different expressions)
The client script calculated on the client before the callback is started. The result of the script is returned to the client event handler.

Clienterrorcallback (this script function is triggered after a callback failure, which is used to register this name)
The name of the client-side event handler. the handler receives the result when an error occurs in the server-side event handler.

Useasync
True indicates synchronous execution callback; false indicates asynchronous execution callback.

Reload list
Description
Clientscriptmanager. getcallbackeventreference (control, string (argument), string (clientcallback), string (context) obtains a reference to the client function. When this function is called, calls back a client that starts a server-side event. The client function of this overload method contains the specified controls, parameters, client scripts, and context.
Clientscriptmanager. getcallbackeventreference (control, string (argument), string (clientcallback), string (context), Boolean) gets a reference to the client function. When this function is called, calls back a client that starts a server-side event. The client function of this overload method contains the specified controls, parameters, client scripts, context, and boolean values.
Clientscriptmanager. getcallbackeventreference (control, string (argument), string (clientcallback), string (context), string (clienterrorcallback), Boolean) gets a reference to the client function. When this function is called, calls back a client that starts a server-side event. The client functions of this overload method include the specified controls, parameters, client scripts, context, error handlers, and boolean values.
Clientscriptmanager. getcallbackeventreference (string (target), string (argument), string (clientcallback), string (context), string (clienterrorcallback), Boolean)

The general steps are as follows:
1. The server control that processes client callback inherits the icallbackeventhandler interface and implements the following two methods:

// Process parameters sent by the client.
Public void raisecallbackevent (string eventargument) {STR = eventargument );}

// Process the result sent back to the client. The client receives the result from the clientcallback client.
Public String getcallbackresult () {return STR}

2. Use clientscript members in the page class to manage scripts
Use clientscriptmanager cs = page. clientscript class

Associate server controls and register scripts with clients
String cbreference = cs. getcallbackeventreference ("'" + Page. uniqueid + "'", "Arg", "eseserverdata", "", "processcallbackerror", false );
String callbackscript = "function calltheserver (ARG, context) {" + cbreference2 + ";}";
CS. registerclientscriptblock (this. GetType (), "calltheserver", callbackscript, true );

3. register the client Script Function
Function compute eserverdata (ARG, context ){.......}
Function processcallbackerror (ARG, context ){..............}

4. Client call script
Calltheserver1 (ARG, context)

Example 1
Callback. aspx. CS code:
Using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. IO;
Using system. text;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;

Public partial class callback: system. Web. UI. Page, system. Web. UI. icallbackeventhandler
{
Public String STR = string. empty;

// Process parameters sent by the client.
Public void raisecallbackevent (string eventargument)
{
STR = eventargument;
}

// Process the result sent back to the client. The client receives the result from the clientcallback client.
Public String getcallbackresult ()
{
String [] STR = new string [] {"11", "22", "33", "44 "};
Listitem item = NULL;
This. drplist1.items. Clear ();
Foreach (string TMP in Str)
{
Item = new listitem (TMP, TMP );
This. drplist1.items. Add (item );
}

Stringwriter writer1 = new stringwriter (system. Globalization. cultureinfo. invariantculture );
Htmltextwriter writer2 = new htmltextwriter (writer1 );
This. drplist1.rendercontrol (writer2 );

Writer2.flush ();
Writer2.close ();
Return writer1.tostring ();
}

Protected void page_load (Object sender, eventargs E)
{
Clientscriptmanager cs = page. clientscript;

// Define the processing function after the client successfully calls
Stringbuilder context1 = new stringbuilder ();
Context1.append ("function extends eserverdata1 (ARG, context )");
Context1.append ("{");
Context1.append ("King. innerhtml = ARG ;");
Context1.append ("}");

// Define the client error handling function
Stringbuilder context2 = new stringbuilder ();
Context2.append ("function processcallbackerror (ARG, context )");
Context2.append ("{");
Context2.append ("alert ('an error has occurred .');");
Context2.append ("}");

// Register the script with the client
CS. registerclientscriptblock (this. GetType (), "processcallbackerror ",
Context2.tostring (), true );

// Return the script code for the customer to call the service event. The result is as follows: webform_docallback ('_ page', ARG, receiveserverdata1, function receiveserverdata1 (ARG, context) {king. innerhtml = ARG;}, processcallbackerror, false );
// The content value is a script function.
String cbreference1 = cs. getcallbackeventreference ("'" +
Page. uniqueid + "'", "Arg", "receiveserverdata1", context1.tostring (),
"Processcallbackerror", false );

String callbackscript1 = "function calltheserver1 (ARG, context) {" +
Cbreference1 + ";}";

// Register the script block that triggers the callback event
CS. registerclientscriptblock (this. GetType (), "calltheserver1 ",
Callbackscript1, true );
}

}

The callback. aspx code is as follows:
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "Callback. aspx. cs" inherits = "Callback" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en ""Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns ="Http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> getcallbackeventreference </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Span id = "King">
<Asp: dropdownlist id = "drplist1" runat = "server" width = "171px">
<Asp: listitem> ASA </ASP: listitem>
</ASP: dropdownlist>
</Span>
<A href = "javascript: calltheserver1 ('', '')"> change </a>
</Form>
</Body>
</Html>

Example 2
Callback1.aspx. CS code:
Using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. IO;
Using system. text;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;

Public partial class callback1: system. Web. UI. Page, system. Web. UI. icallbackeventhandler
{
Public String STR = string. empty;

// Define method that processes the callbacks on server.
Public void raisecallbackevent (string eventargument)
{
STR = eventargument;
}

// Define method that returns callback result.
Public String getcallbackresult ()
{
String [] STR = new string [] {"11", "22", "33", "44 "};
Listitem item = NULL;
This. drplist1.items. Clear ();
Foreach (string TMP in Str)
{
Item = new listitem (TMP, TMP );
This. drplist1.items. Add (item );
}

Stringwriter writer1 = new stringwriter (system. Globalization. cultureinfo. invariantculture );
Htmltextwriter writer2 = new htmltextwriter (writer1 );
This. drplist1.rendercontrol (writer2 );

Writer2.flush ();
Writer2.close ();
Return writer1.tostring ();
}

Protected void pageappsload (Object sender, eventargs E)
{
Clientscriptmanager cs = page. clientscript;

// Define the processing function after the client successfully calls
Stringbuilder context1 = new stringbuilder ();
Context1.append ("function extends eserverdata1 (ARG, context )");
Context1.append ("{");
Context1.append ("context. innerhtml = ARG ;");
Context1.append ("}");

// Define the error handling function
Stringbuilder context2 = new stringbuilder ();
Context2.append ("function processcallbackerror (ARG, context )");
Context2.append ("{");
Context2.append ("alert ('an error has occurred .');");
Context2.append ("}");

// Register the script with the client
CS. registerclientscriptblock (this. GetType (), "eseserverdata1 ",
Context1.tostring (), true );
CS. registerclientscriptblock (this. GetType (), "processcallbackerror ",
Context2.tostring (), true );

// Return the script code for the customer to call the service event. The result is as follows: webform_docallback ('_ page', ARG, receiveserverdata1, function receiveserverdata1 (ARG, context) {king. innerhtml = ARG;}, processcallbackerror, false );
// The content is the king client King object.
String cbreference1 = cs. getcallbackeventreference ("'" +
Page. uniqueid + "'", "Arg", "receiveserverdata1", "context ",
"Processcallbackerror", false );

String callbackscript1 = "function calltheserver1 (ARG, context) {" +
Cbreference1 + ";}";

// Register the script block that triggers the callback event
CS. registerclientscriptblock (this. GetType (), "calltheserver1 ",
Callbackscript1, true );
}

}

The callback1.aspx code is as follows:
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "Callback. aspx. cs" inherits = "Callback" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en ""Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns ="Http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> getcallbackeventreference </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Span id = "King">
<Asp: dropdownlist id = "drplist1" runat = "server" width = "171px">
<Asp: listitem> ASA </ASP: listitem>
</ASP: dropdownlist>
</Span>
<A href = "javascript: calltheserver1 ('', King) "> change </a>
</Form>
</Body>
</Html>

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.