Using system;
Using system. collections;
Using system. IO;
Using system. text;
Using system. Web;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. componentmodel;
Namespace Platform
{
/// <Summary>
/// Can process the JScript callback of the client.
/// </Summary>
[Defaultproperty (""),
Toolboxdata ("<{0}: scriptcallbackmanager runat = server ></ {0}: scriptcallbackmanager>")]
Public class scriptcallbackmanager: system. Web. UI. webcontrols. webcontrol
{
Private icallbackeventhandler _ handler;
Private string _ functionnameaftercallback;
Public scriptcallbackmanager (){
// _ Handler = (icallbackeventhandler) page;
}
/// <Summary>
/// Call back the executed client Function
/// </Summary>
Public String functionnameaftercallback
{
Get {return _ functionnameaftercallback ;}
Set {_ functionnameaftercallback = value ;}
}
Public icallbackeventhandler callbackeventhandler
{
Get {return _ handler ;}
Set {_ handler = value ;}
}
/// <Summary>
/// Present the control to the specified output parameter.
/// </Summary>
/// <Param name = "output"> HTML writer to be written </param>
Protected override void render (htmltextwriter output)
{
Output. Write (this. getcallbackeventreference ());
}
Protected override void oninit (eventargs e ){
Base. oninit (E );
Try {
This. handlerequest ();
}
Catch (exception ex ){
Object o = ex;
}
}
private void handlerequest () {
httprequest request = This. page. request;
httpresponse reponse = This. page. response;
string stype = request ["type"];
If (stype! = NULL) & (stype = "_ scriptcallback:" + this. ID)
{< br> string srequest = This. getrequestcontent (request);
string sresult = This. callhandler (srequest);
reponse. clear ();
reponse. expires = 0;
reponse. write (sresult);
reponse. end ();
}< BR >}
Private string getrequestcontent (httprequest request ){
Httprequest r = request;
String S = R. tostring ();
Using (streamreader reader = new streamreader (request. inputstream )){
Return reader. readtoend ();
}
}
Private string callhandler (string PARAM ){
If (this. callbackeventhandler! = NULL ){
Return this. callbackeventhandler. raisecallbackevent (this, Param );
}
Return "";
}
/*
private icallbackeventhandler getscriptcallbackhandleronpage (string ID)
{< br> return this. getscriptcallbackhandler (this. page, ID);
}
private icallbackeventhandler getscriptcallbackhandler (Control, string ID)
{< br> If (control is icallbackeventhandler) & (control. id = ID)
{< br> return control as icallbackeventhandler;
}< br> foreach (control child in control. controls)
{< br> icallbackeventhandler handler = getscriptcallbackhandler (child, ID);
If (handler! = NULL)
{< br> return handler;
}< BR >}< br> return NULL;
}< br> */
Private string getcallbackeventreference ()
{
Stringbuilder sb = new stringbuilder ();
SB. append ("<SCRIPT> ");
SB. append ("function" + this. ID + "(Param, context ){");
SB. append ("Var XMLHTTP = new activexobject ('msxml2. xmlhttp ');");
SB. append ("XMLHTTP. Open ('post', '" + httpcontext. Current. Request. url. tostring () + "? Type =__ scriptcallback: "+ this. ID +" ', false );");
SB. append ("XMLHTTP. Send (PARAM );");
SB. append (this. functionnameaftercallback + "(XMLHTTP. responsetext, context );");
SB. append ("}");
SB. append ("</SCRIPT> ");
Return sb. tostring ();
}
}< BR >}