關(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)里面多了下面行
<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è)隱藏控件
<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è)面均有效
<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>
這里介紹一個(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)文章
asp.net(c#)實(shí)現(xiàn)從sqlserver存取二進(jìn)制圖片的代碼
有一個(gè)員工表Employee,需要保存員工照片(Photo)到數(shù)據(jù)庫(kù)(sql server)上。員工照片對(duì)應(yīng)的字段是varbinary(max),也就是要存成二進(jìn)制文件類(lèi)型(這和以前討巧地存圖片文件路徑就不相同了),默認(rèn)可以為空。2011-09-09使用微信PC端的截圖dll庫(kù)實(shí)現(xiàn)微信截圖功能
這篇文章主要為大家詳細(xì)介紹了使用微信PC端的截圖dll庫(kù)實(shí)現(xiàn)微信截圖功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06asp.net(C#)跨域及跨域?qū)慍ookie問(wèn)題
在網(wǎng)站www.A.com下通過(guò)iframe或ajax調(diào)用www.B.com下的內(nèi)容時(shí),默認(rèn)情況下IE會(huì)阻止www.B.com寫(xiě)任何Cookie2011-10-10基于ASP.NET+EasyUI框架實(shí)現(xiàn)圖片上傳提交表單功能(js提交圖片)
這篇文章主要介紹了基于ASP.NET+EasyUI框架實(shí)現(xiàn)圖片上傳再提交表單(js提交圖片)的相關(guān)資料,需要的朋友可以參考下2016-06-06一個(gè)ASP.Net下的WebShell實(shí)例
一個(gè)ASP.Net下的WebShell,主要完成cmd命令。一般的服務(wù)器設(shè)置,asp.net用戶的權(quán)限都比較高。如果asp的webshell無(wú)法執(zhí)行,可能asp.net的可以執(zhí)行。2013-07-07asp.net下生成英文字符數(shù)字驗(yàn)證碼的代碼
用了asp.net隨機(jī)數(shù),獲取指定位數(shù)的字母或數(shù)字以后,進(jìn)行圖片輸出的驗(yàn)證碼函數(shù)。2009-12-12Visual?Studio創(chuàng)建WPF項(xiàng)目
這篇文章介紹了使用Visual?Studio創(chuàng)建WPF項(xiàng)目的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04