How to register Javascript in asp.net Custom Controls

Source: Internet
Author: User

Copy codeThe Code is as follows:
Protected override void OnPreRender (EventArgs e)
{

Base. OnPreRender (e );
RenderJS ();
}

Private void RenderJS ()
{
If (! Page. ClientScript. IsClientScriptBlockRegistered (SCRIPT_ID) // if no statement has been registered, register
{
Page. ClientScript. RegisterClientScriptBlock (this. GetType (), SCRIPT_ID, SCRIPT_CONTENT );
}

}
Private const string SCRIPT_ID = "5B7A061B93D546A7A2601D56A8738DB9"; // custom ID Guid
Private const string SCRIPT_CONTENT = "<script type = \" text/javascript \ "> \ nvar CB4949501DA_checkDouble = function (data) {\ nvar key = event. keyCode; \ nif (key <48 | key> 57) & key! = 46 & key! = 45) {\ nreturn false; \ n} \ nelse {\ nif (key = 46) {\ nif (data. indexOf (\".\")! =-1 | data. length = 0) \ nreturn false; \ n} \ nelse if (key = 45) {\ nif (data. indexOf (\"-\")! =-1 | data. length! = 0) {\ nreturn false; \ n} \ nreturn true; \ n} \ nvar cb49501da_checkint = function (data) {\ nvar key = event. keyCode; \ n \ nif (key <48 | key> 57) & key! = 45) {\ nreturn false; \ n} \ nelse {\ nif (key = 45) {\ nif (data. indexOf (\"-\")! =-1 | data. length! = 0) {\ nreturn false; \ n} \ nelse {\ nvar input = String. fromCharCode (key) \ nvar intData = parseInt (data + input); \ nif (intData> 2147483647 | intData <-2147483648) {\ nreturn false; \ n} \ nreturn true; \ n} \ nvar <span style = "color: # ff0000; "> CB4949501DA_checkLong </span> = function (data) {\ nvar key = event. keyCode; \ n \ nif (key <48 | key> 57) & key! = 45) {\ nreturn false; \ n} \ nelse {\ nif (key = 45) {\ nif (data. indexOf (\"-\")! =-1 | data. length! = 0) {\ nreturn false; \ n} \ nelse {\ nvar input = String. fromCharCode (key) \ nvar longstrData = data + input \ nif (longstrData. length> 19) {\ nreturn false; \ n} \ nreturn true; \ n} \ n </script> \ n ";

Then add the code to call the written javascript function:
Copy codeThe Code is as follows:
Protected override void AddAttributesToRender (System. Web. UI. HtmlTextWriter writer)
{

Base. AddAttributesToRender (writer );
Writer. addattripress ("onkeypress", "return <span style =" color: # ff0000; "> CB4949501DA_checkLong </span> (this. value );");}

In addition, the reference file is like this.
Copy codeThe Code is as follows:
If (! Page. ClientScript. IsClientScriptIncludeRegistered ("reference js File 1") // if no reference is available, reference
{
Page. ClientScript. RegisterClientScriptInclude ("reference js File 1", "JS/MYJS. JS ");
}

View the source code on the generated page. The result is as follows:
Copy codeThe Code is as follows:
<Script src = "JS/MYJS. JS" type = "text/javascript"> </script>
<Script type = "text/javascript">
Var CB4949501DA_checkDouble = function (data ){
Var key = event. keyCode;
If (key <48 | key> 57) & key! = 46 & key! = 45 ){
Return false;
}
Else {
If (key = 46 ){
If (data. indexOf (".")! =-1 | data. length = 0)
Return false;
}
Else if (key = 45 ){
If (data. indexOf ("-")! =-1 | data. length! = 0 ){
Return false;
}
}
}
Return true;
}
Var CB4949501DA_checkInt = function (data ){
Var key = event. keyCode;

If (key <48 | key> 57) & key! = 45 ){
Return false;
}
Else {
If (key = 45 ){
If (data. indexOf ("-")! =-1 | data. length! = 0 ){
Return false;
}
}
Else {
Var input = String. fromCharCode (key)
Var intData = parseInt (data + input );
If (intData> 2147483647 | intData <-2147483648 ){
Return false;
}
}
Return true;
}
Return true;
}
Var CB4949501DA_checkLong = function (data ){
Var key = event. keyCode;

If (key <48 | key> 57) & key! = 45 ){
Return false;
}
Else {
If (key = 45 ){
If (data. indexOf ("-")! =-1 | data. length! = 0 ){
Return false;
}
}
Else {
Var input = String. fromCharCode (key)
Var longstrData = data + input
If (longstrData. length> 19 ){
Return false;
}
}
}
Return true;
}
</Script>

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.