欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net自定义控件中注册Javascript问题解决方案

程序员文章站 2024-02-25 21:05:09
复制代码 代码如下: protected override void onprerender(eventargs e) { base.onprerender(e); ren...

复制代码 代码如下:

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>