asp.net custom control register JavaScript Problem Solution _ Practical Tips

Source: Internet
Author: User

Copy Code code as follows:

protected override void OnPreRender (EventArgs e)
{

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

private void Renderjs ()
{
if (! Page.ClientScript.IsClientScriptBlockRegistered (script_id))//If you have not yet registered a statement, register
{
Page.ClientScript.RegisterClientScriptBlock (this. GetType (), script_id,script_content);
}

}
Private Const string script_id = "5B7A061B93D546A7A2601D56A8738DB9";//Custom Identity GUID
Private Const string script_content = "<script type=\" text/javascript\ >\nvar cb4949501da_checkdouble = function ( Data) {\nvar key = event.keycode;\nif (Key < | | | key >) && key!= && key!=) {\nreturn F Alse;\n}\nelse {\nif (key = =) {\nif (Data.indexof (\ ". \")!=-1 | | data.length = 0) \nreturn false;\n}\nelse if (key = = {\nif (Data.indexof (\ "-\")!=-1 | | data.length!= 0} {\nreturn False;\n}\n}\n}\nreturn true;\n}\nvar CB4949501DA_chec Kint = function (data) {\nvar key = event.keycode;\n\nif ((Key < | | | key >) && key!=) {\nreturn fal Se;\n}\nelse {\nif (key = =) {\nif (Data.indexof (\-\)!=-1 | | data.length!= 0) {\nreturn false;\n}\n}\nelse {\nvar i Nput = String.fromCharCode (key) \nvar Intdata = parseint (data + input) \nif (Intdata > 2147483647 | | intdata <-21474 83648) {\nreturn false;\n}\n}\nreturn true;\n}\nreturn true;\n}\nvar <span style= "color: #ff0000;" >cb4949501da_checklong </span>= FunctioN (data) {\nvar key = event.keycode;\n\nif (Key < | | | key > () && key!=) {\nreturn False;\n}\nelse { \nif (key = =) {\nif (Data.indexof (\-\)!=-1 | | data.length!= 0) {\nreturn false;\n}\n}\nelse {\nvar input = String. fromCharCode (key) \nvar longstrdata = data + input\nif (Longstrdata.length >) {\nreturn true ; \n}\n</script>\n ";

Add code to invoke the written JavaScript function:
Copy Code code as follows:

protected override void AddAttributesToRender (System.Web.UI.HtmlTextWriter writer)
{

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

In addition, the reference file is like this.
Copy Code code as follows:

if (! Page.ClientScript.IsClientScriptIncludeRegistered ("Reference JS file 1")//if there is no reference, then reference
{
Page.ClientScript.RegisterClientScriptInclude ("Quote JS file 1", "Js/myjs.") JS ");
}

Generated pages view the source code effect as follows:
Copy Code code 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 < | | key >) && key!= && 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 < | | | key >) && 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 < | | | key >) && 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.