Asp.net callback Javascript

Source: Internet
Author: User
Tags call back

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 >}

Related Article

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.