ASP.NET与JavaScript开发常见陷阱及实用技巧汇总
作者:青柠时间:2026-10-25 15:48:01浏览:297
本文整理了ASP.NET开发中Button控件OnClientClick需加return防止回发、window.showModalDialog内嵌编辑器失效等常见问题,并提供了Checkbox全选、Table列合并、定时器管理、页面保存、WebBrowser控件调用及CSS样式控制等JavaScript实用代码示例。
ASP.NET OnClientClick与模态窗口问题
在编写ASP.NET的<asp:Button>控件时,若定义了OnClientClick客户端点击事件,务必在JavaScript函数前加上return关键字。否则,即使客户端验证返回false,服务器端的OnClick事件仍会被触发,导致逻辑错误。
具体示例如下:
代码实现:
<asp:Button ID="btnSearch" runat="server" CssClass="button" Text="查找...." OnClientClick="return CheckBox();" OnClick="btnSearch_Click"></asp:Button> 使用window.showModalDialog()方法打开模态窗口时,如果窗口内部包含JavaScript编辑器或其他第三方编辑组件,可能会出现无法编辑甚至无法选中文本的情况。
目前尚未找到完美的解决方案,建议改用window.open()方式打开新窗口以规避此问题。
关于Checkbox全选功能的实现:
HTML结构如下:
代码实现:
<input type="checkbox" id="chkAll" onclick="selectAll()" />全选
<asp:CheckBoxList ID="ckblist" runat="server" RepeatLayout="Flow" RepeatDirection="Horizontal"></asp:CheckBoxList> 对应的JavaScript逻辑:
代码实现:
function selectAll()
{
var obj = document.getElementById("chkAll");
var ckblist = document.getElementById("ckblist");
var chk = ckblist.getElementsByTagName("input");
if(obj.checked)
{
for(var i = 0; i < chk.length; i++)
{
chk[i].checked = true;
}
}
else
{
for(var i = 0; i < chk.length; i++)
{
chk[i].checked = false;
}
}
} JavaScript常用功能代码示例
以下是一些JavaScript基础操作技巧:
代码示例:
①
//获取Table元素并修改其列合并样式
<script type="text/javascript">
function setColSpan()
{
var x=document.getElementById('myTable').rows[0].cells //获取table的第一行
x[0].colSpan="1" //改变table的样式
x[1].colSpan="3" //同上
}
</script>
<table id="myTable" border="1">
<tr>
<td colspan="2">单元格1</td>
<td colspan="2">单元格2</td>
</tr>
<tr>
<td>单元格3</td>
<td>单元格4</td>
<td>单元格5</td>
<td>单元格6</td>
</tr>
</table>
<input type="button" onclick="setColSpan()" value="改变colspan值">
②
//JS中的定时循环调用函数 setInterval (无限弹)和对应的停止函数
var timer //首先要声明一个变量用来存储setInterval返回
的值.
timer=setInterval("alert('123')",500); //用声明好的变量存储setInterval返回的值.
clearInterval(timer); //清除setInterval函数
如果确实要在setInterval方法中调用参数,并且该函数需要传递参数,那么可以通过通过如下方式:把要传的值声明为全局变量,然
后在方法中进行调用.例如:
<script type="text/javaScript">
var n=1,obj ;
function f1()
{ obj =setInterval(f2(),1000) }
function f2()
{ alert(n+=n;)}
</script>
③
//JS获得本页网址
var Url = window.location.href;
④
//JS控制保存本页面
document.execCommand("SaveAs","","C:\\index.htm"); //1.要进行的操作名,2.默认保存到的地址和文件名和文件类型
function save(dizhi){
document.execCommand("SaveAs","",dizhi);
}
⑤
//打开,另存为,属性,打印"等14个JS代码
■打开■
<input name=Button onClick=document.all.WebBrowser.ExecWB(1,1) type=button value=打开>
<OBJECT classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height=0 id=WebBrowser width=0></OBJECT>
■另存为■
<input name=Button onClick=document.all.WebBrowser.ExecWB(4,1) type=button value=另存为><OBJECT
classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height=0 id=WebBrowser width=0></OBJECT>
■属性■
<input name=Button onClick=document.all.WebBrowser.ExecWB(10,1) type=button value=属性><OBJECT
classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height=0 id=WebBrowser width=0></OBJECT>
■打印■
<input name=Button onClick=document.all.WebBrowser.ExecWB(6,1) type=button value=打印><OBJECT
classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height=0 id=WebBrowser width=0></OBJECT>
■页面设置■
<input name=Button onClick=document.all.WebBrowser.ExecWB(8,1) type=button value=页面设置><OBJECT
classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height=0 id=WebBrowser width=0></OBJECT>
■刷新■
<input type=button value=刷新 name=refresh onclick="window.location.reload()">
■导入收藏■
<input type="button" name="Button" value="导入收藏夹" onClick=window.external.ImportExportFavorites(true,);>
■导出收藏■
<input type="button" name="Button3" value="导出收藏夹" onClick=window.external.ImportExportFavorites(false,);>
■加入收藏■
<INPUT name=Button2 onclick="window.external.AddFavorite(location.href, document.title)" type=button value=加入收藏
夹>
■整理收藏夹■
<INPUT name=Submit2 onclick="window.external.ShowBrowserUI(OrganizeFavorites, null)" type=button value=整理收藏夹>
■查看原文件■
<INPUT name=Button onclick=window.location = "view-source:" + window.location.href type=button value=查看源文件>
■语言设置■
<INPUT name=Button onclick="window.external.ShowBrowserUI(LanguageDialog, null)" type=button value=语言设置>
■前进■
<INPUT name=Submit onclick=history.go(1) type=submit value=前进>
■后退■
<INPUT name=Submit2 onclick=history.go(-1) type=submit value=后退>
⑥
//改变CSS样式
一、局部改变样式(区分大小写)
改变className(不用加上style)
document.getElementById('t2').className=”…”
改变classText(必须加上style)
document.getElementById('t2').style.cssText=”…”
直接改变控件的CSS的样式
document.getElementById('t2').style.backgroundColor=”#003366″
二、全局改变样式(换肤)
给现在使用的CSS样式加一个id,换肤的时候,更改引用的CSS样式的路径,例如:
<link rel = "stylesheet" type="text/css" id="css" href="firefox.css" />
<span on click="javascript:document.getElementById('css').href = 'ie.css'">点我改变样式</span>
⑦
//生成随机数,参数为(最小值,最大值)
function GetRnd(min,max){
return parseInt(Math.random()*(max-min+1));
}
⑧
//JS中把字符串转化为数字
parseInt("123"); //123作者最新文章
ES6模块化中js文件怎么用import和export交互
2026-11-05 17:00:48
JS读取本地文件时报错或无响应的常见原因
2026-11-05 16:32:31
let、const与var的区别:JavaScript变量声明最佳实践
2026-11-05 14:16:38
JS中直接定义静态二维数组的字面量写法
2026-11-05 14:16:08
JS 怎么查找字符串中所有匹配项的位置
2026-11-05 14:15:53
