真爱无限的知识驿站

学习积累技术经验,提升自身能力

asp.net自定义控件中注册Javascript的问题

.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">
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 <span style="color:#ff0000;">CB4949501DA_checkLong </span>= 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>
";


再添加代码去调用写入的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>


发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

Powered By Z-BlogPHP 1.7.3

Copyright 2024-2027 pukuimin Rights Reserved.
粤ICP备17100155号