Clientscriptmanager manages client scripts

Source: Internet
Author: User
Tags javascript array

ClientscriptmanagerClass is used to manage client scripts and add them to Web ApplicationsProgram. You can obtain a reference to the clientscriptmanager class from the clientscript attribute of the page object.

ProgramSource code: Clientscriptmanager.rar

Add a script to the client:

/**/ ///   <Summary>
/// Add a script to the client
///   </Summary>
///   <Param name = "page"> Page for adding a script </Param>
///   <Param name = "csname"> Script key </Param>
///   <Param name = "cstext"> Script content </Param>
Public   Void Registerclientscriptblock (page, String Csname, String Cstext)
{
Clientscriptmanager CS = Page. clientscript;
Type cstype = Page. GetType ();

If ( ! CS. isclientscriptblockregistered (cstype, csname ))
{
CS. registerclientscriptblock (cstype, csname, cstext,True);
}

}

For example:

Protected   Void Page_load ( Object Sender, eventargs E)
{
Scriptmanager= NewScriptmanager ();
Scriptmanager. registerclientscriptblock (This,"Testalert","Alert ('test alert! ');");
}

Add an array to the client:

/**/ ///   <Summary>
/// Register the Javascript array declaration with the Page Object using the array name and array value.
///   </Summary>
///   <Param name = "page"> Page for adding scripts </Param>
///   <Param name = "arrayname"> Array name </Param>
///   <Param name = "array"> Array value </Param>
Public   Void Registerarraydeclaration (page, String Arrayname, array)
{
Stringbuilder sbarrayvalue = New Stringbuilder ();
For ( Int I =   0 ; I < Array. length; I ++ )
{
If (I ! =   0 )
{
Sbarrayvalue. append (",\""+ Array. getvalue (I). tostring () +"\"");
}
Else
{
Sbarrayvalue. append ("\""+ Array. getvalue (I). tostring () +"\"");
}
}

Clientscriptmanager CS = Page. clientscript;
CS. registerarraydeclaration (arrayname, sbarrayvalue. tostring ());
}

For example:

Protected   Void Page_load ( Object Sender, eventargs E)
{
String Arrayname =   " Testarray " ;
Int [] Array = {1,2,3} ;
Scriptmanager =   New Scriptmanager ();
Scriptmanager. registerarraydeclaration ( This , Arrayname, array );

Stringbuilder cstext =   New Stringbuilder ();
Cstext. append ( " Function dosum (){ " );
Cstext. append ( " VaR arrsum = " );
Cstext. append ( " Parseint ( "   + Arrayname +   " [0]) + " );
Cstext. append ( " Parseint ( "   + Arrayname +   " [1]) + " );
Cstext. append ( " Parseint ( "   + Arrayname +   " [2]); " );
Cstext. append ( " Alert (arrsum ); " );
Cstext. append ( " } " );

Scriptmanager. registerclientscriptblock ( This , " Sum " , Cstext. tostring ());
Btndosum. onclientclick =   " Dosum (); Return false " ;

}

The client script includes:

/**/ ///   <Summary>
/// Use the key and URL to register the client script inclusion with the page object.
///   </Summary>
///   <Param name = "page"> Page for adding scripts </Param>
///   <Param name = "includename"> The key of the client script to be registered. </Param>
///   <Param name = "url"> The URL of the client script to be registered. </Param>
Public   Void Registerclientscriptinclude (page, String Includename, String URL)
{
Clientscriptmanager CS=Page. clientscript;
CS. registerclientscriptinclude (includename, URL );
}

For example:

Protected   Void Page_load ( Object Sender, eventargs E)
{< br> scriptmanager = New scriptmanager ();
scriptmanager. registerclientscriptinclude ( This , " testinclude " , " JScript. JS " );
}

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.