欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

關(guān)于前臺(tái)調(diào)用后臺(tái)事件__doPostBack函數(shù)

 更新時(shí)間:2007年04月13日 00:00:00   作者:  
這里需要提一下的是,asp.net編程提供了服務(wù)端控件和客戶端控件的說(shuō)法,其實(shí)還是脫離不了HTML的本質(zhì),客戶端和服務(wù)端需要交互必須要提交,提交有兩種方式get和post,get就是通過(guò)向服務(wù)端發(fā)送連接地址,服務(wù)端通過(guò)地址的參數(shù)來(lái)獲得信息的,一般這些參數(shù)都是明文,能在瀏覽器地址欄看到。而post是通過(guò)表單的input等元素提交到服務(wù)端的頁(yè)面的,這些數(shù)據(jù)一般是看不到的。asp.net的服務(wù)端控件其實(shí)就是對(duì)一般的HTML控件做了個(gè)包裝,大體是通過(guò)隱藏控件提供控制的參數(shù)的。 
這里介紹一個(gè)常用的函數(shù)_doPostBack,這個(gè)函數(shù)如果如果是ASP.Net render出來(lái)的頁(yè)面就是自動(dòng)產(chǎn)生這個(gè)函數(shù),比如有帶autopostback屬性的控件,且其屬性為true的頁(yè)面,帶編輯列的datagrid頁(yè)面。 
__doPostBack是通過(guò)__EVENTTARGET,__EVENTARGUMENT兩個(gè)隱藏控件向服務(wù)端發(fā)送控制信息的,__EVENTTARGET為要調(diào)用控件的名稱(chēng),如果要調(diào)用的控件是子控件,用''$'或':'分割父控件:子控件,__EVENTARGUMENT是調(diào)用事件時(shí)的參數(shù) 
下面演示下如何調(diào)用后臺(tái)事件: 
1.新建工程 
2.拖入一個(gè)服務(wù)端Button1,一個(gè)DropDownList1和一個(gè)客戶端Button 
3.設(shè)置DropDownList1的AutoPostBack屬性為T(mén)rue,Button1的Visible為False 
4.雙擊Button1,在事件里寫(xiě)下Response.Write("hello:" ); 
5.頁(yè)面的HTML里找到客戶端Button,寫(xiě)入onclick="__doPostBack('Button1','')" 
6.編譯,運(yùn)行,點(diǎn)擊Button是不是出現(xiàn)了"Hello" 
7.查看源代碼,發(fā)現(xiàn)里面多了下面行 
復(fù)制代碼 代碼如下:

<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> 

以及兩個(gè)隱藏控件 
復(fù)制代碼 代碼如下:


<input type="hidden" name="__EVENTTARGET" value="" />  
<input type="hidden" name="__EVENTARGUMENT" value="" /> 




值得注意的是,_doPostPack的第一個(gè)參數(shù)是大小寫(xiě)不敏感的 

細(xì)心的人會(huì)發(fā)現(xiàn),在__doPostBack里,提交調(diào)用的是theform.submit(),這樣就導(dǎo)致對(duì)Form的onsubmit事件校驗(yàn)失效了,幸好這個(gè)問(wèn)題在asp.net 2.0已經(jīng)修復(fù)了。這里提供一個(gè)替換的解決辦法,在Form的最下面插入下面的代碼,這段代碼在保證不管是不是render出來(lái)的頁(yè)面均有效  
復(fù)制代碼 代碼如下:

<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> 

相關(guān)文章

最新評(píng)論