关于前台调用后台事件__doPostBack函数
这里需要提一下的是,asp.net编程提供了服务端控件和客户端控件的说法,其实还是脱离不了HTML的本质,客户端和服务端需要交互必须要提交,提交有两种方式get和post,get就是通过向服务端发送连接地址,服务端通过地址的参数来获得信息的,一般这些参数都是明文,能在浏览器地址栏看到。而post是通过表单的input等元素提交到服务端的页面的,这些数据一般是看不到的。asp.net的服务端控件其实就是对一般的HTML控件做了个包装,大体是通过隐藏控件提供控制的参数的。 
这里介绍一个常用的函数_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">
<script language="javascript"> 
![]() <!--
<!-- 
![]() function __doPostBack(eventTarget, eventArgument) {
    function __doPostBack(eventTarget, eventArgument) { 
![]() var theform;
        var theform; 
![]() if (window.navigator.appName.toLowerCase().indexOf("netscape") > -1) {
        if (window.navigator.appName.toLowerCase().indexOf("netscape") > -1) { 
![]() theform = document.forms["Form1"];
            theform = document.forms["Form1"]; 
![]() }
        } 
![]() else {
        else { 
![]() theform = document.Form1;
            theform = document.Form1; 
![]() }
        } 
![]() theform.__EVENTTARGET.value = eventTarget.split("$").join(":");
        theform.__EVENTTARGET.value = eventTarget.split("$").join(":"); 
![]() theform.__EVENTARGUMENT.value = eventArgument;
        theform.__EVENTARGUMENT.value = eventArgument; 
![]() theform.submit();
        theform.submit(); 
![]() }
    } 
![]() // -->
// --> 
![]() </script>
</script> 
以及两个隐藏控件
![]() <input type="hidden" name="__EVENTTARGET" value="" />
<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">
            <script language="javascript"> 
![]() <!--
<!-- 
![]() function __doPostBack_Ex(eventTarget, eventArgument)
    function __doPostBack_Ex(eventTarget, eventArgument)  
![]() {
    { 
![]() var theform;
        var theform; 
![]() if (window.navigator.appName.toLowerCase().indexOf("netscape") > -1) {
        if (window.navigator.appName.toLowerCase().indexOf("netscape") > -1) { 
![]() theform = document.forms[0];
            theform = document.forms[0]; 
![]() }
        } 
![]() else {
        else { 
![]() theform = document.forms[0];
            theform = document.forms[0]; 
![]() }
        } 
![]() 
 
![]() if(!theform.__EVENTTARGET)
        if(!theform.__EVENTTARGET) 
![]() {
        {             
![]() theform.appendChild(document.createElement("<input type='hidden' name='__EVENTTARGET'>"));
            theform.appendChild(document.createElement("<input type='hidden' name='__EVENTTARGET'>")); 
![]() }
        } 
![]() 
         
![]() if(!theform.__EVENTARGUMENT)
        if(!theform.__EVENTARGUMENT) 
![]() {
        {             
![]() theform.appendChild(document.createElement("<input type='hidden' name='__EVENTARGUMENT'>"));
            theform.appendChild(document.createElement("<input type='hidden' name='__EVENTARGUMENT'>"));                         
![]() }
        } 
![]() 
         
![]() theform.__EVENTTARGET.value = eventTarget.split("$").join(":");
        theform.__EVENTTARGET.value = eventTarget.split("$").join(":"); 
![]() theform.__EVENTARGUMENT.value = eventArgument;
        theform.__EVENTARGUMENT.value = eventArgument; 
![]() if ((typeof(theform.onsubmit) == "function"))
        if ((typeof(theform.onsubmit) == "function"))  
![]() {
        { 
![]() if(theform.onsubmit()!=false)
            if(theform.onsubmit()!=false) 
![]() {
            { 
![]() theform.submit();
                theform.submit();     
![]() }
            } 
![]() }
        } 
![]() else
        else 
![]() {
        {             
![]() theform.submit();
            theform.submit();     
![]() }
        } 
![]() 
         
![]() function __doPostBack(eventTarget, eventArgument)
        function __doPostBack(eventTarget, eventArgument) 
![]() {
        { 
![]() __doPostBack_Ex(eventTarget, eventArgument);
            __doPostBack_Ex(eventTarget, eventArgument); 
![]() }
        } 
![]() 
         
![]() 
 
![]() }
    } 
![]() // -->
// --> 
![]() </script>
            </script> 
这里介绍一个常用的函数_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">
<script language="javascript">  <!--
<!--  function __doPostBack(eventTarget, eventArgument) {
    function __doPostBack(eventTarget, eventArgument) {  var theform;
        var theform;  if (window.navigator.appName.toLowerCase().indexOf("netscape") > -1) {
        if (window.navigator.appName.toLowerCase().indexOf("netscape") > -1) {  theform = document.forms["Form1"];
            theform = document.forms["Form1"];  }
        }  else {
        else {  theform = document.Form1;
            theform = document.Form1;  }
        }  theform.__EVENTTARGET.value = eventTarget.split("$").join(":");
        theform.__EVENTTARGET.value = eventTarget.split("$").join(":");  theform.__EVENTARGUMENT.value = eventArgument;
        theform.__EVENTARGUMENT.value = eventArgument;  theform.submit();
        theform.submit();  }
    }  // -->
// -->  </script>
</script> 以及两个隐藏控件
 <input type="hidden" name="__EVENTTARGET" value="" />
<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">
            <script language="javascript">  <!--
<!--  function __doPostBack_Ex(eventTarget, eventArgument)
    function __doPostBack_Ex(eventTarget, eventArgument)   {
    {  var theform;
        var theform;  if (window.navigator.appName.toLowerCase().indexOf("netscape") > -1) {
        if (window.navigator.appName.toLowerCase().indexOf("netscape") > -1) {  theform = document.forms[0];
            theform = document.forms[0];  }
        }  else {
        else {  theform = document.forms[0];
            theform = document.forms[0];  }
        }  
  if(!theform.__EVENTTARGET)
        if(!theform.__EVENTTARGET)  {
        {              theform.appendChild(document.createElement("<input type='hidden' name='__EVENTTARGET'>"));
            theform.appendChild(document.createElement("<input type='hidden' name='__EVENTTARGET'>"));  }
        }  
          if(!theform.__EVENTARGUMENT)
        if(!theform.__EVENTARGUMENT)  {
        {              theform.appendChild(document.createElement("<input type='hidden' name='__EVENTARGUMENT'>"));
            theform.appendChild(document.createElement("<input type='hidden' name='__EVENTARGUMENT'>"));                          }
        }  
          theform.__EVENTTARGET.value = eventTarget.split("$").join(":");
        theform.__EVENTTARGET.value = eventTarget.split("$").join(":");  theform.__EVENTARGUMENT.value = eventArgument;
        theform.__EVENTARGUMENT.value = eventArgument;  if ((typeof(theform.onsubmit) == "function"))
        if ((typeof(theform.onsubmit) == "function"))   {
        {  if(theform.onsubmit()!=false)
            if(theform.onsubmit()!=false)  {
            {  theform.submit();
                theform.submit();      }
            }  }
        }  else
        else  {
        {              theform.submit();
            theform.submit();      }
        }  
          function __doPostBack(eventTarget, eventArgument)
        function __doPostBack(eventTarget, eventArgument)  {
        {  __doPostBack_Ex(eventTarget, eventArgument);
            __doPostBack_Ex(eventTarget, eventArgument);  }
        }  
          
  }
    }  // -->
// -->  </script>
            </script>  
                     
                    
                 
                    
                
 
    
 
         
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号