__doPostBack的瞭解與備註

__doPostBack是通過__EVENTTARGET與__EVENTARGUMENT

__EVENTTARGET是目標控件

__EVENTTARGUMENT是給傳參數

此事件對於使用Visble的控件無效,但可以使用Style="Display:none"來實現隱藏

如在母版頁的子頁面進行__doPostBack時需要使用如下操作:

                        var obj = document.getElementById("<%= btnRefresh.ClientID%>");
                        __doPostBack(obj.name, '');


轉載原址如下: http://www.cnblogs.com/ttc/archive/2011/03/25/1995651.html

一、 在後臺判斷哪個按鈕點擊了。

前臺代碼:

<button type="button" id="Button1" οnclick="<strong><span style="color:#3333FF;">__doPostBack('btnReturn','1')</span></strong>">查詢</button>

注意 onclick="__doPostBack('btnReturn','1')"

第一個參數表示按鈕ID,第二參數隨便寫的,爲的就是在後臺確定按鈕是否被點擊了。

後臺代碼:

string controlName = Request.Params.Get( "__EVENTTARGET" );
string eventArgument = Request.Params.Get( "__EVENTARGUMENT" );

// 確定按鈕事件
if( controlName == "btnConfirm" && eventArgument == "2" )
{
  btnConfirm_Click( sender, e );
}

// 查詢按鈕事件
if( controlName == "btnReturn" && eventArgument == "1" )
{
  btnReturn_Click( sender, e );
}

二、應用二,利用eventArgument 向後臺傳遞參數。

前臺JS代碼:

__doPostBack('btnDelete',delStr);

delStr就是向後臺傳遞的一個字符串。

 後臺代碼:

string eventArgument = Request.Params.Get( "__EVENTARGUMENT" );
string[] delStr = eventArgument.Split( ',' );


其它鏈接: http://blog.csdn.net/prayerlee/article/details/4516138

下面演示下如何調用後臺事件:

1.新建工程

2.拖入一個服務端Button1,一個DropDownList1和一個客戶端Button

3.設置DropDownList1的AutoPostBack屬性爲True,Button1的Visible爲False

4.雙擊Button1,在事件裏寫下Response.Write("hello:" );

5.頁面的HTML裏找到客戶端Button,寫入οnclick="__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>
 <input type="hidden" value="" />
 <input type="hidden" value="" />

細 心的人會發現,在__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>


其它鏈接:http://www.cnblogs.com/hjf1223/archive/2006/07/05/443761.html此鏈接有源碼下載

鏈接地址:

http://itworktor.blog.163.com/blog/static/175203029201103071652517/

發佈了13 篇原創文章 · 獲贊 2 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章