这里介绍一个常用的函数_doPostBack,这个函数如果如果是ASP.Net render出来的页面就是自动产生这个函数,比如有带autopostback属性的控件,且其属性为true的页面,带编辑列的datagrid页面。
__doPostBack是通过__EVENTTARGET,__EVENTARGUMENT两个隐藏控件向服务端发送控制信息的,__EVENTTARGET为要调用控件的名称,如果要调用的控件是子控件,用''$'或':'分割父控件:子控件,__EVENTARGUMENT是调用事件时的参数
下面演示下如何调用后台事件:
1.新建工程
2.拖入一个服务端Button1,一个DropDownList1和一个客户端Button
3.设置DropDownList1的AutoPostBack属性为True,Button1的Visible为False
4.双击Button1,在事件里写下Response.Write("hello:" );
5.页面的HTML里找到客户端Button,写入onclick="__doPostBack('Button1','')"
6.编译,运行,点击Button是不是出现了"Hello"
7.查看源代码,发现里面多了下面行
<
script language
=
"
javascript
"
>
<!--
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();
}
// -->
</ 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();
}
// -->
</ script >
以及两个隐藏控件
<
input type
=
"
hidden
"
name
=
"
__EVENTTARGET
"
value
=
""
/>
< input type = " hidden " name = " __EVENTARGUMENT " value = "" />
< input type = " hidden " name = " __EVENTARGUMENT " value = "" />
值得注意的是,_doPostPack的第一个参数是大小写不敏感的
细心的人会发现,在__doPostBack里,提交调用的是theform.submit(),这样就导致对Form的onsubmit事件校验失效了,幸好这个问题在asp.net 2.0已经修复了。这里提供一个替换的解决办法,在Form的最下面插入下面的代码,这段代码在保证不管是不是render出来的页面均有效
<
script language
=
"
javascript
"
>
<!--
function __doPostBack_Ex(eventTarget, eventArgument)
{
var theform;
if (window.navigator.appName.toLowerCase().indexOf("netscape") > -1) {
theform = document.forms[0];
}
else {
theform = document.forms[0];
}
if(!theform.__EVENTTARGET)
{
theform.appendChild(document.createElement("<input type='hidden' name='__EVENTTARGET'>"));
}
if(!theform.__EVENTARGUMENT)
{
theform.appendChild(document.createElement("<input type='hidden' name='__EVENTARGUMENT'>"));
}
theform.__EVENTTARGET.value = eventTarget.split("$").join(":");
theform.__EVENTARGUMENT.value = eventArgument;
if ((typeof(theform.onsubmit) == "function"))
{
if(theform.onsubmit()!=false)
{
theform.submit();
}
}
else
{
theform.submit();
}
function __doPostBack(eventTarget, eventArgument)
{
__doPostBack_Ex(eventTarget, eventArgument);
}
}
// -->
</ script >
本文源码:
下载<!--
function __doPostBack_Ex(eventTarget, eventArgument)
{
var theform;
if (window.navigator.appName.toLowerCase().indexOf("netscape") > -1) {
theform = document.forms[0];
}
else {
theform = document.forms[0];
}
if(!theform.__EVENTTARGET)
{
theform.appendChild(document.createElement("<input type='hidden' name='__EVENTTARGET'>"));
}
if(!theform.__EVENTARGUMENT)
{
theform.appendChild(document.createElement("<input type='hidden' name='__EVENTARGUMENT'>"));
}
theform.__EVENTTARGET.value = eventTarget.split("$").join(":");
theform.__EVENTARGUMENT.value = eventArgument;
if ((typeof(theform.onsubmit) == "function"))
{
if(theform.onsubmit()!=false)
{
theform.submit();
}
}
else
{
theform.submit();
}
function __doPostBack(eventTarget, eventArgument)
{
__doPostBack_Ex(eventTarget, eventArgument);
}
}
// -->
</ script >