程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> ASP.NET基礎 >> asp.net自定義控件中注冊Javascript問題解決方案

asp.net自定義控件中注冊Javascript問題解決方案

編輯:ASP.NET基礎

復制代碼 代碼如下:
protected override void OnPreRender(EventArgs e)
{

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

private void RenderJS()
{
if (!Page.ClientScript.IsClientScriptBlockRegistered(SCRIPT_ID))//如果還沒有注冊語句,則注冊
{
Page.ClientScript.RegisterClientScriptBlock(this.GetType(),SCRIPT_ID,SCRIPT_CONTENT);
}

}
private const string SCRIPT_ID = "5B7A061B93D546A7A2601D56A8738DB9";//自定義標識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}\n}\n}\nreturn true;\n}\nvar CB4949501DA_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}\n}\nelse {\nvar input = String.fromCharCode(key)\nvar intData = parseInt(data + input);\nif (intData > 2147483647 || intData < -2147483648) {\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 < 48 || key > 57) && key != 45) {\nreturn false;\n}\nelse {\nif (key == 45) {\nif (data.indexOf(\"-\") != -1 || data.length != 0) {\nreturn false;\n}\n}\nelse {\nvar input = String.fromCharCode(key)\nvar longstrData = data + input\nif (longstrData.length > 19) {\nreturn false;\n}\n}\n}\nreturn true;\n}\n</script>\n";

再添加代碼去調用寫入的javascript函數:
復制代碼 代碼如下:
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);");}

另外,引用文件是這樣的。
復制代碼 代碼如下:
if (!Page.ClientScript.IsClientScriptIncludeRegistered("引用js文件1"))//如果還沒有引用,則引用
{
Page.ClientScript.RegisterClientScriptInclude("引用js文件1", "JS/MYJS.JS");
}

生成的頁面查看源代碼效果如下:
復制代碼 代碼如下:
<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>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved