Using httphandler to handle the need to reference a large number of JS files in custom controls

Source: Internet
Author: User

Sometimes, when creating custom controls, you may need to include a lot of Js for combination, especially heavyweight custom controls such as online editors. General controls may also contain a lot of JavaScript, there are roughly two solutions:

1First construct the script string, and then register it to the client script block by directly using registerclientscriptblock or registerstartupscript.
2. Define an attribute for you to specify the JS script path. The custom control registers the script block by obtaining this path.

Of course, the 1st methods are not suitable for a large number of JS cases. The 2nd methods have their advantages and are also a very common method.

There is a new requirement: the control will contain a lot of JS files, and you do not need to specify the JS path, and may choose different JS based on the user's settings on the control attributes. Now we will introduce a simple solution and provide an idea.

This control is very simple. It completes a simple Automatic completion control with the Javascript in the input Automatic completion class of dooit.
1. Prepare the autocomplete. js file, add the JS file to the custom control project, and set its "generate operation" attribute to "embedded resource ".

2. Design a jshttphandler class for httphandler processing to implement the ihttphandler interface, and process a URL with the extension of. axd, such as http: // xxxx/JS. axd,

Using System;
Using System. IO;
Using System. reflection;
Using System. text;
Using System. Web;

Namespace Jrtcontrols
{
/**/ ///   <Summary>
/// Used to process httphandle, which must be in Web. config <System. Web> Node definition:
/// <Httphandlers>
/// <Add verb = "*" Path = "Js. axd" type = "jrtcontrols. jshttphandler, jrtcontrols"/>
/// </Httphandlers>
///   </Summary>
Public   Class Jshttphandler: ihttphandler
{
Public Jshttphandler ()
{
}

Ihttphandler Member # Region Ihttphandler Member

Internal   Const   String Resourcehandlerpagename =   " JS. axd " ;

/**/ /// <Summary>
///Processing Js. ASD? Res = autocomplete. js
/// </Summary>
/// <Param name = "context"> </param>
Public   Void Processrequest (httpcontext context)
{
Assembly ASM =   Null ;
Streamreader SR =   Null ;
Context. response. Clear ();
String Resname = Context. Request. querystring [ " Res " ];
String Restype = Resname. substring (resname. lastindexof ( ' . ' ) +   1 ). Tolower ();
ASM = Assembly. getexecutingassembly ();

If (Restype = " JS " )
{
Context. response. contenttype =   " Text/JavaScript " ;
Stringbuilder compositeresponse =   New Stringbuilder ();
Sr =   New Streamreader (ASM. getmanifestresourcestream ( Typeof (Jshttphandler ), " AutoComplete. js " ));
Compositeresponse. append (Sr. readtoend (). append (environment. newline );
String Response = Compositeresponse. tostring ();
Context. response. Write (response );
}

}

Public   Bool Isreusable
{
Get
{
Return True;
}
}

# Endregion
}
}

3. In the control implementation section, we mainly rewrite the onprerender method to register the script block: Protected   Override   Void Onprerender (eventargs E)
{
Base . Onprerender (E );
String Typename = Typeof (Autocompletebox). tostring (). Replace ( " . " , " _ " );

// The initialization script is provided to JS. axd? URLs like res = autocomplete. js are processed by httphandle.
If ( ! Page. isclientscriptblockregistered (typename ))
{
Stringbuilder sb =   New Stringbuilder ();
SB. append ( " <SCRIPT type = \ " Text / Javascript \ " Src = \ "" );
SB. append ( " JS. axd? Res = autocomplete. js " );
SB. append ( " \ " > </ Script > " );
Page. registerclientscriptblock (typename, SB. tostring ());
}

If ( ! Page. isstartupscriptregistered (typename ))
{
Stringbuilder sb =   New Stringbuilder ();
SB. append ( " <Script language = 'javascript '> " );
SB. append ( " VaR ACBAR = new class_auotcomplete (); " );
SB. append ( " ACBAR. Setup (document. All. " + This . ID + " ); " );
SB. append ( " </SCRIPT> " );
Page. registerstartupscript (typename, SB. tostring ());
}
}

How to use it after the final completion?
In web. config
<System. web> Configure httphandler under the node. Note the following type = "jrtcontrols. jshttphandler, jrtcontrols ", jrtcontrols. jshttphandler indicates the complete name of the processed class, And jrtcontrols indicates the name of the class library.

< Httphandlers >
< Add Verb = "*" Path = "Js. axd" Type = "Jrtcontrols. jshttphandler, jrtcontrols"   />
</ Httphandlers >

Add the control to the page,
Autolist attribute for the control in the background:
Autocompletebox1.autolist = new string [] {"123123", "123", "werewr "};
The running effect is as follows:

Source codeDownload/files/Jintan/jrtcontrols.rar

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.