ASP.Net 1.x 中 __doPostBack实现的问题
2004-11-25 09:04
591 查看
ASP.Net 1.x的client side postback script是这样的:
<!--
function __doPostBack(eventTarget, eventArgument) {
var theform;
if (window.navigator.appName.toLowerCase().indexOf("netscape") > -1) {
theform = document.forms["Form1"];
}
else {
theform = document.Form1;
}
theform.__EVENTTARGET.value = eventTarget.split("$").join(":");
theform.__EVENTARGUMENT.value = eventArgument;
theform.submit();
}
// -->
它是通过form.submit()去submit的。这样就有一个问题,form.onsubmit事件不会被触发,这么一来可能有些client side validation script就被绕过了。在ASP.Net 2.0里,这个问题被fix了。对于ASP.Net 1.x,据我所知,SP1也没有解决这个问题。我们可以使用下面的代码解决这个问题:
string myDoPostBack = @"
<script language=""javascript"">
<!--
function __myDoPostBack(eventTarget, eventArgument) {
var theform;
if (window.navigator.appName.toLowerCase().indexOf(""netscape"") > -1) {
theform = document.forms[""Form1""];
}
else {
theform = document.Form1;
}
theform.__EVENTTARGET.value = eventTarget.split(""$"").join("":"");
theform.__EVENTARGUMENT.value = eventArgument;
if ((typeof(theform.onsubmit) == ""function"") && theform.onsubmit()!=false) {
theform.submit();
}
}
__doPostBack = __myDoPostBack
// -->
</script>";
RegisterStartupScript("myDoPostBack", myDoPostBack);
<!--
function __doPostBack(eventTarget, eventArgument) {
var theform;
if (window.navigator.appName.toLowerCase().indexOf("netscape") > -1) {
theform = document.forms["Form1"];
}
else {
theform = document.Form1;
}
theform.__EVENTTARGET.value = eventTarget.split("$").join(":");
theform.__EVENTARGUMENT.value = eventArgument;
theform.submit();
}
// -->
它是通过form.submit()去submit的。这样就有一个问题,form.onsubmit事件不会被触发,这么一来可能有些client side validation script就被绕过了。在ASP.Net 2.0里,这个问题被fix了。对于ASP.Net 1.x,据我所知,SP1也没有解决这个问题。我们可以使用下面的代码解决这个问题:
string myDoPostBack = @"
<script language=""javascript"">
<!--
function __myDoPostBack(eventTarget, eventArgument) {
var theform;
if (window.navigator.appName.toLowerCase().indexOf(""netscape"") > -1) {
theform = document.forms[""Form1""];
}
else {
theform = document.Form1;
}
theform.__EVENTTARGET.value = eventTarget.split(""$"").join("":"");
theform.__EVENTARGUMENT.value = eventArgument;
if ((typeof(theform.onsubmit) == ""function"") && theform.onsubmit()!=false) {
theform.submit();
}
}
__doPostBack = __myDoPostBack
// -->
</script>";
RegisterStartupScript("myDoPostBack", myDoPostBack);
相关文章推荐
- 在ASP.NET中动态修改文件下载[翻译]
- 编写安全的ASP代码
- 在网上看到有朋友要DVBBS -> ASPNETForums2.0的转换程序.
- 跟踪的诀窍
- DataSet导出CSV格式(ASP.NET,C#)
- ASP.NET的修复
- ASP.NET Web 服务还是 .NET Remoting:如何选择
- ASP.NET中使用数据处理插入数据注意的问题
- asp实现k线图(在线)
- [ASP-Last-Code]显示ASP页面的代码
- JasperReport 学习之路
- 利用反射实现ASP.NET控件和数据实体之间的双向绑定,并且在客户端自动验证输入的内容是否合法
- 用asp.net实现把此文推荐给好友的功能
- ASP.NET读取POP3邮件的操作(纯代码)
- ASP如何获取客户端真实IP地址
- 用ASP.NET设计高效邮件列表
- JasperReport 学习之路
- 安全至上---ASP“动网论坛”漏洞分析
- ASP.NET Session详解
- 如何使用ASP实现网站的“目录树”管理